- Home /
How do you handle progressive event calls?
I am currently developing a 2D top-down game, a very simple one but how do I handle events that runs step by step?
Like for example, I create a public variable that takes an array of functions like:
public func[] events;
and in the inspector, I add strings like:
events[0] = wait(2f)
events[1] = show_message(string)
events[2] = wait(2f)
events[3] = show_effect(target_id, effect_id)
Then there's an event manager that calls the coroutines one by one but not until the other one is done.
How do I exactly do that? Is that hard to make?
~~~~~~~~~~~~~~EDIT~~~~~~~~~~~~~~~
Okay so I managed to make it work, for now. But I need someone to check it for me like the problems that I will encounter if I go with this kind of approach?
Here is the event detector (if the player collides with it, it calls the events stored within it)
using UnityEngine;
using System.Collections;
public class event_manager : MonoBehaviour {
public string[] events;
public bool isTrigger;
event_collection ev;
void Start () {
ev = GetComponent<event_collection>();
isTrigger = GetComponent<BoxCollider2D>().isTrigger;
}
void Update () {
if (!isTrigger){
// run event when player pushes a button
}
}
void OnTriggerEnter2D(){
if (isTrigger){
if (events.Length <= 0) return;
StartCoroutine(ev.run (events));
}
}
}
Then here is the event collections. The collections of available event to use:
using UnityEngine;
using System.Collections;
public class event_collection : MonoBehaviour {
private Queue queue;
private bool event_running;
private bool coroutine_running;
void Start(){
queue = new Queue();
event_running = false;
coroutine_running = false;
}
public IEnumerator run(params string[] events){
// Break the IEnumerator if there are no events
if (events.Length <= 0) yield break;
// Enqueue the events
for (int ev = 0; ev < events.Length; ev++){
queue.Enqueue(events[ev]);
}
// Run the events while the queue is not empty
while (queue.Count > 0){
if (event_running == false){
event_running = true;
string arg = (string) queue.Dequeue();
if (coroutine_running == false){ //This is to stop coroutine from running twice
//check if the strings have commas that will be used as arguments
if (arg.Contains(",")){
char[] delimiter = {','};
string[] args = arg.Split(delimiter);
StartCoroutine(args[0], args[1]);
} else {
StartCoroutine(arg);
}
coroutine_running = true;
}
} else {
yield return null;
}
}
}
// Reset flags
void refresh(){
event_running = false;
coroutine_running = false;
}
// Collections of events
IEnumerator wait(string arg){
Debug.Log("Waiting " + arg);
float sec;
if (!float.TryParse(arg, out sec)) {
Debug.LogError(arg + " is not a float value!");
yield break;
}
yield return new WaitForSeconds(sec);
refresh();
}
IEnumerator testa(){
Debug.Log("running testa");
yield return null;
Debug.Log("testa run complete");
refresh();
yield break;
}
IEnumerator testb(){
Debug.Log("running testb");
yield return null;
Debug.Log("testb run complete");
refresh();
}
}
The string format of my events are like this: "coroutine_name, args"
<< it is comma seperated and will be split to act as an argument of the coroutine.
Any thoughts about it? is this an acceptable way to do events?
Your answer
Follow this Question
Related Questions
How to move rigidbody a certain distance, rather than constantly 1 Answer
How to make 3 bullets fire at different angles 1 Answer
prefabs inside content area not fitting entire width of content area? 0 Answers
Sprite image not changing 0 Answers
The type or namespace name `Events' does not exist in the namespace `TouchScript' 1 Answer