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?