How to store a reference to a add_Child() object? The function returns void.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By The_Black_Chess_King

We can add nodes by doing add_child(node), however how we can store a reference for later use? My expectation was that this was going to work:

Julliet = add_child(Julliet_Template)

However in the editor it says:

Assignment operation, but the function 'add_child()' returns void.

I have researched the topic but nobody is talking about this, this will be very useful for a lot of reasons. Where is the blackmagic? I can’t find it. o.o

:bust_in_silhouette: Reply From: jgodfrey

The node you add is your reference. In your example, Julliet_Template is a reference to the node you added. I’m not sure what else you’re looking for there…

I was expecting a unique RID from add_child, kind like a unique copy. Because we can add multiple instances using add_child as far as I know. But I can still do this using meta or some sort of reference injection through the parent if say, a child has X var or value. But I see what you mean now. Thanks @Jgodfrey!

Just for details why I thought like this, is because I am used to create some nodes like this:

onready var TWEEN =

func _ready():

    print(name," : feedback 1")

The_Black_Chess_King | 2020-11-26 16:51

Because we can add multiple instances using add_child as far as I know.

No, that’s not true. A node instance can only exist in one place in the scene tree, and therefore can only have a single parent. If you attempt to parent the same node instance to multiple nodes (via add_child) the second one (and beyond) will fail as the instance already has a parent.

You can create multiple instances of the same scene, and add each of them to the scene tree in any location you want. But, each single instance can only have a single parent. And, each instance really is the reference you’re after…

jgodfrey | 2020-11-26 17:00

Oh I see now, never thought of instances like that. I guess it’s my fault for not going far down the rabbit hole enough :smiley: thanks again, you always bring clear answers! +1

The_Black_Chess_King | 2020-11-26 17:04