Signal connect issues C#

Godot Version

4.1.3

Question

while trying to connect a newly spawned (instantiated) monster to a player:
if (!Mob._dungeon_monsters[i].HasSignal(“MonsterVanished”))
Mob._dungeon_monsters[i].Connect(“MonsterVanished”, new Callable(_player, “_OnMonsterVanished”));

I get the following error during runtime:
E 0:01:35:0028 Godot.NativeInterop.NativeFuncs.generated.cs:345 @ void Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr , IntPtr , System.Void** , System.Void* ): Signal ‘MonsterWon’ is already connected to given callable ‘CharacterBody2D(Player.cs)::_OnMonsterWon’ in that object.
<C++ Error> Method/function failed. Returning: ERR_INVALID_PARAMETER
<C++ Source> core/object/object.cpp:1294 @ connect()
Godot.NativeInterop.NativeFuncs.generated.cs:345 @ void Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr , IntPtr , System.Void** , System.Void* )
NativeCalls.cs:6402 @ Int32 Godot.NativeCalls.godot_icall_3_709(IntPtr , IntPtr , Godot.NativeInterop.godot_string_name , Godot.Callable& , UInt32 )
GodotObject.cs:792 @ Godot.Error Godot.GodotObject.Connect(Godot.StringName , Godot.Callable , UInt32 )
MOB_spawning.cs:206 @ void MOB_spawning.DistributeMOBS()
MOB_spawning.cs:121 @ void MOB_spawning._Process(Double )
Node.cs:2087 @ Boolean Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
CanvasItem.cs:1374 @ Boolean Godot.CanvasItem.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
Node2D.cs:516 @ Boolean Godot.Node2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
CollisionObject2D.cs:661 @ Boolean Godot.CollisionObject2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
PhysicsBody2D.cs:89 @ Boolean Godot.PhysicsBody2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
StaticBody2D.cs:131 @ Boolean Godot.StaticBody2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
MOB_spawning_ScriptMethods.generated.cs:73 @ Boolean MOB_spawning.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(IntPtr , Godot.NativeInterop.godot_string_name* , Godot.NativeInterop.godot_variant** , Int32 , Godot.NativeInterop.godot_variant_call_error* , Godot.NativeInterop.godot_variant* )

if I check if the signal exists I don’t get the error, but it also doesn’t seem to work while running (ie. “_OnMonsterVanished” doesn’t get hit):
if (!Mob._dungeon_monsters[i].HasSignal(“MonsterVanished”))

since I can’t find an edit button, of course it should be without the if in the beginning:
while trying to connect a newly spawned (instantiated) monster to a player:
Mob._dungeon_monsters[i].Connect(“MonsterVanished”, new Callable(_player, “_OnMonsterVanished”));

Looks like it is already connected:

Signal ‘MonsterWon’ is already connected to given callable ‘CharacterBody2D(Player.cs)::_OnMonsterWon’ in that object.

And thats why it throws errors.

The error message refers to a different signal than the code you shared.

[…] Signal ‘MonsterWon’ is already connected to given callable […]

MonsterWon seems to be different from MonsterVanished - Does that help you hone in on the bug?

Other than that, your attempt to check for the signal looks a bit off to me. you wrote:

if (!Mob._dungeon_monsters[i].HasSignal(“MonsterVanished”))

HasSignal does not check for an existing connection, but for whether the object has that signal defined. So I’m assuming that this condition will always be false (given that all objects in _dungeon_monsters have that signal defined) and thus the signal won’t be connected to at all. I don’t think this approach will work.

Another thing to maybe look at: The call stack shows that you’re calling DistributeMOBS from _Process. _Process is called every frame, so you should double-check if you’re ensuring that DistributeMobs (and thus, the code that connects the signals) is only called once.

1 Like

thanks, I didn’t see what the HasSignal actually checks.
I changed the project a bit, though the reason why it was being called from _Process is I was testing the spawn function by a key press/stroke (which was defined there).

I’m now using a custom node, but same logic, which seems to work, though now the animations aren’t playing :frowning: