Timer won't start

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

This is my script for a projectile. After the second timer (the one that doen’t work) the player should have his speed back (the projectile debuffs for a short time). The script:

    extends Area2D
const SPEED = 4
var velocity = Vector2()
var direction = 1
var impact = false

func _ready():
	pass
	
func set_fireball_direction(dir):
	if impact == false:
		direction = dir
		if dir == -1:
			$AnimatedSprite.flip_h = true

func _physics_process(delta):
	if impact == false:
		velocity.x = SPEED + delta * direction
		translate(velocity * direction)
		$AnimatedSprite.play("shoot")



func _on_VisibilityNotifier2D_screen_exited(): #quando esce dallo schermo viene cancellata
	queue_free()



func _on_PlantAttack_body_entered(body):
	impact = true
	$AnimatedSprite.play("impact")
	if "Enemy" in body.name:
		body.dead()
	$Timer.start()
	if "Player" in body.name:
		body.ACCELERATION = 10
		$Timer2.start() #Timer doesn't start
		body.dead()



func _on_AnimatedSprite_animation_finished():
	impact = false


func _on_Timer_timeout():
	queue_free()


func _on_Timer2_timeout(): 
	get_parent().get_node("Player").ACCELERATION = 50 #This line doesn't work
	print("ciao")
:bust_in_silhouette: Reply From: Kanor

I think you’re destroying the timer before it can complete.

Timer2 is a child of bullet, but in _on_Timer_timeout for the first timer, you call queue_free(). This destroys the bullet AND its children. This means timer2 is freed before it can timeout.

You can fix this by changing Timer2 to be a child of the player, which would make resetting acceleration easier:

bullet:

if "Player" in body.name:
    body.ACCELERATION = 10
    body.get_node("Timer2").start()
    body.dead()

the signal and timeout function would then be moved to the player:

func _on_Timer2_timeout(): 
    ACCELERATION = 50 #Timer now finishes as a child of player
    print("ciao")