GDSCIPT how to add and remove scene from another scene

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

using GDSCRIPT (not C# etc)

I have root scene called “test level”
it has as children
(a camera2d as a child of player)
(with coin1 coin2 etc. as a child of COINS)
(with green Portal and blue Portal as children of Portal)

The HUD displays the number of coins you have collected

when I use the green portal it takes you to another section of the screen
(i would like the green portal to delete HUD and load HUD2)

my code looks like this

func _on_Green_Portal_body_entered(body):
$SpongBob.position.x = 928
$SpongBob.position.y = -480

so in this code how do i delete HUD scene and add HUD2 scene

:bust_in_silhouette: Reply From: NachoCheese989
var hud2 = preload("path to HUD2")
func remove_hud():

func add_hud():
    h = hud2.instance()

queue_free() tells a node to remove itself from the tree.
preload the scene you want to add by setting a var with preload, and add one by instancing it then adding it as a child.

you can connect a signal to the added scene using the connect() method, with something like this:

$coin.connect("signal to connect", path_to_hud2_in_tree, "function in hud2 script")

make sure that the function you’re connecting to in the hud2 script has the right parameters

NachoCheese989 | 2022-09-13 11:13

i got most of it but i am having trouble with $coin.connect…
what is the path_in_tree

Richard_A_Odell | 2022-11-10 11:48

so fr i have $COINS/coin1.connect(“coin_collected”,

the function in hud2 is _on_coconut_collected():

what i am having trouble with is the path_to_hud2_in _tree

can some help me figure it out

Richard_A_Odell | 2022-11-10 12:08

path_to_hud2 is the path to the hud2 node in the tree that was instanced in. try making a new node in the editor and then calling $node.add_child(hud2_instance), then you can use $coin.connect("signal to connect", $node.get_child(0), "function to connect"). replace $node

NachoCheese989 | 2022-11-10 21:15