Cannot Lookup Tilemap - Instance not found

So I am trying to get tileset data, but the URL I see in the editor isn’t working.

print(tilemap.get_bounds("/map#tiles"))

ERROR:SCRIPT: /main/hero.script:144: Instance /map#tiles not found

This is from the RPG map sample, so the collection is loaded via factory and so I assume the /map#tiles is altered. To get the factory object, I can use /game#factory-0-0.

1 Like

Looks like you’re trying to access the /map object without first going through the /root object. I don’t see your script in the Outline view, so the relative path /map#tiles doesn’t make much sense.

I am not sure what you mean by going through the root object… However, that prompted me to move the code where I am trying to access the tilemap into the exact collection that’s being rendered and the same issue.

When I specify the collection name in the URL I get the following issue:

print(tilemap.get_bounds("0-0:/map#tiles"))

function called can only access instances within the same collection.

Once I moved the script code into the collection, the same path did not work.

print(tilemap.get_bounds("/map#tiles"))

Instance /map#tiles not found

I also tried adding root:

print(tilemap.get_bounds("/root/map#tiles"))

Instance /root/map#tiles not found

So I tried a relative path and it worked.

print(tilemap.get_bounds("map#tiles"))

Because this is loaded via a factory, I cannot use /map#tiles. In a collection not loaded through a factory, the URL would normally work and I wouldn’t need to use a relative URL. However, since it’s going through a factory the path is different and I am not sure what the absolute URL is.

Try something like:

local tiles = msg.url(map, nil, "tiles")
1 Like

Thank you so much!, this let me find its absolute location main:/collection0/map#tiles. I also noticed that the collision data gives me ["other_id"] = hash: [/collection0/map] so that should have been another flag on where its was at. However, looks like Defold auto-increments the collection name as well, because if I load a new collection and then load back the previous collection, it now becomes /collection2/map. So I tried something like this, which obviously does not work:

tilemap.get_tile("main:" .. hash_to_hex(message.other_id) .. "#tiles"

It just turned more and more into a bad idea. As it doesn’t look like you can unhash, or append to a hash. So I am just going to go back to the relative URL and sending a message to the other object, moving the logic directly into the collection where I am trying to access its data.

msg.post(message.other_id, "collision_tile", { 
	tile_id = tile_id 
})

Sorry, I checked the syntax, and I believe this is the correct one:

local tiles = msg.url(nil, map, "tiles")

I have this code in my game to get a sprite:

local bg = factory.create("factories#bg_factory")
local sprite = msg.url(nil,bg,"sprite")
2 Likes