Help Coding Rapid Fire

Godot v4.3

So I’m making a side scrolling shoot 'em up. I was able to make code for firing that works really well, but the problem is, it only fires each time the button is pressed. As a result, one would have to rapidly press the fire button to shoot lots of times in a row. I’m wondering what would be the easiest way to allow the player to hold the fire button to fire a continuous stream of projectiles.

The code I have for shooting is as follows, with this simply being the basic attack without any weapon upgrades:

	if Input.is_action_just_pressed("action_shoot") and akane_upgrade == 0:
		var new_proj1 = proj1.instantiate()
		new_proj1.global_position = $Main_Gun.global_position
		add_sibling(new_proj1)

Any suggestions for the best way to do so?

In _process(delta) function:
if Input.is_action_pressed(“fire”):

Connect “Input.is_action_pressed” signal and place in _process. It should execute each tick allowing continuous fire as long as the action button of choice is being pressed. Just plop your firing code below the is action pressed signal.

2 Likes

one solution is to use a timer

var gun_is_cooked :bool = false
if Input.is_action_just_pressed("action_shoot") and akane_upgrade == 0:
		$Timer.start()
		while !gun_is_cooked:
				# stuff

func _on_timer_finished():
		gun_is_cooked = true

Yeah a timer sounds good, but don’t use a while loop.

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action("action_shoot"):
		if event.is_pressed():
			$Timer.start()
			# uncomment below to also shoot every press
			#_on_timer_timeout()
		elif event.is_released():
			$Timer.stop()


func _on_timer_timeout() -> void:
	# your shoot code
	var new_proj1 = proj1.instantiate()
	new_proj1.global_position = $Main_Gun.global_position
	add_sibling(new_proj1)
3 Likes

So I tried your idea, and it works. However, is there a way to space out how frequently it’s fired with your method? Because right now it fires as a continuous stream.

I would imagine a timer in a global var or other, could set a timer to fire off and only fire every x tick instead of every tick.

2 Likes

So, by combining your method with @TheAncientOne 's method, I was able to get it working! Tysm, everyone!

Glad to hear! sorry for late reply, I had to get some zzz’s :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.