What the hell is a generator????????

This is causing me sooooo much grief it just isn’t funny any more…:

function CreateNameForHighscore() : boolean

Give the error message:

Removing the return value and saying:

function CreateNameForHighscore() 
{
	scores = (Globals.currentRange * 2) + 1;

	if (Globals.currentScore < highScores[scores][highScoresToSave - 1])
	{
		Globals.positionThisTurn = highScoresToSave;
		return false;
	}
...

Gives me the error:

I have run into so many of these errors it just isn’t funny.

Another example:
I make a function, calling it with a yield, everything works fine.
I make a second function, it is fine, I call a yield on the second function now the first function has become a generator and cannot return a value and cannot be used with the yield statement…

Heck, I even copied and pasted the example code from the docs into my page but because I already have a function I call yield on, the EXAMPLE CODE returns errors saying it is a generator and cannot return values…

So my question again:
What the hell is a generator???

Any function which contains a yield statement is a coroutine (which is a specialised kind of generator function). The mechanism which controls coroutines requires that they return an IEnumerator object, and the yield statement automatically returns this object for you. However, that means you can’t output your own values with the return statement and you can’t specify the type of the return value as being anything other than IEnumerator.

If you need to get a value out of a coroutine, you’ll have to put it somewhere that the calling function can get at, such as in a member variable (declared outside of all functions). Don’t forget that coroutines are designed to run over an indefinite period of time, and the variable probably won’t be set right away, so you’ll have to make sure you know when the coroutine has finished before relying on its value.

At the risk of showing my age: What happened to the days when using the “H” word resulted in a mouth full of soap? :wink:

thankx for that

i noticed a waitforseconds within my function. so yielding that cased my latest generator issue…

i already solved the issue using a workaround, like your former suggestion, but at least now i know why it was needed…

thankx again

oh, and apologies for the H word :slight_smile:
no Harm intended :stuck_out_tongue:

@ NCarter
Thanks for the intelligent answer. I like knowing why and that was a pretty clear description.
Always appreciated.

@ bigkahuna
lol! we must be close to the same age. got a good laugh out of that.
thanks!