So I need to make a scripted event system that will have a large number of pretty diverse events that can happen. I’ve got a few ideas of how to do this and I keep going back and forth so I decided to crowd source for opinions.
One idea is to have an event class that has a bunch of generic modifier methods and a public array that will execute those methods in different orders on an object.
Simple Example:
public class MyObject {
public string myString = "";
public int myInt = 0;
public float myFloat = 0.0f;
}
public class MyEvent : MonoBehaviour {
public MyObject myObject;
public string[] actions = new string[]{
"ModifyString",
"ModifyInt",
"ModifyFloat"
};
void OnEnable () {
foreach(string s in actions){
Invoke(s, 0);
}
}
void ModifyString () {
myObject.myString = "New String";
}
void ModifyInt () {
myObject.myInt = 37;
}
void ModifyFloat () {
myObject.myFloat = 89.675f;
}
}
This system would make things pretty generic I think and it definitely has its limitations. No return values (or at least not an easy way to say what return value I want at what point anyway).
another idea is to have a base event class and just extend all my possible events from that.
public class MyObject {
public string myString = "";
public int myInt = 0;
public float myFloat = 0.0f;
}
public class MyEvent : MonoBehaviour {
public MyObject myObject;
void OnEnable () {
Execute();
}
public virtual void Execute () {
}
}
public class Event1 : MyEvent {
public override void Execute () {
myObject.myString = "New String";
myObject.myInt = 37;
myObject.myFloat = 89.675f;
}
}
public class Event2 : MyEvent {
public override void Execute () {
myObject.myInt = 88;
myObject.myFloat = 3.2f;
}
}
public class Event3 : MyEvent {
public override void Execute () {
myObject.myString = "A Different String";
}
}
This definitely seems the most versatile but it could get a little hard to manage all the possible scenarios. I’m also not sure what the cost of having a whole bunch of smaller classes would be vs one large, generic class that I can just invoke from.
These examples are simple but in reality I need these events to do a wide variety of things from showing some text in the GUI while waiting for an input to simply modifying a number somewhere.
Thoughts? Suggestions? Random Ideas?