- Edited
tried to use a timer, didnt work out for me, How to i set a firing cooldown that uses frames? ( also im using 35 fps )
also heres my current code using the timer:
extends CanvasLayer
#var time_since_last_shot = 0.0
#var fire_rate = 1.0
@export var fireDelay: float# = 0.5
@onready var FireDelayTimer := $FireDelayTimer
# Called when the node enters the scene tree for the first time.
func _ready():
$AnimatedSprite2D.animation_finished.connect(_on_AnimatedSprite2d_animation_finished)
$AnimatedSprite2D.play(Global.current_weapon + "_Idle")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
#time_since_last_shot += delta
#var can_shoot = time_since_last_shot >= (1.0 / fire_rate)
# Assuming some action needs to happen every 1/35th of a second
if Global.current_weapon != "Knife" and Global.ammo <= 0:
Global.current_weapon = "Knife"
$AnimatedSprite2D.play("knife_idle")
if Input.is_action_pressed("main_fire_weapon") and FireDelayTimer.is_stopped():# and can_shoot:
FireDelayTimer.start(fireDelay)
if Global.current_weapon == "Knife":
$AnimatedSprite2D.play("Knife_Stab")
else:
print(FireDelayTimer.time_left)
$AnimatedSprite2D.play(Global.current_weapon + "_Fire")
#time_since_last_shot = 0.0
if Global.current_weapon != "Knife":
if Global.ammo > 0:
Global.ammo -= 1
# one 35 fps frame =
match Global.current_weapon:
"Gun":
fireDelay = 0.028570
"MG":
fireDelay = 0.028570
"Chain":
fireDelay = 0.028570
"Knife":
fireDelay = 0.028570
_:
fireDelay = 0.028570
func _on_AnimatedSprite2d_animation_finished():
$AnimatedSprite2D.play(Global.current_weapon + "_Idle")