I want to emit a signal when a property in my globals.cs code changed, also this globals.cs is already in my autoload so i can accesss it anywhere as long as the properties and methods are static for example this is a part of my globals.cs:

    private static int _health = 50;
    [Signal] public delegate void HealthChangedEventHandler();
    public static int health
    {
        get => _health;
        set
        {
            if (value >= 100)
            {
                _health = 100;
            }
            else
            {
                _health = value;
            }
            EmitSignal("HealthChanged");
        }
    }

the problem here is i cannot emit signal in setter because the health is defined as static and EmitSignal needs an instance to work with and the other problem is if i dont define the health as static then i cannot access it in other c# codes because then need an instance of Globals to work with and as i know when i add Globals.cs to autoload it wont create an instance like the c# way so the c# codes cannot recognize it as an instance and this cause to define everything as static in the Globals to be able to access them in other codes BUT in gdscript i realized that we can do it easily with this code:

signal HealthChanged()
var health: int = 50:
    get:
        return health
    set(value):
        if value >= 100:
            health = 100
        else:
            health = value
        HealthChanged.emit()

how can i achieve this behavior in c#?! i searched everywhere i cannot find a solution for it

  • cr0 replied to this.
    • Best Answerset by cr0

    cr0 i find a way to impliment gdscript way and that is i need to define the variables as non static to be able to emit signals inside them and then when i running the game i need to create an instance of the globals class so i created a new property inside the class itself and instantiate the class itself inside it like:

    public static Globals Instance { get; private set; }
    public override void _Ready()
    {
        Instance = this;
    }

    and this way i can now call Globals.Instance and now i can access the health and other properties that are not static and also i dont need to define a variable inside all of my codes and get this node to create seperate instances! 🙂

    • Edited
    • Best Answerset by cr0

    cr0 i find a way to impliment gdscript way and that is i need to define the variables as non static to be able to emit signals inside them and then when i running the game i need to create an instance of the globals class so i created a new property inside the class itself and instantiate the class itself inside it like:

    public static Globals Instance { get; private set; }
    public override void _Ready()
    {
        Instance = this;
    }

    and this way i can now call Globals.Instance and now i can access the health and other properties that are not static and also i dont need to define a variable inside all of my codes and get this node to create seperate instances! 🙂