- Home /
Wait for Coroutine without yield? (C#)
I am using C# and I have a situation in which I need to wait for a coroutine to finish. However, the call for StartCoroutine is being done inside a bool function, so the yield keyword is not available to me. Essentially, the bool function is being returned before the coroutine can finish and set the return bool properly. Is there a way to wait for the coroutine without the yield keyword?
Here is the code:
//Check guest database whether score made it in top 20
public bool CheckGuestScore(int score, int miniGameId)
{
guestScore = score;
gameId = miniGameId;
StartCoroutine(CheckGuestScores(score, miniGameId));
return bPostScore;
}
//Check guest scores in MySQL DB
//remember to use StartCoroutine when calling this function!
IEnumerator CheckGuestScores(int score, int miniGameId)
{
string post_url = checkGuestScoreURL + "miniGameId=" + miniGameId + "&score=" + score;
//Post the URL to the site and create a download object to get the result.
WWW hs_check = new WWW(post_url);
yield return hs_check; // Wait until the download is done
if (hs_check.error != null)
{
print("There was an error posting the high score: " + hs_check.error);
}
if (hs_check.text == "true")
{
bPostScore = true;
}
else
{
bPostScore = false;
}
}
Answer by syclamoth · Nov 17, 2011 at 01:56 PM
Basically, what you are trying to do here fundamentally doesn't make sense. It sounds like it should work, but when you think about it, you're basically asking the entire program to halt, waiting for this one value to return. There's a reason why Coroutines work the way they do- they have to be able to act separately from the rest of the program.
Instead of using a method that returns a straight bool, split it into two methods-
public void StartScoreCheck(int score, int miniGameId)
{
guestScore = score;
gameId = miniGameId;
StartCoroutine(CheckGuestScores(score, miniGameId));
}
public bool PollGuestScores()
{
return bPostScore;
}
Then, in your other script, call the StartScoreCheck when you want to send a request, and then poll PollGuestScores() every frame until the result returns successfully.
Possibly, you could have PollGuestScores return a datatype which includes an 'undefined' state, for before the result has returned-
public enum ScoreInfo
{
InTopTwenty,
NotInTopTwenty,
Retrieving
}
Then make bPostScore not a boolean, but a ScoreInfo (and I guess change its name since it'd no longer be accurate), and set its state when the CheckGuestScores coroutine finishes.
Thanks syclamoth.
I was trying to avoid having to setup multiple function calls and a state system, but it appears it can not be avoided to achieve what I am looking for.
Your answer
![](https://koobas.hobune.stream/wayback/20220613045004im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trouble Resuming after Yielding while Inside Coroutine 1 Answer
Coroutine execution not continuing [Solved] 1 Answer
Multiple Cars not working 1 Answer
Waiting twice inside coroutine (C#) 2 Answers
Distribute terrain in zones 3 Answers