Search Unity

Logic's tips for Event System

Discussion in 'Scripting' started by Burton-kun, Nov 26, 2015.

  1. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    Hello guys,
    I worked on a peronal Event system to make my player interact with other npcs on map. The Event system implements a system of variables and switches to trigger the storyline.
    Everything works, everything is done, the various commands will be called from a list of string.
    For each string in this list, the Interpreter will call a specific function during the game (Moving an object, show a dialog and so on).
    The problem, dear me, is the Interpreter itself. I want this "reader" to call the various commands one by one.
    I tried with a coroutine but nothing, or maybe I scripted bad.
    How can I call these methods, these commands in sequence, waiting for the previous one to be over?
    Thank you very much!
     
  2. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    nobody?
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    It sounds like you're on the right track. Make each function a coroutine. In your main coroutine, yield until the function coroutine is done. For example, roughly:
    Code (csharp):
    1. IEnumerator Interpret(List<string> commands) {
    2.     foreach (var command in commands) {
    3.         yield return StartCommandCoroutine(command);
    4.     }
    5. }
    6.  
    7. IEnumerator StartCommandCoroutine(string command) {
    8.     if (string.StartsWith("MoveObject")) {
    9.         return StartCoroutine(MoveObjectCoroutine(...));
    10.     } else if (string.StartsWith("ShowDialog")) {
    11.         return StartCoroutine(ShowDialogCoroutine(...));
    12.     } // etc.
    13. }
    Consider it pseudocode above, just to get the idea across. I assume you'll have to do extra parsing to get the details for each command, so I glossed over StartCommandCoroutine.
     
    Burton-kun likes this.
  4. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    That's exactly what I was looking for.
    Thank you very much TonyLi! :)