I'm looking for a way to attach listener to a C# variable, so that every time its updated or read, it will execute a function which will go on and process their own variables.
I know of the means described below but I'm looking for something that would work like the actualListener
. Every single time someone asks this question they're re-refered to get/set.
Is there a way to attach an actual listener to a variable which would update the variable as well as invoke an Event? Nobody ever says it can't be done or that it will take major effort.
// Downside: If I have to do this for every variable, if I will have to change a little bit of set, I will have to repeat that action in every set
class getset
{
int _myInt = 5;
string _myText = "Donk";
public int myInt
{
get => _myInt;
set
{
_myInt = value;
print($"React to some of this: {myInt}");
// or PerformAction(myInt);
}
}
public string myText
{
get => _myText;
set
{
_myText = value;
print($"React to some of this: {myInt}");
}
}
void Awake()
{
myInt *= 2;
}
}
// Downside: Way too messy, need to guess variables, don't get IDE autocomplete, enum only postpones the maintenance doom
class funcset
{
public int myInt = 5;
public string myText = "Donk";
void Awake()
{
dynamic _ = GetVariable("myInt");
SetVariable("myInt", _ * 2);
}
void SetVariable(string variableName, object variableValue)
{
switch (variableName)
{
case nameof(myInt):
{
myInt = (int)variableValue;
break;
}
case nameof(myText):
{
myText = (string)variableValue;
break;
}
}
}
dynamic GetVariable(string variableName)
{
switch (variableName)
{
case nameof(myInt):
{
return myInt;
}
case nameof(myText):
{
return myText;
}
}
return null;
}
}
// Downside: It's less messy than above, but its equally unmaintainable and its less performant than above
class reflection
{
public int myInt = 5;
public string myText = "Donk";
void Awake()
{
// GetField from GetMethod from GetType, you know what goes here
// SetField from method cached above
}
}
// Does it exist? If not, is there something equally effective? Its clear, readible, performant
class actualListener
{
public int myInt = 5;
public string myText = "Donk";
myInt.onChange.AddListener(() => print($"React to some of this: {myInt}"));
myText.onChange.AddListener(() => print($"React to some of this: {myText}"));
}