0 votes

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 getnodes and getparents and getroots/gettrees 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! :-) I mean, should be easy right. :-) (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....

asked Dec 8, 2016 in Engine by Robster (808 points)

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.

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...

--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.

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

1 Answer

+1 vote
Best answer

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()
answered Dec 8, 2016 by puppetmaster- (673 points)
selected Dec 8, 2016 by Robster

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. :-)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.