'Already connected to callable' error

Godot Version

Godot 4

Question

Hi! I am in a bit of a fix in my Space Invaders clone (I am a beginner). I have made a timer to stop spamming the laser shoot button, where I disconnect and connect the signal of my laser being shot, but it is showing an error 'E 0:00:28:0368 main.gd:23 @ _on_timer_timeout(): Signal ‘laser_shot’ is already connected to given callable ‘Node2D(main.gd)::_on_player_laser_shot’ in that object.
<C++ Error> Method/function failed. Returning: ERR_INVALID_PARAMETER
<C++ Source> core/object/object.cpp:1402 @ connect()
main.gd:23 @ _on_timer_timeout()

The code is:
Main Code:
extends Node2D

const LASER_BEAM = preload(“res://Scenes/LaserBeam.tscn”)
@onready var player: CharacterBody2D = $Player
@onready var laser_container: Node2D = $Container
@onready var timer: Timer = $Timer

Called when the node enters the scene tree for the first time.

func _ready() → void:
player.laser_shot.connect(_on_player_laser_shot)

func _on_player_laser_shot(_LASER_BEAM, location):
var e = LASER_BEAM.instantiate()
e.global_position = location
laser_container.add_child(e)
timer.start()
player.laser_shot.disconnect(_on_player_laser_shot)

func _on_timer_timeout() → void:
player.laser_shot.connect(_on_player_laser_shot)

Player code:
extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var win_height
var p_height
const LASER_BEAM = preload(“res://Scenes/LaserBeam.tscn”)
@onready var marker_2d: Marker2D = $Marker2D

signal laser_shot(LASER_BEAM, location)

func _ready() → void:
win_height = get_viewport_rect().size.x
p_height = 64

func _process(_delta: float) → void:
if Input.is_action_just_pressed(“Laser”):
shoot()

func _physics_process(_delta: float) → void:
var direction := Input.get_axis(“Left”, “Right”)
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)

position.x = clamp(position.x, p_height-595, (win_height - p_height)/2)

move_and_slide()

func shoot():
laser_shot.emit(LASER_BEAM, marker_2d.global_position)

Can anyone help me?

According to the usage instructions related to the signal ,A signal can only be connected once to the same Callable. If the signal is already connected, returns @GlobalScope.ERR_INVALID_PARAMETER, The reason for this issue is generally related to the incorrect order of connecting and disconnecting.
Try using breakpoints at the connection and disconnection points to step through and observe the operation, which should help you identify the cause of the error.

2 Likes

Connecting and disconnecting signals does not feel like the right direction. Just have a boolean, say, is_ready_to_fire, and set it to false or true. You can spam the fire button, but until the weapon is ready to fire again (based on a timer or on the first laser shot being destroyed or whatever) it won’t fire.

1 Like

It seems a bit late to reply, but thank you for the suggestion!

1 Like