- Home /
Have a function to Wait until true?
Hi there,
I have a spawn function thats called every time my player dies. It basically turns off all the colliders and renderers on my player, moves it to a 'spawn point' wait a few seconds, and turns the player back on.
Is it possible to have this function to wait until a certain external variable is true, and then carry on?
So, right now it does:
- Turn off player 
- Move player 
- Wait a few seconds 
- Turn player on 
I now want it to do:
- Turn off player 
- Move player 
- Wait until 'blah' is true 
- Turn player on 
If your wondering... that 'Blah' is a weapon list that pops up when you die, and when the player selects the weapon he wants, 'blah' comes true. Thus then continuing to spawn the player.
I know theres all sorts of different methods. But I really want to know if a form of 'yield' is possible?
Thanks
Answer by awdawdawd · Aug 19, 2012 at 02:13 PM
I figured it out, not as complicated as I thought:
 function Spawn(){
 
    turnOffPlayer();
 
    while(weaponSelected == false){
       yield;
    }
 
    turnOnPlayer();
 }
See the manual on coroutines.
 IEnumerator Spawn() {
 
     turnOffPlayer();
  
     while(weaponSelected == false){
        yield return null;
     }
  
     turnOnPlayer();
 }
Answer by aFeesh · Nov 23, 2016 at 07:34 AM
Without the while loop in C# using WaitUntil
 IEnumerator Spawn() {
  
      turnOffPlayer();
 
      yield return new WaitUntil(() => weaponSelected == true);
 
      turnOnPlayer();
  }
using lambada ........ GENIAL, why i don't get it myself and googling like idiot ?
Why would you make it understandable while you can use some obscure lambda expression?
To remove the lambda, you can put it in a normal function:
 bool HasSelection() {
     return weaponSelected;
 }
 IEnumerator Spawn() {
     turnOffPlayer();
     yield return new WaitUntil(HasSelection);
     turnOnPlayer();
 }
This is....amazing. Thank you. This is probably the easiest method ive seen so far.
Answer by Trainzland · Jun 09, 2018 at 02:17 PM
Time_Flys example: Don't forget to use
 StartCoroutine(Spawn());
Otherwise it would not work.
Correct me if im wrong, but isnt it
 StartCoroutine("Spawn");
Answer by danielmahon · Apr 26, 2019 at 03:53 PM
Using async/await via https://assetstore.unity.com/packages/tools/integration/async-await-support-101056
 async void Spawn() {
     turnOffPlayer();
     await new WaitUntil(() => weaponSelected == true);
     turnOnPlayer();
 }
Note: I love the async/await paradigm (having used it in javascript/typescript) but I only recently started using it C# Unity so I'm not sure of any side-effects but haven't come across any so far. Makes the code soooo much cleaner IMHO.
await new WaitUntil(() => weaponSelected == true); This version waits per frame until weaponSelected == true. This is already asynchronous!
async void Spawn() .. Could you explain this, please.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                