1

issue is as follows. I'm making a variant of a typing game, my issue is that I need to loop through my char array and check to see if the user is hitting the right key or not (introducing the requirement to wait for user input). When the right key is pressed, I need the while loop to stop and go back to the for loop and start over. But when I yield return, it exits both loops and I get Unreachable Code Detected.

I know this means that the for loop can't go through the rest of its iterations.. So where do I yield/return?

IEnumerator TestMyCoroutine() {

    for (int i = 0; i < gCharArray.Length; i++) {
        string charHolder = gCharArray[i].ToString();

        while (true) {

            if (Input.anyKeyDown) {

                if (Input.GetKeyDown(charHolder)) {

                } else {

                    print("Wrong Letter");

                }
                print(charHolder);

            }

            yield return null;
        }
    }
    print("Word Typing Ended");
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
WerdaFerk
  • 57
  • 1
  • 8

2 Answers2

2

yield return does not stop the loop from running, it just (in essence) pauses it, and return the current value. Once you resume, you are still in the loop, and as it's a while(true) loop, you're gonna stay there so the statement i++ is indeed theoretically unreachable. Also, and you'll never get to print("Word Typing Ended"); if the array has any elements.

You can exit the while loop using a traditional break statement, or if you basically want to end the execution of the enumerator you can also use yield break.


Aside from that, yield should be used to simplify returning a complex collections of object, where getting the elements might include some overhead, or where the elements are dependent on previous state, but that by itself, should be relatively side-effect free. I'm pretty sure that using yield return null to process an existing array is a misuse of the feature.

Try to explain what you actually want done, there might be a solution that does not require yielding.

Community
  • 1
  • 1
SWeko
  • 30,434
  • 10
  • 71
  • 106
  • 2
    Minor correction: the `print("Word Typing Ended");` **is** reachable: in the scenario where the array is zero length. It is the `i++` that is unreachable,. – Marc Gravell Dec 16 '15 at 09:24
  • 2
    Re the collections comment: `yield return` has been used - fairly successfully - as a mechanism to implement co-routines in several frameworks; I suspect that is what the OP is doing here (i.e. implementing code to work inside one of those frameworks). – Marc Gravell Dec 16 '15 at 09:26
  • Right, sorry I didn't include that. i++ is indeed unreachable. So the issue still stands that I need to get out of that while loop to get to my iterator. – WerdaFerk Dec 16 '15 at 09:29
  • @MarcGravell, Yes, the name of the method is a hint for that :) – SWeko Dec 16 '15 at 09:29
  • I was told that using a co-routine is a way to make my program stop and wait for user input, before continuing through the loop. Otherwise it flies through the for loop and my if statements never get executed unless the button on the keyboard is being held down repeatedly. – WerdaFerk Dec 16 '15 at 09:33
  • @WerdaFerk well, who told you that, and in what context? Most of the time, you **don't want** to make a program stop - a hot `while (true)` loop is a *very bad* way to wait for user input... – Marc Gravell Dec 16 '15 at 09:45
  • Someone from another forum, lol. I go there when I don't want to bother the pro's with my low-level questions :). But when something goes wrong I come here for better insight. So, I'm assuming I should just remove the for loop altogether, and find some other way to cycle through my char array while pausing for user input. Or maybe I should nest switch statements inside my for loop? Or will that not work? – WerdaFerk Dec 16 '15 at 10:06
2

When the right key is pressed, I need the while loop to stop and go back to the for loop and start over.

so... continue; ?

But when I yield return, it exits both loops and I get Unreachable Code Detected.

no, yield return does not exit any loops. Execution will resume exactly where it was.

The reason you are seeing "unreachable code detected" is because of the while(true) that never exits, so it knows that i++ is never reached. If you add a break; inside the while (true): it will work correctly. You could also add a yield break; to exit the entire enumerator, but that will still mean that the i++ is never reached, and will flag as unreachable.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900