Custom C# signals not working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SeerXiao

When creating a custom signal, it is not possible to connect the signal. When calling the connect method, an error is thrown.

Node emitting script :

public partial class SignalEmitter : ColorRect
{
	[Signal]
	public delegate void MySignalEventHandler();
	
	public override void _Process(double delta)
	{
		EmitSignal(SignalName.MySignal);
	}
}

The script of the node listening to the signal:

public partial class RespondToSignal : ColorRect
{
	public override void _Ready()
	{
		ColorRect emmiter = GetNode<ColorRect>("SignalEmmiter");
		emmiter.Connect("MySignal", this, "SomeFunction");
	}
	
	public void SomeFunction()
	{
		GD.Print("work");
	}
}

And here are the errors I’m getting:

E:\Godot\Test\RespondToSignal.cs(10,31): error CS1503: Argument 2: cannot convert from ‘RespondToSignal’ to ‘Godot.Callable’ [E:\Godot\Test\Test.csproj]
E:\Godot\Test\RespondToSignal.cs(10,37): error CS1503: Argument 3: cannot convert from ‘string’ to ‘uint’ [E:\Godot\Test\Test.csproj]

Please help me. I don’t understand what I’m doing wrong.

:bust_in_silhouette: Reply From: Zylann

There are several ways to connect a signal, but all involve passing a Callable as the signal handler, instead of a string.

Maybe you should use different syntax: Object — Godot Engine (stable) documentation in English

var emmiter = GetNode<ColorRect>("SignalEmmiter");
emmiter.Connect("MySignal", Callable.From(SomeFunction));

The script doesn’t work. And I am getting these two errors

E 0:00:01:0518 Godot.NativeInterop.NativeFuncs.generated.cs:345 @ void Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr , IntPtr , System.Void** , System.Void* ): Node not found: “SignalEmmiter” (relative to “/root/Node2D/RespondToSignal”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1620 @ get_node()
Godot.NativeInterop.NativeFuncs.generated.cs:345 @ void Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr , IntPtr , System.Void** , System.Void* )
NativeCalls.cs:6025 @ Godot.GodotObject Godot.NativeCalls.godot_icall_1_676(IntPtr , IntPtr , Godot.NativeInterop.godot_node_path )
Node.cs:797 @ Godot.Node Godot.Node.GetNode(Godot.NodePath )
NodeExtensions.cs:47 @ T Godot.Node.GetNode(Godot.NodePath )
RespondToSignal.cs:9 @ void RespondToSignal._Ready()
Node.cs:2093 @ 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& )
Control.cs:2827 @ Boolean Godot.Control.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
ColorRect.cs:66 @ Boolean Godot.ColorRect.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
RespondToSignal_ScriptMethods.generated.cs:38 @ Boolean RespondToSignal.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* )

E 0:00:01:0522 RespondToSignal.cs:10 @ void RespondToSignal._Ready(): System.NullReferenceException: Object reference not set to an instance of an object.
<C# Error> System.NullReferenceException
<C# Source> RespondToSignal.cs:10 @ void RespondToSignal._Ready()
RespondToSignal.cs:10 @ void RespondToSignal._Ready()
Node.cs:2093 @ 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& )
Control.cs:2827 @ Boolean Godot.Control.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
ColorRect.cs:66 @ Boolean Godot.ColorRect.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
RespondToSignal_ScriptMethods.generated.cs:38 @ Boolean RespondToSignal.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* )

SeerXiao | 2023-07-08 04:48

I copied the example from the doc…

Node not found: “SignalEmmiter” (relative to “/root/Node2D/RespondToSignal”).

This sounds like a different error. Your node path is probably incorrect, or your code runs before the node is ready. It even looks like a completely different node, not the code I posted.

Before you had a compiling error. Now it compiles, since you are able to run it (and get a runtime error), which means the initial error was fixed, I guess…

Zylann | 2023-07-08 12:39