Coroutines and waiting for a function to finish execution


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.



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.