So, I need some help on adding a firing rate on my projectiles in my game. Here is my code for firing bullets

func shoot():
	if charge < 4:
		var bullet_uncharged = load("res://Scenes/Bullets/Bullet_Uncharged.tscn")
		var bullet = bullet_uncharged.instantiate()
		bullet.position = get_global_position()
		get_parent().add_child(bullet)
	else:
		var bullet_charged = load("res://Scenes/Bullets/bullet_charged.tscn")
		var chargedBullet = bullet_charged.instantiate()
		chargedBullet.position = get_global_position()
		get_parent().add_child(chargedBullet)

func _physics_process(delta):
	if Input.is_action_pressed("attack") && charge < 4:
		charge = charge + 0.1
	if Input.is_action_just_released("attack"):
		shoot()
		charge = 0
  • DaveTheCoder and SnapCracklins replied to this.
  • Sabir what I would do is set an export variable for frames, as in how many frames you want to pass before you can fire a bullet, along with a bool that determines whether or not you can fire. The bool flips to false when you fire and that resets your total for the frames. Then when you fire, bool flips off and frames start "draining".

    You could also use a timer and have the bool flip every timeout, and start the timer after you shoot. EITHER WAY, only allow a shoot function whenever the bool is flipped.

    var _canFire = true ## if off, can't shoot
    export var _fireFramesMax = 50 ## a base value for the timer to fill
    export var _fireRate = 1 ## the "rate" the frames drain (if not using timer)
    var _fireFramesCurrent = _fireFramesMax ## frames until next shot (only if not using timer)
    
    func _process(_delta):
           if _canFire:
                _shoot( )
           else:
                 _fireFramesCurrent - _fireRate
                 if _fireFramesCurrent <= 0:
                     _canFire = true
                     _fireFramesCurrent = _fireFramesMax

    OR the easier way with a timer and you don't need to track current time frames.
    add $Timer.start(_fireFramesMax) to your shoot function along with _canFire = false when you shoot.
    Then connect the Timer signal and do a similar process logic function, though as you can see, this is easier.

    func _process(_delta):
           if _canFire:
                _shoot( )
    
    
    func _on_Timer_timeout(_):
            _canFire = true

    Sabir what I would do is set an export variable for frames, as in how many frames you want to pass before you can fire a bullet, along with a bool that determines whether or not you can fire. The bool flips to false when you fire and that resets your total for the frames. Then when you fire, bool flips off and frames start "draining".

    You could also use a timer and have the bool flip every timeout, and start the timer after you shoot. EITHER WAY, only allow a shoot function whenever the bool is flipped.

    var _canFire = true ## if off, can't shoot
    export var _fireFramesMax = 50 ## a base value for the timer to fill
    export var _fireRate = 1 ## the "rate" the frames drain (if not using timer)
    var _fireFramesCurrent = _fireFramesMax ## frames until next shot (only if not using timer)
    
    func _process(_delta):
           if _canFire:
                _shoot( )
           else:
                 _fireFramesCurrent - _fireRate
                 if _fireFramesCurrent <= 0:
                     _canFire = true
                     _fireFramesCurrent = _fireFramesMax

    OR the easier way with a timer and you don't need to track current time frames.
    add $Timer.start(_fireFramesMax) to your shoot function along with _canFire = false when you shoot.
    Then connect the Timer signal and do a similar process logic function, though as you can see, this is easier.

    func _process(_delta):
           if _canFire:
                _shoot( )
    
    
    func _on_Timer_timeout(_):
            _canFire = true

    Thank you for your help! This was really useful.