Laserbeam playing when it's not supposeed to

Godot Version

4.6.2

Question

i used a timer and when it’s off, it’s supposed to completely stop the laserbeam from appearing at all, but when the timer is false, it lets it play once and then not again. I don’t know what I did wrong, and can’t find anything that stands out in the code

laserbeam code:

class_name beamattack extends Node2D

@export var beamtimer : bool = false
@export var cooldowntimer : float = 3

@onready var animation_player: AnimationPlayer = $AnimationPlayer

func _ready() -> void:
	if beamtimer == true:
		attack_delay()
		
		
func attack() -> void:
	animation_player.play("attack")
	await animation_player.animation_finished
	animation_player.play("default")
	if beamtimer == true:
		attack_delay()

func attack_delay() -> void:
	await get_tree().create_timer(cooldowntimer).timeout
	attack()

I’m guessing that when you want to start the laser again, you set the beamtimer = true?
You are not checking anywhere for the value of beamtimer except for in the _ready() and right after the attack() is finished, so when you switch the value to true, it doesn’t do anything anymore.
You should call attack_delay() instead.

However, this whole setup could be improved if you get rid of all awaits and use proper signal handling instead.

1 Like

So you don´t want the laser beam to appear at all when beamtimer is false, but it does appear once. Am I understanding the problem correctly? If so, then the issue is that something external to your function is calling attack or attack_delay. So simply don’t do that then.

More serious problem: you have infinite recursion between attack and attack_delay, which will eventually blow your stack. I suggest a non-recursive rewrite. Something like this:

func attack_once() -> void:
	animation_player.play("attack")
	await animation_player.animation_finished
	animation_player.play("default")

func continual_attack() -> void
    while beamtimer:
        await attack_once()
        await get_tree().create_timer(cooldowntimer).timeout

1 Like

Yes that is the problem. I’ve looked through and can’t seem to find any instances but I’ll keep combing through it