How to connect signal using C#?

Godot Version

Question

script cue.cs:

[Signal]
	public delegate void ShootEventHandler(Vector2 power);

public override void _Process(double delta)
	{
		Vector2 mousePos = GetViewport().GetMousePosition();
		LookAt(mousePos);



		if (!Input.IsMouseButtonPressed(MouseButton.Left))
		{

			Vector2 dir = mousePos - Position;
			EmitSignal("Shoot", power * dir);
			power = 0;

		}
		else
		{
			power += 0.1f;


		}

		

	}

script main.cs

    private void onCueShoot(Vector2 power)
    {
        cueBall.ApplyCentralImpulse(power);
        

    }

Sorry for the English, I'm using a translator, thank you very much..

Assuming you want to connect from main.cs to cue.cs you do:

cue.Shoot += () => onCueShoot

// To emit the signal with parameters
EmitSignal(SignalName.Shoot, power);
1 Like

not worked =c

I managed to solve it using chatgpt:

private Sprite2D cueNode;

_Ready()
{
cueNode = GetNode<Sprite2D>("Cue");
        cueNode.Connect("Shoot", new Godot.Callable(this, "onCueShoot"));

}

Ah you didn’t have a reference to the cue node!

And yes you can use that syntax too though the docs recommend you use the new one I sent you but that will work fine :slight_smile:

1 Like

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