Finding the parent of a clicked instance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

I hope I can describe this well.

The setup:

I have a scene called gameLevel
I have another scene called iceBlock
I have instanced several iceBlock nodes into the gameLevel scene

The iceBlock scene looks like this:

iceBlock
| - iceBlockAnimationPlayer
| - TextureButton
– ¦ - iceBlockSprite

The gameLevel scene, after instancing the iceBlock scene looks like this:

gameLevel
| - iceBlocksPositionNode
–¦ - iceBlockInstance (named “1”)
–¦ - iceBlockInstance (named “2”)
–¦ - iceBlockInstance (named “3”)
–¦ - etc

So essentially I’ve brought in the iceBlock scene, placed it under a position node just for grouping and then renamed them all 1, 2, 3, etc.

Now, the fun part, and the question:

Back in the original iceBlock scene.
I have a signal setup for the textureButton release that runs a function.

Now the trick. How can I get the name of the instance root? For example, looking above I’m looking for the name of iceBlockInstance (named "1") if I click on #1. iceBlockInstance (named "2") if I click on #2. Etc.

I’ve tried so many variations of get_nodes and get_parents and get_roots/get_trees etc etc that my head is spinning. Obviously I’m not fully grasping the concept of a child referencing itself from when it’s instanced and then calling its parent! :slight_smile: I mean, should be easy right. :slight_smile: (I jest at my own logical ceiling)

Remember. The function is running from within the original iceBlock code. It’s then instanced into the other scene and run.

I hope that makes sense and I hope someone can nudge me in the right direction. You’ve all been very helpful and I do appreciate it.

Thank you…

I fail to see where you are having trouble.
If the code is stored in a function in a script attached to the root node of the iceBlock scene then all you need to do is call get_name(), since when you instantiate the ice block in the game scene and rename it it’s actually a copy of the root node of the ice block scene that gets renamed.

Warlaan | 2016-12-08 11:22

And if a child has the script, just do get_parent().get_name()

But you don’t have to get the “name” to make a signal connection, just get the node (be it self or parent).


ps:Also, there is the node name and the node path…

eons | 2016-12-08 12:29

–I fail to see where you are having trouble.

Beginner. That’s the trouble I’m having. Just don’t know enough yet and trying to get my head around it all.

– If the code is stored in a function in a script attached to the root node of the iceBlock scene then all you need to do is call get_name(), since when you instantiate the ice block in the game scene and rename it it’s actually a copy of the root node of the ice block scene that gets renamed.

That’s exactly what I didn’t realise. It makes sense now but I didn’t imagine that the instance would change its name. It’s starting to come together for me. Posts like yours are really helpful. Thank you.

Robster | 2016-12-08 22:55

The instance will have a unique reference ID (RID) but the name on the tree can change because need to be unique between siblings (you can change the name if you want).

In most cases you will work with a Node variable or the NodePath, or go blind getting children and filtering by type or group (or calling groups directly).

eons | 2016-12-09 01:08

:bust_in_silhouette: Reply From: puppetmaster-

Put your script on the top node (iceBlock)

iceBlock (script hear)
| - iceBlockAnimationPlayer
| - TextureButton
– ¦ - iceBlockSprite

get_node("TextureButton").connect("pressed",self,"my_function")

func my_function():
 print(get_parent().get_name()) #you get name off iceBlocksPositionNode
 print(get_name()) #you get name off iceBlockInstance

Try to work with groups is easyer then with names if possible.

get_tree().call_group(0, "iceBlock", "melt")

or

get_tree().call_group(0, "player", "freeze")

or

for water in get_tree().get_nodes_in_group("water"):
 water.freeze()

Thank you for the solution as well as the alternatives and the further examples. That’s really appreciated.

I had no idea that the root node of the iceBlock scene would rename when I instanced it in another scene, then renamed the instance. Of course it makes perfect sense now. Hence get_name() working.

Thanks again. :slight_smile:

Robster | 2016-12-08 22:53