- Home /
[C#] yield instruction in function with return value
I would like to create a function which returns a value based on some user choices (clicks on GUI buttons).
Is it possible to do this? The function should be something like
int getUserChoice(){
/*
* When the user finishes clicking on the GUI buttons, another thread
* sets the user choice to its actual value. Until then its value is -1.
*/
userChoice = -1;
displayAvailableChoices();
while(userChoice < 0)
yield return WaitForFixedUpdate();
return userChoice;
}
The problem is that I can't use the yield statement because the function is not a "IEnumerator". Is there any way I can do this? It's not like I cannot have a callback function, but this would simplify A LOT my classes (because of polymorphism). Thank you in advance :)
P.S. The code is not actually this one, it's a lot more complicated, but you get the idea ;)
Answer by whydoidoit · May 13, 2012 at 07:37 PM
Unfortunately you can't return from a function that uses yield as that routine will execute over a period of time and it would make your code block. You could pass a delegate to call when the fixedupdate is done or implement the choice at that point.
That's too bad, my code was extremely clean and pretty awesome. Well except that it doesn't work..
What if I change the return type to IEnumerator and use pointers for the output? Would that work?
Your answer
Follow this Question
Related Questions
C# Coroutine help 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I make sure a resource is loaded or is instantiated? 2 Answers
c# update loop. How to? 2 Answers