how to make spawn only one node by click

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bárbara Gabriela

I am trying to make a node appear on the side of the player and the player changes animation when pressing enter, but while it is in the second state it continues to spawn until pressing enter again. I just want it to appear once.

Here is the code:

#states variables
const slimezinho = preload(“res://scenes/slimes/slimezinho.tscn”)
onready var state = $player_sprite
var state_now = 1
#change state
if(Input.is_action_just_pressed(“enter”)):
state_now += 1

#change sprite
if(state_now == 1):
	state.play("slime")
elif(state_now == 2):
	state.play("slimezinho")
	var slime = slimezinho.instance()
	get_parent().add_child(slime)
	slime.set_position(get_node("spawn").get_global_position())
elif(state_now == 3):
	state.play("mini_slimezinhoinho")
elif(state_now > 3):
	get_tree().reload_current_scene()
:bust_in_silhouette: Reply From: johnygames

I guess the problem arises because you have not indented the second part of your code.
See, you have probably put this in the _process(delta) function thus generating a new slime instance every tick of the game, regardless of whether you press enter or not. The first time you press enter, the game reaches state = 2 and, since you repeat the same process every tick, a new slime is created. Indent the second part properly.