Does _ready() get called again if you remove a node and instantiate it again?

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

So for my game i’ve got one main scene where i then instantiate the other scenes to it (like the main menu and all the levels) and the player scene is instantiated in the other scenes (not through code) that get added to the main scene. Would the _ready() function on the players script get called when i swap scenes (levels)? To clarify, if i remove the current levels scene and add another one that also has the player scene in it would the players _ready() function get called again?

I believe the _ready function would be called again, yes. However, if I understand your situation correctly, it’s important to realize that although the _ready function in the player’s script will be called multiple times, from a player instance point of view, individual player instances’ _ready function will only be called once, since you are creating multiple player instances assuming each stage defines it’s own player.

I believe that moving (via code) the player from one level to the next would not trigger the _ready function to be called twice, but I may be wrong on this point.

godot_dev_ | 2022-08-05 20:37

A tip, for future - for this and other similar questions.

Just throw a print statement in ready()_ and try it. Do you see your message output each time you load a new scene? Or, similarly, set a debug breakpoint in _ready(). Does it get hit each time you load a new scene?

jgodfrey | 2022-08-05 21:31

:bust_in_silhouette: Reply From: Inces

Yes, every ready will be called when new scene is is instantiated, along with its children. Take a note, that despite every level having a childed player node, these are not the same player nodes. They are different instances of one scene, they have different IDs. And this is pretty much why their ready is called.
Because when a node with certain ID gets removed from the tree, the next time it is childed, its ready is not called ! It will then require request_ready to call ready once more.