I was watching a simple tutorial on how to use a global script to work as an intermediary for signals issued by third parties.
The tutorial is this: https://youtu.be/OIrQ1PsEl3s
I tried it with GDScript and there are no problems, but the problem is when I want to translate it to C#.
Here I leave you the 3 GDScript blocks that I used, they are quite simple, but I couldn’t translate them (I was reading the documentation, but I didn’t find a solution there).
using Godot;
public partial class Globals_GD : Node
{
[Signal] public delegate void ButtonPressedEventHandler();
}
Node2D.gd in C#:
using Godot;
public partial class Node2d_GD : Node2D
{
private Button button;
private Globals_GD globals;
public override void _Ready()
{
globals = GetNode("/root/Globals_GD") as Globals_GD;
button = GetNode("NAME_YOUR_BUTTON") as Button;
button.Pressed += OnButtonPressed;
}
private void OnButtonPressed()
{
globals.EmitSignal(Globals_GD.SignalName.ButtonPressed);
}
}
CuadradoEscena.gd in C#:
using Godot;
public partial class CuadradoEscena_GD : ColorRect
{
private Globals_GD globals;
public override void _Ready()
{
globals = GetNode("/root/Globals_GD") as Globals_GD;
globals.ButtonPressed += OnButtonPressedReceived;
}
private void OnButtonPressedReceived()
{
this.RotationDegrees += 15;
GD.Print("CuadradoEscena.CS - OnButtonPressedReceived");
}
}
P.S. Just paste those code blocks into your files and you’re done. It is better to paste the code from globals first, otherwise you will get an error in other files that they don’t see it and you won’t like it. I pasted the code in my editor, there are no errors, but whether it works or not, I do not know, because the logic of the code is yours :0