0 votes

using GDSCRIPT (not C# etc)

I have root scene called "test level"
it has as children
Player
(a camera2d as a child of player)
HUD
COINS
(with coin1 coin2 etc. as a child of COINS)
Portal
(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 onGreenPortalbody_entered(body):
$SpongBob.position.x = 928
$SpongBob.position.y = -480

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

Godot version Godot 3.3.3 stable (or Godot 3.4.4 stable or Godot 3.5)
in Engine by (52 points)

1 Answer

0 votes
var hud2 = preload("path to HUD2")
func remove_hud():
    $HUD.queue_free()

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

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.

by (32 points)

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

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

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

the function in hud2 is oncoconut_collected():

what i am having trouble with is the pathtohud2_in _tree

can some help me figure it out

pathtohud2 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

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected]ne.org with your username.