Description
I'm trying to write effects (à la buffs/debuffs) for my game. Here's what I want to build:
The class EffectsManager houses all active effects. A class like HealthController queries it for health-related effects, i.e. effects that implement the IHealthEffect interface, and gets back a List containing only such effects.
Attempted Solution
Inside EffectsManager:
public List<T> GetEffects<T>() {
List<T> effects = new List<T>();
foreach (ActiveEffect activeEffect in activeEffects) {
// ActiveEffect is just a wrapper for Effect that houses duration, time elapsed, etc.
// I only need to return the Effect itself, which houses the logic.
Effect effect = activeEffect.GetEffect();
if (effect is T) {
effects.Add((T)effect);
}
}
return effects;
}
And then, the caller script would write:
List<IHealthEffect> healthEffects = effectsManager.GetEffects<IHealthEffect>();
Problem
Cannot convert type 'Effect' to 'T'
On effects.Add((T)effect);.
Similar Questions
Cannot implicitly convert type 'Int' to 'T'
But in my case, I'm doing not an implicit but an explicit cast. Why won't the compiler let me proceed, and should I use Convert.ChangeType() — as suggested there — anyway?
More Info
Effect is just an abstract class with only 1 member: public int priority;.
Since an effect may affect multiple attributes, such as health and mana, I decided to go with interfaces that demand that the implementer write a specific method, e.g.: void Execute(HealthController healthController);.
Here you go:
As for why: you'd need an
objectcast in the middle:(T)(object)effect