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")
  • xyz replied to this.

    xyz could you please explain how do i use delta for this?

    • xyz replied to this.

      JetteBoyTV I could, but let's first determine what you actually want to do. It's still not clear. Why do you insist on measuring time in frames with some arbitrary fps, when you can simply measure real time. If you want to fire a weapon at regular intervals, define that interval in seconds and connect a timer to call a trigger function. If the interval is extremely short (near or below project's frame interval) then instead of a timer, you'll need to use frame's delta time so you can properly handle time reminders or multiple intervals that might have happened within a single frame interval.

        xyz i insist on using frames because the animation is animated in the frame rate, and using real time has worked like complete shit for me because it isnt perfectly synced with the animation causing it to switch between the firing and idle animations while holding down the fire button (plus once i add sounds it would probably also sound horrible since its not synced)

        • xyz replied to this.

          JetteBoyTV In that case I already suggested to use animated sprite's signals (in your previous question). However you still haven't clearly explained what needs to be synched with what.

          With fire rates, it's customary to have it defined in real time, and let the animation be triggered by that realtime rate, instead of vice versa, which is what you're seemingly insist on doing.

            xyz the fire rate needs to be synced with the animation, and the animation is a minigun spinning, so if its not synced, it looks like the minigun suddenly stopped spinning, also in the animation it looks like the minigun fires two times in the two firing frames (each frame takes two 35fps frames)

            • xyz replied to this.

              JetteBoyTV It's not a matter of syncing per se, but who/what drives that syncing. For the third time; if you insist on triggering something by the animated sprite's animation - use animated sprite's signals. Don't measure time separately.

              But I'd handle this by defining the fire rate in realtime, and then calculate the spinning animation fps to match the firing rate, not the other way around.

              Both approaches can work but you actually need to sync them, not handle their timings with two separate time keeping mechanisms.

                xyz with the sprite signal thing, what do i do if the animation has the gun refining two times in the animation?

                • xyz replied to this.

                  xyz cool, how do i use the sprite signals to tell it when to fire?

                  • xyz replied to this.

                    JetteBoyTV Use frame_changed signal. In the signal handler check the current frame and fire if you're supposed to fire on that frame.