Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | lincolnpepper |
I am making a space shooter game, and i want it so that you can press the button to fire any time (as long as the amount of bullets on screen are less than or equal to 2) but if you hold the button down it will fire the bullets at a steady pace determined by a fire cooldown timer ‘HoldFireTimer’, but i don’t want you pressing the button to fire to be limited by the timer. I have this code for it and it works, but there is no way that this is the best way to do it. Does anyone know a cleaner more efficient way of doing it? This is honestly annoying me with all its messiness and copy+paste.
func _input(event):
if event.is_action_pressed('fire') and get_node("Muzzle/Bullets").get_child_count() < 2:
var bullet = preload("res://Scenes/Bullets&Projectiles/Player Bullet.tscn")
var b = bullet.instance()
get_node("Muzzle/Bullets").add_child(b)
b.set_position(position + $Muzzle.position)
$HoldFireTimer.start()
func _on_HoldFireTimer_timeout():
if Input.is_action_pressed('fire') and get_node("Muzzle/Bullets").get_child_count() < 2:
var bullet = preload("res://Scenes/Bullets&Projectiles/Player Bullet.tscn")
var b = bullet.instance()
get_node("Muzzle/Bullets").add_child(b)
b.set_position(position + $Muzzle.position)
$HoldFireTimer.start()