Enemy Shoot Timer wont work

Godot Version

Godot 4.2

Question

My Timer wont work at all, even if I use get_tree().create_timer() or I create a timer node… none works… basically I am making an enemy who shoots a bullet every 3 seconds… doesn’t work for some reason…

here’s my code for the enemy

extends Area2D

@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var timer = $Timer
@onready var attack_zone = $AttackZone
@onready var Bullet = preload(“res://big bomb.tscn”)
@onready var isAttacking:bool = false

func _process(delta):
attack()
handleAnimations()
await get_tree().create_timer(1).timeout

func attack():
isAttacking = true
var b = Bullet.instantiate()
add_child(b)
b.transform = attack_zone.transform
isAttacking = false

func handleAnimations():
if isAttacking:
animated_sprite_2d.play(“attack”)
else:
animated_sprite_2d.play(“default”)

Hi!

This is not the way to do this…
You are doing the attack every frame and it is impossible.

You have a timer that do nothing, but you have to use it for this.
Put the timer Autostart = True for the first time and Loop = false, then you have to bind the timeout signal in a function to attack

Example (based in your code):

func _physics_process(delta):
    handleAnimations()

func attack():
    isAttacking = true
    var b = Bullet.instantiate()
    add_child(b)
    b.transform = attack_zone.transform
    isAttacking = false

func handleAnimations():
    if isAttacking:
        animated_sprite_2d.play(“attack”)
    else:
        animated_sprite_2d.play(“default”)

func _on_timer_timeout():
    attack()
    $timer.start()

I hope this help you