I'm trying to create status effect's for my game those are things that affect your player's statistics over time for example. However I'm having trouble with remembering the passed variable i.e the one that should be modified. Let's say you have some status effect that reduces your Health by 10 % for 10 seconds the way I do it is by instantiating a new class passing some parameters in along with the variable that needs to be modified in this case player's health, but the problem is that once the constructor is gone the reference is being broken between those 2 and from then on I just edit some variable that is never being used.
Here's how i instantiate a Status Effect
/// Creates a status effect which ticks at specified period of time without any condition.
public StatusEffect(float increase, EffectIncreaseType increaseType, float timeOfStatusEffect, float tickTime)
this.increase = increase;
this.endTimeOfStatusEffect = Time.time + timeOfStatusEffect;
this.tickTime = tickTime;
increaseCalculator = increaseType == EffectIncreaseType.Percentage ? increaseCalculator = (a) => (int)(increase / 100f * a) : increaseCalculator = (a) => (int)increase;
public bool TryTrigger(ref int affectedProperty)
affectedProperty += increaseCalculator.Invoke(affectedProperty);
There's no way to store a "ref" to a variable in .NET; that's deliberate. It eliminates the class of bugs common in C / C++, where you store a ref to a variable past its lifetime and bad things happen.
The way to do what you want is instead, pass in an
Action<T> that sets the variable (or property) you want to change, and then when you want to change it, you invoke the action.
How then does that solve the problem? Because C# makes sure that the variable captured by the delegate is always long-lived.