I’m coming from Unity and I have been working on games with it for years now. Recently, I decided to start investing in Defold (learning and making it my main weapon of choice). I get that we use the Messaging System (msg.post()) to communicate with other objects but I just wanna know why getting id or getting parent of other objects don’t work in my case.
So go.get_id() can have a parameter of the path, right? Here’s the Outline setup:
local aStarId = go.get_id("/aRoot/aStar")
local aRoot = go.get_parent(aStarId)
go.animate(aRoot, "euler.z", go.PLAYBACK_LOOP_PINGPONG, 90, go.EASING_INOUTSINE, 1, 0)
line 2 fails: ERROR:SCRIPT: test/bStar.script:25: Instance (null) not found stack traceback: [C]:-1: in function get_parent test/bStar.script:25: in function <test/bStar.script:16>
I also tried these but didn’t work:
local aStarId = go.get_id("test:/aRoot/aStar") -- test.collection
local aStarId = go.get_id("../aRoot/aStar")
local aStarId = go.get_id("/test/aRoot/aStar")
According to Defold docs, here’s the sample usage of go.get_id():
local id = go.get_id() -- no path, defaults to the instance containing the calling script
print(id) --> hash: [/my_sub_collection/my_instance]
local id = go.get_id("/my_sub_collection/my_instance") -- absolute path
print(id) --> hash: [/my_sub_collection/my_instance]
local id = go.get_id("my_instance") -- relative path
print(id) --> hash: [/my_sub_collection/my_instance]
Please enlighten me how things work here in Defold. Actually, this is my 2nd attempt learning this game engine. This time, I wanna push through but I wanna know the ins and outs of the basic stuff first so I don’t just guess what will work and what won’t when I’m actually doing commercial titles.
Getting the url of GO with Defold may be tricky. The first mistake is, using /aRoot/aStar. If you want to use the aStar, simply use its name. Defold does not use the parent/child relation other engines use.
Also, there is a simple trick you need to use when getting the url of GO.
if you use:
local aStarId = go.get_id("/aStar")
this will look a GO named aStar on the main collection.
To find the GO on the same collection, simply delete the first “/” charcter.
local aStarId = go.get_id("aStar") --> This will look for a GO under the same collection
aStarId is null. I know that I don’t have to use aRoot/aStar in this case but I wanna know why it ain’t working. I’m not yet sure if there will be a need for me in the future hence I wanna understand the “why won’t it work?”.
With Defold, you cannot have GO with the same name (in the same collection?) so you should be able to access any GO in the same collection using the only Go’s name and ignore parents. If you still want to use the parent’s id, you probably would need something like (assuming your collection is named: Stars)
local aStarId = go.get_id("main:/Stars/aRoot/aStar")
But this approach may fail if:
Your current collection is not named main or you have loaded another collection in the main with collection proxy
Or you have multiple Stars collections so some of them are named Stars2, Stars3 etc. Then, this approach may fail and you would need a more dynamic approach