0 votes

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():

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)

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

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

func _on_AnimatedSprite_animation_finished():
    impact = false

func _on_Timer_timeout():

func _on_Timer2_timeout(): 
    get_parent().get_node("Player").ACCELERATION = 50 #This line doesn't work
asked May 21 in Engine by MightyRat (55 points)

1 Answer

+1 vote

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:


if "Player" in body.name:
    body.ACCELERATION = 10

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
answered May 21 by Kanor (50 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.