Accessing property from a spawned instance (SOLVED)

Hi,

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"))
4 Likes

The go.get needs an url to an instance but you are passing in only the instance hash itself.
Try
pprint(go.get(msg.url(newtile), “passable”))

Think of id’s as (and it actually is) a hash of only the object:
eg
hash: [/instance0]

meanwhile an url could really a combination of socket and hashes
url: [main:/instance0]

Hope it will help you out.

1 Like

Thanks for your answer.

Sadly, same thing happens:

pprint(newtile)
pprint(msg.url(newtile))
pprint(go.get(msg.url(newtile), "type"))

Give

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

1 Like

World.script

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 :wink:

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”))

5 Likes

Of course!

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!

Have a nice end of weekend!

K

3 Likes

Thanks I just spent a very long time trying to figure this out. Gotta remember to come to the forum more often

1 Like