'C# class inheritance with ScriptableObject UnityEvents and differently-typed UnityActions
I'm creating custom UnityEvents for a game. The base class looks like this:
public class EventSO : ScriptableObject
{
List<UnityAction> listeners = new List<UnityAction>();
public virtual void Raise() { ... }
public virtual void AddListener(UnityAction listener) { ... }
public virtual void RemoveListener(UnityAction listener) { ... }
}
Sometimes I want to pass variables to an event. So I inherit:
public class FloatEventSO : EventSO
{
List<UnityAction<float>> listeners = new List<UnityAction<float>>();
public override void Raise(float f) { ... }
public override void AddListener(UnityAction<float> listener) { ... }
public override void RemoveListener(UnityAction<float> listener) { ... }
}
My goal is to be able to create ScriptableObjects for individual events I can then assign and invoke: a good example for a Float event might be to communicate game times. So, a ScriptableObject named "OnEveryHalfSec", for example.
The first problem with this approach is that I'll need to make a new class for each combination of variables passed to an event: UnityAction<float, int>, UnityAction<float, string>... and so on.
The second problem is that I can't override the base class like I've shown above; Raise(float) can't override Raise(). So I have to re-define on every class.
Is there a better way to approach this problem?
Solution 1:[1]
It looks like you should define
public override void Raise(float f) { ... }
as
public virtual void Raise(float f) { ... } in the base class
You didn't put "(float f)" in the base class
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Murphy |
