for some context i’m trying to make a very simple system that adds a wand on screen when 1 is pressed and removes it if it’s pressed again
the way i’ve worked with that until now is that i’ve made a wand scene that i remove onready
then if i press 1 and some conditions are met the wand (and thus the node) will be added via add_child in this little bit of code
func get_wand_in_out():
var wand = wand_scene.instantiate()
if Input.is_action_pressed("First_wand") and wand_out == false:
await get_tree().create_timer(0.2).timeout
print("ok")
add_child(wand)
wand_out = true
and that works
HOWEVER
when i try to remove it with this piece of code
var wand = null # Define wand variable
func get_wand_in_out():
if Input.is_action_just_pressed("First_wand"):
await get_tree().create_timer(0.2).timeout # Wait 0.2 seconds
if is_instance_valid(wand): # If wand is a valid instance (of wand_scene)
wand.queue_free() # Remove the instance
wand_out = false # You probably don't need this
else: # If wand is not an instance of wand_scene
wand = wand_scene.instantiate() # Make it so
add_child(wand) # And add it as a child to this node
wand_out = true # You probably don't need this
Use Input.is_action_just_pressed instead of Input.is_action_pressed. The latter tells you whether the button is currently held down, which means it’ll keep returning true until the user lets go of the button. Even a quick button press will often register over several frames.