Getting an object's parent from id?

Is there a way to get the id of an object’s parent from that child object’s id alone? I have a collection hierarchy where I’d like to get the parent of an object, and the object is only identifiable by message.other_id in a ‘trigger_response’ message.

1 Like

Alright, after an hour or so of searching, here’s what I’ve put together:

string.sub(tostring(msg.url(message.other_id).path), 8, -2)

This little hack gets a string from a hashed id. From this string, I should be able to… nope, never mind. When I try this:

msg.post(string.sub(tostring(msg.url(message.other_id).path), 8, -2) .. "/..", "armed_hit", {})

I get an error saying that “Instance ‘/player/item/…’ could not be found”, suggesting that the internal url resolver doesn’t support this type of filesystem-style addressing.

I’m going to mess around with Lua string manipulation and try to see if I can mimic the effect of “…” by cutting off the end of the string. This has to be able to be done programmatically, since some enemies will use this system to wield weapons like the player, so I can’t just message “/player”. Does anyone know of a less painful way to find a parent address than what I’ve done here?

Also, my bad, I didn’t realize that ‘trigger_response’ messages get sent to the trigger collider as well. I can use that to make this a lot simpler by not having to relay information through the enemy. Maybe I’m getting confused because Unity doesn’t alert trigger objects in its internal physics to my knowledge, and I used that before Defold. However, it would still be nice to know if there’s some way to do this, it’s frustrating that it’s this tricky to get an object’s parent from its script.

1 Like

Two things here.

First, the parent-child relation is only a scene graph relation. If you need to track some other relation you need to do that in code. There are many options, depending on your use-case:

  1. Send a message from child to parent and store the id in a table.
  2. Send message from parent to child and store.
  3. Send message to some manager that stores and tracks relation.
  4. Store relation in shared data.

Second, the string representation of hashes is a debug feature that only exist in debug builds. If you rely on it your code will break when you do a release build.

2 Likes

Oh, ok. As far as the parent-child relation, the url for the ‘item’ is “/player/item” (‘player’ in that case is just the collection name, it’s a collection included in main.collection not created runtime in this case) so there isn’t that url-based parent-child relation. Good to know that the string representation of hashes is only a debug thing, that makes sense because it’s probably a decent performance hit.

Since I was able to use the ‘trigger_response’ message hitting “/player/item”, I ended up doing what sounds like option 2. The player sends a message to “item” informing it of its id, and then the item object takes “trigger_response” messages and forwards them back to the player through the id.

Oh wait, duh.

Thanks to my decision to name the collection with the same name as the collection’s “player” object, I confused myself. I can just send messages to “player” from the item.collection script.

Thanks for the help, I knew there was some discrepancy between the scene transformations and url hierarchy but I thought that there was one level of parent-child relations on objects due to some confusion, I think I’ve got it now!

1 Like

Cool, you can check out http://www.defold.com/manuals/message-passing/ which explains all of this in detail.

1 Like

Yup, I’ve looked at that a couple times so far, almost got the hang of things

1 Like