Connecting to signal giving thousands of errors

Godot Version

Godot Engine v4.2.2.stable.official.15073afe3

Question

I’m making a flappy bird copy and want to update a variable “game_over” to be true when the player collides with an area named “KillZone”.
I’m instantiating killzones during runtime so I have put them in a group called “KillZones”. But I also have to constantly update the nodes in the group “KillZones” for when new ones are spawned in. Doing this gives thousands of errors though so I want to know if it’s possible to do in another way or at least remove the errors, thank you in advance :slight_smile:
Here is the error:

E 0:00:00:0859   general_manager.gd:9 @ _physics_process(): Signal 'body_entered' is already connected to given callable 'Node(general_manager.gd)::_on_kill_zone_body_entered' in that object.
  <C++ Error>    Method/function failed. Returning: ERR_INVALID_PARAMETER
  <C++ Source>   core/object/object.cpp:1358 @ connect()
  <Stack Trace>  general_manager.gd:9 @ _physics_process()

And in an autoload I’ve got this code:

extends Node
var score = 0
var game_over = false

# Connecting to game_over signal in all killzones every time a pillar spawns
func _physics_process(delta):
	var kill_zone = get_tree().get_nodes_in_group("KillZones")
	for KillZone in kill_zone:
		KillZone.body_entered.connect(_on_kill_zone_body_entered)

# Changes game_over variable to true
func _on_kill_zone_body_entered(body):
	var death_sound = get_node("/root/Game/DeathSound")
	if game_over == false:
		death_sound.play()
	print("Player died")
	game_over = true

It is worth noting that the code works perfectly fine, it just gives tons of errors

Why are you connecting every frame? That doesn’t seem correct

If you do need to you should either check if it’s connected (with is_connected with the callable) or use CONNECT_REFERENCE_COUNTED

1 Like

I’m new to godot, could you show how I would be able to use is_connected or CONNECT_DEFERRED?

See the documentation

Also my mistake meant CONNECT_REFERENCE_COUNTED, but you should probably just use is_connected, but you probably need to detail more what you actually are trying to do because this code looks wrong

1 Like

Well I want to connect to the function “_on_kill_zone_body_entered” on all instances of the “kill_zone” scene so I’ve put it in a group so I can connect to all functions at once, but when I connect to the function it only connects to the existing killzones and not the ones that are connected later.
I don’t really know what the for KillZone in kill_zone: does because it was suggested by somebody else…

You go through every node in the group, but you keep adding the ones that already add connected, over and over, so you should probably use:

if not KillZone.body_entered.is_connected(_on_kill_zone_body_entered):
    KillZone.body_entered.connect(_on_kill_zone_body_entered)
1 Like

Ah thanks! I forgot the .body_entered in if not KillZone.body_entered.is_connected(_on_kill_zone_body_entered):
It works with no errors now :smiley:

1 Like

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