- Home /
How to properly use Yield for a coroutine?
I have some functions that fire one after the other and I want to make sure the first one is finished before the second one starts.
I used "yield" before the function names, ie:
yield myFirstFunction();
yield mySecondFunction();
Sometimes this works fine, but other times it gives an error:
"It is not possible to evaluate an expression of type 'void'."
If I remove yield from the first line it still errors out.
What's weird is that sometimes this works fine. Even for the block of code. It just stops working when I change something down the line but I don't really know what it is that's breaking it.
Can anyone shed some light on this? I searched the forum but found nothing. The errors in question are happening in a Javascript script.
ADDED: Here is one of the functions that won't let me yield it:
function FillCharactersThumbArray() { // loads in thumbnail images from a local folder and puts them into the appropriate character thumb in the listCharacters[] array...
for (nng=0;nng<listCharacters.length;nng++)
{
var www1 = new WWW ("file:///c:/SS/AssetPacks/thumbs/c/tn_"+listCharacters[nng].gender+"_"+listCharacters[nng].image+".png");
yield www1;
listCharacters[nng].image = www1.texture;
}
}
When you use file://
in WWW you don't need to yield anyway (it gets the result immediately), so that doesn't have to be a coroutine at all.
Well, you need to access to the file. If it's a big one or there are multiple files, it can freeze your game. Whether it's bearable or not depends of the context, e.g. when you're loading a level or you're about to snipe the ennemy ;)
Answer by Mike 3 · May 06, 2010 at 06:01 AM
the function that you call needs to be of type IEnumerator - this is done implicitly when you use yield in the function, but if you add in a return (or it is possible to get through the function without hitting a yield), it'll see it as a void function
if you want to tell the compiler to do it explicitly, you could do function myFirstFunction() : IEnumerator {
Edit - In the example you posted, the compiler can't be sure there will be a yield if the string is too short (0 chars) - so it's confused as to the return type
you could fix it by putting yield; at the end, or making the function IEnumerator
Thanks $$anonymous$$ike and Eric. I'm still wrapping my head around it but your answers are going a long way towards helping me understand.
Answer by Absyss · Aug 09, 2010 at 10:15 AM
what Mike meant is following:
function myFirstFunction():IEnumerator { ... } function mySecondFunction():IEnumerator { ... }
...
yield myFirstFunction(); yield mySecondFunction();
this way the compiler wont get confused about the correct type.
Answer by Techsuport · Sep 02, 2011 at 09:29 AM
you could do something like a call back, so even though you cant get the return right away, you can have function B call function D in the same class as function A to continue on where A left off.
class Face{
var brain:Brain;
function Talk(){
brain.Think(OfWhatToSay)
}
function OfWhatToSay(words:String){
print(words)
}
}
class Brain{
function Think(callBack:function(String)){
callBack(GetWordsToSayElseWhereInThisClass());
}
function GetWordsToSayElseWhereInThisClass():String{
return "Giberish";
}
}
check out this for an intro for callbacks.
Your answer
Follow this Question
Related Questions
WaitForSeconds Question 2 Answers
how would i cause the function to wait until it stops to function again? 1 Answer
C# yield not working. 2 Answers
Yield And Return 2 Answers