Need help with C# signals with extra parameters

C# 4.4 dev 7

I just ported a C# 3.6 game to 4.4. I have a few signals that I had to add additional parameters to. My understanding in C# I must use Lambdas.

  1. The first problem is, after setting them up, I get errors every time I use them.
  2. I cannot disconnect them, and get errors when I try.
Button btn1 = GetNode<Button>("vbx/grdBtns/btn0");
Button btn2 = GetNode<Button>("vbx/grdBtns/btn1");

opt.ItemSelected += (idx) => _optSelected(idx, "Dark", 23)
btn1.ButtonUp += () => _btnUp(0);
btn2.ButtonUp += () => _btnUp(1);

Whenever I use the opt node, it runs as expected, but I get this error:
ERROR: Error calling from signal ‘item_selected’ to callable: ‘Control(Main.cs)::_itlHistorySelected’: Method not found.
at: emit_signalp (core/object/object.cpp:1213)

Then when I try to disconnect the signal with this code:
btn0.ButtonUp -= () => _btnUp(0);
It doesn’t work and the button isstill usable but I also get this error:

ERROR: Attempt to disconnect a nonexistent connection from ‘btn0:<Button#32916899293>’. Signal: ‘button_up’, callable: ‘Delegate::Invoke’.
at: _disconnect (core/object/object.cpp:1493)

Could someone please help me figure out these two errors and fix them

You don’t needed lambdas, you connect to signals, events, delegates, you cannot disconnect from lambadas if you don’t have reference to this lambda.

1 Like

Yes, I would use .Connect() but in C# I can’t connect to a signal AND extra parameters without lambdas.

If I have a button I want to pass a parameter to it when I push it? How is this done with C# and .Connect()?

In GDScript you can “.bind” more parameters to a “pressed” signal, but how do you do this C# without lambdas?

I think you currently can’t, you have to use lambdas.

Remember though that you can mix C# and GDScript scripts together within your project, so if your usecase will benefit from GDScript functionalities, then why not trying to convert that one script to GDScript (or at least part of it)?

I ‘think’ if the above is your function, when you call it you have to match the argument signature exactly, so if

_btnUp(0)

is calling that, you’d need to add those other arguments, like:

_btnUp(0, "Light", 12)

(just an example of the other two arguments above)

but I might be misreading what you have in your post, I just know for the issues I had that was the cause of the ‘Method not foundd’ error.

  1. Can make custom signal,
  2. Can make variable getters for signal senders, and access while normal signal call.
using System;
using Godot;


namespace LabdaTest2025;
public partial class ButtonWithLamda : Button
{
	Action MyAction;
	public override void _EnterTree()
	{
		MyAction = () => CustomMethodWithArgument("Test");
		Pressed += MyAction;
	}
	public override void _ExitTree()
	{
		Pressed -= MyAction;
	}

	private void CustomMethodWithArgument(string Text)
	{
		GD.Print(Text);
	}
}

instead this

you can also:

		MyAction = delegate { CustomMethodWithArgument("Test"); };