- Home /
generic event system with scriptable objects
Hey, I currently try to build myself an event system. I'm using scriptable Objects to build loose couplings. This is what I currently have:
[CreateAssetMenu]
public class GameEvent : ScriptableObject {
private List<Action> subscribers = new List<Action>();
public void Raise() {
for(int i = subscribers.Count - 1; i >= 0; i--) { subscribers[i](); }
}
public void Subscribe(Action subscriber) {
subscribers.Add(subscriber);
}
public void Unsubscribe(Action subscriber) {
subscribers.Remove(subscriber);
}
}
With that code I can subscribe every method without parameters by doing that:
public class ExampleClass : MonoBehaviour {
[SerializeField]
private GameEvent gameEvent;
private void OnEnable() {
gameEvent.Subscribe(OnEventRaised);
}
private void OnDisable() {
gameEvent.Unsubscribe(OnEventRaised);
}
public void OnEventRaised() { }
}
So know to my question!
I want to make a version of that that can take parameters but the only solution I found was making a new custom GameEvent class for every new combination of variable types. Like this for sending an int variable.
[CreateAssetMenu]
public class GameEventInt : ScriptableObject {
private List<Action<int>> subscribers = new List<Action<int>>();
[SerializeField]
private int debugValue = 0;
public int DebugValue { get { return debugValue; } }
public void Raise(int value) {
for (int i = subscribers.Count - 1; i >= 0; i--) { subscribers[i](value); }
}
public void Subscribe(Action<int> subscriber) {
subscribers.Add(subscriber);
}
public void Unsubscribe(Action<int> subscriber) {
subscribers.Remove(subscriber);
}
}
(I also have a custom Editor to Raise the Event from the inspector of the created scriptable objects, therefore, is the debugValue)
So do you have an idea how to make a more generic version where I can select the variable type after creating the scriptable object?
(The best would be to have the option to select if and how many parameters you want to sent and then the types of the variables but I think that's very complex so I would be happy with the select variable type thing :D)
Ps: That's my custom editor just if you need it:
[CustomEditor(typeof(GameEventInt))]
public class GameEventIntEditor : Editor {
public override void OnInspectorGUI() {
DrawDefaultInspector();
GameEventInt myScript = (GameEventInt)target;
if(GUILayout.Button("Raise Event")) {
myScript.Raise(myScript.DebugValue);
}
}
}
What if, ins$$anonymous$$d of using Actions, you create a class than has an action as a member, with additional information. You could create an enum of types the events can take, and parse the inco$$anonymous$$g event object into whatever it is expecting. Then your game event class can keep a list of these new actionable classes, and handle them accordingly.
Did you ever find an answer to this? I would love to know your solution if you did.
Your answer
Follow this Question
Related Questions
Serializing specialized subclasses of generic classes not working 1 Answer
Get list of all "Action" classes 1 Answer
How i can subscribe to an Event through Inspector 0 Answers
Stop a MonoBehavior from being addable as a script? 0 Answers
Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene? 1 Answer