I've been trying to build a "event system" for a project that I'm working on . Here is how I'm doing it : I populate a list with reference to a gameObject and the functions that I need to execute from that gameObject . Then , when the "event" is triggered (in this case , when the player steps into a trigger collider) I just loop over the list and Invoke the functions inside it.
The problem with this is that every single function inside the list gets executed at the same time. This works fine in some cases but if I want to create a more cinematic event I need to have the ability to execute a function after the previous one has finished it's execution .Sadly I have no idea how to do that.
I've been reading a lot of the documentation of both Unity and C# about coroutines and delegates but I can't seem to wrap my head around all those things and find a way to do implement them on code .So I need your help with that :How can I achieve this?
TL;Dr version : I need your help in coming up with a way to execute a function after the previous function has ended it's execution .These functions would be stored in an array or list.
Here is the code of the trigger of the event itself
public class ActivateEvent : MonoBehaviour {
private bool activated;
public Signal[] actionList;
[HideInInspector]
public int actionEventAmount;
// Use this for initialization
void Start()
{
activated = false;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag.Equals("Player") && !activated)
{
Activate();
}
}
public void Activate()
{
activated = true;
for(int i=0;i<actionEventAmount;i++)
{
actionList[i].Invoke();
}
}
}
and here is the code for the Signal Class
public class Signal
{
public GameObject target;
public string method;
public string argType;
public Signal()
{
}
public Signal(System.Type argType)
{
this.argType = argType.FullName;
}
public void Invoke()
{
if (target != null && !string.IsNullOrEmpty(method))
target.SendMessage(method, SendMessageOptions.RequireReceiver);
}
public void Invoke(object value)
{
if (argType != null)
{
if (target != null && !string.IsNullOrEmpty(method))
{
if (argType.Equals(value.GetType().FullName))
target.SendMessage(method, value, SendMessageOptions.RequireReceiver);
else
Debug.LogError("Incorrect parameter type, expected [" + argType + "], got [" + value.GetType().FullName + "].");
}
}
else
Invoke();
}
}
public class SignalAttribute : System.Attribute
{
public string name;
public SignalAttribute()
{
}
public SignalAttribute(string name)
{
this.name = name;
}
}