c# - Is there a way to stay in a coroutine until a condition is met? -


i trying program simple dialogue system. when player gets dialogue box, box remains until press key. second dialogue box appears. again, press key, , on.

i realizing, however, method of using coroutines doesn't work, because every dialogue box displays @ once. using simplified version:

ienumerator test() {         //code displays message         yield return startcoroutine(waitforkey(keycode.space));     }      ienumerator waitforkey(keycode keycode)     {         while (!input.getkeydown(keycode))             yield return null;     }      void start(){         startcoroutine(test());         startcoroutine(test());     } 

the results of above code 2 messages displayed. plus, display immediately--once condition met, process jumps courtine initiating, runs next line of code, , periodically returns first coroutine see if has finished.

how 1 coroutine finish before continues rest of code following it?

the easy way go creates arrays of canvas dialog. disable of them in editor. code below, can change array size of dialoguegameobjectcanvas how many dialogues have assign parent of each dialogues in order dialoguegameobjectcanvas array in editor. cancas must parent of each dialogue. run it.

pressing space bar go next dialogue. easiest way.

now, pro way. if dialogue generated during run time, can use list add or remove dialog(gameobjectuicanvas)instead of using array it. can convert array list , should work dynamically generated dialogues.

startdisplaydialogue() starts dialogue.

isdialoguedisplaying() tells if dialogue displayed on screen.

getdialoguecurrentnumberdisplaying() gets current array number of dialogue disaplying

stopdisplaydialogue() kills whole dialogue , closes them.

remember startdisplaydialogue() coroutine function , must started startcoroutine (startdisplaydialogue ());

public gameobject[]dialoguegameobjectcanvas; bool isrunning = false; int dialognumberdisplaying = 0;  // use initialization void start () {     //animationsprite = new gameobject[5]; uncomment if want assign through code, otherwise assign dialogues editor     startcoroutine (startdisplaydialogue ()); }  //display first dialogue wait space pressed display next dialue ienumerator startdisplaydialogue () {     //make sure there 1 instance of dialuge functionr running. break if there alread 1 running      if (isrunning) {         yield break;     } else {         isrunning = true;     }      int dialoguenumber = 0; //starts 0 size of animationsprite array     dialognumberdisplaying = dialoguenumber;      while (isrunning) {         //stop if dialognumber >= number of dialogues dispaly(dialogue arrays)         if (dialoguenumber >= dialoguegameobjectcanvas.length) {             isrunning = false;             //dialoguegameobjectcanvas [dialoguenumber].setactive (false);             debug.log ("end of dialogue");             yield break;         }          //dispaly current dialogue based on array number         dialoguegameobjectcanvas [dialoguenumber].setactive (true);          //wait until space bar pressed          while (!input.getkeydown(keycode.space)) {             yield return null; //must wait here or else unity freeze         }          //space bar has been pressed. disable current dialogue increement dialoguenumber next 1 in array display         dialoguegameobjectcanvas [dialoguenumber].setactive (false);         dialoguenumber++;         dialognumberdisplaying = dialoguenumber; //for getdialoguecurrentnumberdisplaying function           //check if stopdisplaydialogue called exit         if (!isrunning) {             dialoguegameobjectcanvas [dialoguenumber].setactive (false);             yield break;         }         yield return null;     } }  //check if dialogue displaying bool isdialoguedisplaying () {     return isrunning; }  //get current array number of dialogue displaying int getdialoguecurrentnumberdisplaying () {     return dialognumberdisplaying; }  //stops dialogue displaying void stopdisplaydialogue () {     isrunning = false; } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -