How to replace one node with another?

Godot Version



I have this hierarchy in my project. How can I make the Barrack_1LL node be replaced by another node when the timer expires?

just remove child, queue freed, then readd child?


I’m new to learning Godot, can you tell me which script to write this in? And the problem is that the timer expiration signal is connected to the BarrackUI node.

what’s the new node you wanted to add, is it the same Barrack_1LL?

there are many ways to do it, if you just want the Barrack_1LL to be added on Barracks node

since you have connected the timeout signal from the timer to BarrackUI script
then either this BarrackUI script will need to reference Barracks node or just send a custom signal telling this Barracks script node to create new Barrack_1LL

As for replacing with another one, you will need to:
in Barracks script:




after removing the existing Barrack_1LL
you will need to add new one
i believe your Barracks Script has the resource load path to the Barracks_1LL scene already, then you just

var new_barrack=the_Barracks_1LL_scene.instantiate()

to make new Barracks_1LL


You could also use the replace_by function of nodes: Node — Godot Engine (stable) documentation in English

I try it, but it gives me error. Maybe i do something wrong


instantiate() , not instanciate()

omg, my brain has been melting lately

Yes, it works, but only if I call this function inside the Barracks script. But if I call it in the BarrackUI script, the model does not change.

can be either 2 things:

  1. the function model_upgrade_to_2ll actually not being accessed
  2. your barrack_2ll is still barrack_1ll scene, so nothing’s changed

Could this be a problem? Something to do with the fact that the function is not called for this parent.

I believe that you get this message when you try to call remove_child(node) where the specified node is not a child of the current node.

That could be because the timer is running out again and calling the method again. You might need to set the timer to one shot. or call $Timer.stop() in the _on_timer_timeout() function.

As to why it’s not changing, it’s hard to tell without more context. If you comment out the part where you add the new model, does the old model disappear? If you put a print("I am upgrading the model") in the model_upgrade_to_2ll() function do you see the message?

Yes, i see this message

Okay, I just created this function in BarrackUI and pre-loaded the necessary scenes there, it’s not exactly what I wanted, but apparently with such a hierarchy of nodes I could only do it this way. I think this is the solution. Thank you all for your help.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.