2
\$\begingroup\$

I have recently decided to start learning Godot after previously having experience with Unity, and am continuing with C# as my primary scripting language. While everything else regarding signals seems to be working, I find myself struggling to emit custom signals, as they are not defined within the SignalName subclass as the Godot Documentation claims.

Complete script:

using Godot;
using System;

public partial class Icon : Sprite2D
{
    [Signal] 
    public delegate void RotatingToggledEventHandler(bool isRotating);

    bool rotating = true;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        if(rotating) 
        {
            Rotate((float) delta);
        }
    }

    public override void _Input(InputEvent @event) 
    {
        if(@event.IsActionPressed("ui_accept")) 
        {
            rotating = !rotating;
        }
        StringName rotatingToggled = "RotatingToggled";
        EmitSignal(SignalName.RotatingToggled);
    }
}

SignalName subclass:

public class SignalName : Node2D.SignalName
{
      public static readonly StringName FrameChanged;
      public static readonly StringName TextureChanged;

      public SignalName();
}

The event which is to be triggered by the signal emit:

using Godot;
using System;

public partial class main : Node2D
{
    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }
    private void OnIconRotatingToggled(bool isRotating) 
    {
        GD.Print("hello there");
        if(isRotating)
        {
            GetNode<Label>("RotatingStatus").Text = "Rotating: active";
        }
        else 
        {
            GetNode<Label>("RotatingStatus").Text = "Rotating: inactive";
        }
    }
}

The receiver method is already linked to main.cs. enter image description here

The error: enter image description here

Error code is CS0117.

ADDITIONAL INFO: I am using VS code as my text editor, and have the C#, godot-tools, and C# tools for Godot extensions enabled, and am connected to the GDScript language server. All other facets of scripting seem to be working. For context, I am trying to reconstruct this tutorial in C# to teach myself Godot signals.

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

What you need to emit a signal is an StringName. You should be able to confirm that by looking at the definition of EmitSignal.

An StringName is an internal string in Godot engine.


SignalName, on the other hand, is a convenient way to get the StringName you need to pass to reference the signal. And it should be generated automatically.

It is not relevant if the StringName comes from SignalName or not, what matter is if the StringName is correct. Consequently it is not necessary to subclass SignalName.

Do not confuse StringName and SignalName.


Thus, if the signal you want is not in SignalName, you can still use EmitSignal, like this:

EmitSignal("RotatingToggled");

This is also the way you would use it if you added signal dynamically during execution (with AddUserSignal).

Here, the string will be implicitly converted to an StringName. You could do that conversion yourself. You could also extract the value to a variable. And if you want to reuse it, perhaps promote said variable to a readonly field. And if you really want, move that field to another class - which you need to reference correctly - and said class could perhaps extend SignalName - which is completely irrelevant - … None of that is necessary for this to work.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you! This seemed to do the trick. I wish the Godot documentation was clearer on this, but maybe I should just actually read the method descriptions. \$\endgroup\$
    – Deano
    Commented Aug 29, 2023 at 17:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .