Global Signals and Autoloading (GDScript to C#)

Godot Version

4.2.1

Question

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).

Node2D.gd

extends Node2D

func _on_button_pressed():
print(“MainScene - OnButtom_Pressed”)
Globals.button_pressed.emit()

Globals.gd

extends Node

signal button_pressed

CuadradoEscena.gd

extends ColorRect
func _ready():
Globals.button_pressed.connect(on_button_pressed_received)
func on_button_pressed_received():
rotation_degrees+=15;
print(“CuadradoEscena.gd - on_button_pressed_received”)

(Optional) Is allowed external links?

I could pass you the zipped project, but I don’t know if that would be allowed here.

you can override _Pressed() if you just need to know if the button was clicked or _GuiInput()

using Godot;
using System;
public partial class MyButton:Button {
	public override void _Ready() { }
	public override void _Process(double delta) { }


	public override void _Pressed() {
		GD.Print("use this if you just need to know if the button was clicked");
	}

	public override void _GuiInput(InputEvent a_event) {
		if(a_event is InputEventMouseButton _a_mouse_button) {
			if(_a_mouse_button.ButtonIndex == MouseButton.Left && _a_mouse_button.Pressed) {
				GD.Print("button named \"", Name, "\" with display text \"", Text, "\" was clicked!");
			}
		}
	}
}

Globals.gd in C#:

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

Good news!
I managed to solve my problem.
I will share the solution with you:

Node2D.cs

using Godot;

public partial class MainEscena : Node2D
{
	public override void _Ready()
	{
        GetNode<Button>("Button").Pressed += OnButtonPressed;
    }

	private void OnButtonPressed()
	{
        GetNode<Globals>("/root/Globals").EmitSignal(Globals.SignalName.ButtonPressed);
    }
}

Globals.cs

using Godot;

public partial class Globals : Node
{
	[Signal] public delegate void ButtonPressedEventHandler();
}

CuadradoEscena.cs

using Godot;

public partial class CuadradoEscena : ColorRect
{
	public override void _Ready()
	{
		GetNode<Globals>("/root/Globals").ButtonPressed += OnButtonPressedReceived;

    }

	private void OnButtonPressedReceived()
	{
		this.RotationDegrees += 15;
		GD.Print("CuadradoEscena.CS - OnButtonPressedReceived");
	}
}

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