- Home /
Coroutines and waiting for a function to finish execution
Hi,
I need to call a function and wait for it to finish before continuing the script.
This is what I have at the moment:
var cardDeck;
function newHand(){
//do stuff
yield shuffleCards();
//do other stuff - that relies on the cardDeck variable
}
function shuffleCards (){
//do stuff
//change cardDeck var
yield; //does this pause the game for a whole frame? can I avoid it??
}
The problem with this is that I don't want to pause the game for a whole frame - just for long enough for the function to finish executing. I am yielding functions quite a few times in this script and it could add up.
I have tried it without the yields and it seems to work fine. - I am just worried that this is messy scripting and could lead to later problems.
I have also tried it without the second yield statement (in the shuffleCards function) and it comes back with the error message: 'It is not possible to evaluate an expression of type void'.
Is there any way I can make sure the function has finished before continuing without pausing for a whole frame?
Thank you in advance.
Yours
Miriam.
This stems from 2010. 8 years later I'm stuck with somewhat of the same issue. I'd like to call a coroutine recusively with yield return startCoroutine(function(param)); without yielding the result... Any ideas?
Answer by Eric5h5 · Apr 04, 2010 at 04:13 PM
If a function is not a coroutine (doesn't have yield in it), and you call it normally, all the code in the function must be executed before anything else can happen anyway. Unity scripting is single-threaded and can only do one thing at a time, unless you specifically create threads.
Functions aren't coroutine, but Unity scripting isn't single-threaded either. (Or at least, not anymore it seems.)
Each separate scripts files are kinda like a coroutine. If you call a function in a script from another script, the function might not be finished before the processor move onto the next line in the first script's code.
The time required for the processor to pass through a script isn't big so the risks aren't nearly as present as I may sound, but any actions that requires, for example, the input or output of another component other than the processor can make a relatively huge difference here. For example, calling a function that deserialize a remote file on the HDD might not be finished before the next line of code is processed in a script IF that function isn't present in the said script.
So if you got a script A that call function "SaveCharacter" which exist in Script B and the process involve quite a few data management and/or conversion on the spot (like converting some unserializable data into strings to be serialized), the "SaveCharacter" function might not be done by the time the processor run the next lines of code in the Script A.
I have faced some major issues related to this by having a saving process "halted" mid-way as I called the save process prior to a scene change function. The Save function wasn't done within the time before the change scene was called and the save file was then incomplete and unusable.
Your answer
Follow this Question
Related Questions
How to properly use Yield for a coroutine? 3 Answers
How to use yield within a class function 2 Answers
StartCoroutine important for using yield? 1 Answer
Coroutoutine doesnt work properly. 3 Answers
Im a bit confused on a simple script 1 Answer