- Edited
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