Code runs 3 times

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

I’ve made a script for guns as you can see here. Code 1:

func _physics_process(delta: float) -> void:
	if $Timer.is_stopped():
	if Input.is_action_pressed("shot") && get_parent().name == "Player" && PlayerData.ammo > 0 :		
		if flip :
			anims.play("shot1")
		else:
			anims.play("shot")
		PlayerData.ammo -= 1		
		$Timer.start()
	if Input.is_action_pressed("shot1") && get_parent().name == "Player1" && PlayerData.ammo1 > 0:
		if flip :
			anims.play("shot1")
		else:
			anims.play("shot")
			PlayerData.ammo1 -= 1		
		$Timer.start()`

Code 2:

func _on_AnimationPlayer_animation_started(anim_name: String) -> void:
prticle.emitting = true
print_debug("shot")
var fireball = bullet.instance()
fireball.dmg = dmg
if flip :
	fireball.dir = -1
	prticle.rotation_degrees = 90
	prticle.position.x = -38.016
else:
	fireball.dir = 1	
	prticle.rotation_degrees = -90	
	prticle.position.x = 38
	
fireball.global_position = self.position
fireball.collision_layer = layer
fireball.collision_mask = mask
get_parent().add_child(fireball)

Sorry for my messy code anyways this code runs 3 times as my console print it that way (dmg is multiplied by 3 and ammo is -3 not -1) I’ve also tried to add code 2 to _physics_process and _on_Timer_timeout function but with same result, any ideas why?

Try putting the input deteciton inside the _input function as it will function more fluently rather than detection it inside the physics process.

also, for better debugging check what every value gives you each time code1 runs using the printt function(yes printt and not print as it gives you everything in a single line seperated by \t which will wield better results).

i believe it will be somewhere with the timer but it might be somewhere else

rustyStriker | 2020-01-02 23:36

:bust_in_silhouette: Reply From: aa
bool is_action_pressed ( String action ) const

Returns true if you are pressing the action event.

vs.

bool is_action_just_pressed ( String action ) const

Returns true when the user starts pressing the action event, meaning it’s true only on the frame that the user pressed down the button.This is useful for code that needs to run only once when an action is pressed, instead of every frame while it’s pressed.

From:

:bust_in_silhouette: Reply From: pepeDEV

Thanks guys fixed it…