Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dscc_doncarbajosa · Jun 17, 2015 at 02:51 AM · c#unity 52d

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges