Michael Paul answer is correct to the original post. right.
but I pointed it could be unsafe to search by node name as like what Michael Paul said.
the risk of attempting to name a scene with a duplicate name
it will be named automatically as unique name when try to set same name under same parent node, like @snake_1@2
or something.
this can happen when you remove node by queue_free()
and make another one after that.
func _ready():
get_node("Button").connect("pressed", self, "on_pressed")
func on_pressed():
if has_node("my sprite"):
get_node("my sprite").queue_free()
var sp = Sprite.new()
sp.set_name("my sprite")
add_child(sp)
you can see that "my sprite" is in scene at first button click on Debugger > Live Scene Tree tab
but after second click, you can see "@my sprite@2", not "my sprite"
because first one "my sprite" is not gone yet.
I just want to point it.
you should make sure and be careful to get a reference by node name.
of course you can name it "my sprite" correctly if you use free
instead of queue_free
, but need to be careful to use free
also.