I’m new to Defold (Love it so far, as it offers a rigid structure that i lacked so far), trying to get a grasp of how to communicate with objects and access properties.
I’m having the following: local newtile = factory.create("#tilesfactory", pos, nil, {}, 1) pprint(newtile) pprint(go.get(newtile, "passable"))
And this tilesfactory use the prototype tile.go which starts as following: go.property("passable", true) function init(self) [...] end
When i build, i get the following:
First pprint: DEBUG:SCRIPT: hash: [/instance0]
Second: '(null)' does not have any property called 'passable'
Any idea to help me understand how to access properties and use the ids (newtile)?
Thanks in advance for your help,
K
Solution:
I needed to access the script component of the object, to get the property.
So the url to provide to the go.get need to append the “script” fragment.
local url = msg.url(newtile)
url.fragment = "script" -- or any id that you have set on it
pprint(go.get(url, "tiletype"))
DEBUG:SCRIPT: hash: [/instance0]
DEBUG:SCRIPT: url: [main:/instance0]
ERROR:SCRIPT: main/world/world.script:25: '(null)' does not have any property called 'type'
Could you please show me the start of the world.script where you set your go.property type?
Also you should avoid the word type as it is a lua keyword.
eg. type(my_table) == “table” – returns true
require("main.hex")
go.property("TILESIZE", 35)
function init(self)
-- Add initialization code here
-- Remove this function if not needed
self.width = 30
self.height = 15
self.tiles = {}
for q = 1, self.width do
self.tiles[q] = {}
for r = 1, self.height do
local h = Hex(q, r)
local pix = hexToPixel(h, go.get("#", "TILESIZE"))
local tile_pos = vmath.vector3(pix.x, pix.y, -q*.001-r*.001)
--local newtile = collectionfactory.create("#tiles_factory", tile_pos, nil, {}, 1)
local newtile = factory.create("#tiles_factory1", tile_pos, nil, {}, 1)
self.tiles[q][r] = newtile
pprint(newtile)
pprint(msg.url(newtile))
pprint(go.get(msg.url(newtile), "tiletype"))
end
end
print("World built")
end
tile.script (from tile.go, prototype of tiles factory)
go.property("tiletype", hash("grass"))
go.property("passable", true)
function init(self)
local rng = math.random()
if rng > .7 then
go.set("#", "tiletype", hash("water"))
end
if self.type == hash("water") then
msg.post("#sprite", "play_animation", {id = hash("tileWater_full")})
go.set("#", "passable", false)
else
msg.post("#sprite", "play_animation", {id = hash("tileGrass_full")})
end
end
Ah! My bad. It’s hard to code without actually coding
So when you are trying to get the property in world script, your url is pointing to the correct gameobject but it needs to go deeper to refer to the component. In this case the type.script fragment. I don’t know what you set as id on that script but it defaults to “script” in the gameobject when you hook it up.
Gameobjects don’t know about the components different properties.
So I would recommend something like:
local url = msg.url(newtile)
url.fragment = “script” – or any id that you have set on it
pprint(go.get(url, “tiletype”))
I keep on forgetting about addressing the specific components!
It is not intuitive (for me) that you need to address the property of a particular component of an object instead of addressing the property of the object itself!
Thank you so much for your help, will help me greatly in the future!