How do I make my character shoot 2 guns in an alternating pattern?

Godot Version

4.2.1

Question

I want my character to shoot 2 guns but when one isn’t firing the other one is but I can’t work out how to signal my second guns script to do that. The script for both guns is very similar and the part here is currently identical betwwen the two so they just both fire simultaniously. The bit i put in a bunch of speech marks is where I was trying to use a signal.

	if Input.is_action_pressed("fire") and can_fire:
		var bullet_instance = bullet.instantiate()
		bullet_instance.rotation = rotation
		bullet_instance.global_position = $Marker2D.global_position
		get_parent().add_child(bullet_instance)
		can_fire = false
		""""""emit_signal("alternate")"""""""
		await get_tree().create_timer(0.4).timeout
		can_fire = true

I see no check on what gun is expected to shoot. I e. What is alternating signal doing?

Do you want a constante rate of alternating fire or, can the guns have their own cool down?
Independent: Pop pop… Pop pop, pop…pop.pop…pop pop

Constant: pop…pop…pop…pop

constant

Hi there! :slight_smile:
Here is how I did it for myself.
You are not far.

I have one bullet scene (bullet_scene) that I instantiate into my player script, and my player has 2 different bullet spawners (Marker2D) as child of my player node declared like this at the top of the script:

@onready var spawn_bullet_1 = $SpawnBullet1
@onready var spawn_bullet_2 = $SpawnBullet2

In your shooting script, you will need to call two different instances of the same bullet. So “bullet_1”, and “bullet_2”. “spawn_bullet_1” will be used for "bullet_1, and “spawn_bullet_2” will be used for "bullet_2.

The shooting speed is set by a “Timer” node, for which you will define the time in the inspector and then slide into your shooting script.

Your “await” timer at the end of the first instance will separate the two instantiations by the same time set in your shooting Timer node.
If you “await” timer is 0.4 sec, the shooting Timer node need to be set at 0.4 sec too. (It can be cool if they mismatch, but one of the instantiated bullet will likely fire after you are finished firing.)

Here is the script:

func shoot():

	# Intantiate your first bullet
	var bullet_1 = bullet_scene.instantiate()
	bullet_1.position = spawn_bullet_1.global_position
	get_tree().current_scene.add_child(bullet_1)

	$ShootSpeed.start() # That is your shooting Timer node right there.
	canshoot = false
	
	await get_tree().create_timer(0.4).timeout
	
	# Intantiate your second bullet
	var bullet_2 = bullet_scene.instantiate()
	bullet_2.position = spawn_bullet_2.global_position
	get_tree().current_scene.add_child(bullet_2)

	$ShootSpeed.start()
	canshoot = false

I hope it makes sense.

Cheers!