The question is answered, right answer was accepted
Buggy While loop/Coroutine, which never truly ends?
I'm working on a classic JRPG, beginning with the combat, as that is by far the most tedious and complex mechanically, so after I finish this, the rest should be a breeze. However, I've ran into an issue that I personally find so complex and illogical, and one that I have next to no idea of how to describe.
Basically, in the combat, I'm currently working on a basic attack function, which should only skip the turn currently. I have it set up so a button on the UI, when activated, calls a function to the CombatDirector, which then finds out who's turn it is, and then proceeds with that character's specified action. As it stands, the attack function should work like this:
1. Character turn begins, UI is enabled and player presses the now enabled Attack button.
2. UI disables, game waits for keyboard input. This is where I'll later implement target selection.
3. After input is given, which right now is given through a simple press of the space bar, the turn is over, and the CombatDirector goes onto the next turn.
4. Rinse and repeat.
Simple enough, correct? And as it stands, that works perfectly. However. As soon as the first turn is over, the second turn character has zero need to press the "Attack" button, and instead, I can simply progress the turn again by pressing the space button. This isn't the case with the first turn, as spamming the space bar before I press the Attack button has zero effect. This already defies my code, as input is being checked for by a while loop that disables itself upon first input, and I know the loop disables due to the debug message I hooked up to said loop no longer coming through. I don't need to press the UI button, I can just spam my space bar and the game keeps on going, skipping through turns as much as I press it. This leads me to believe that SOMEHOW, my original While loop never truly ends, despite every sign pointing to it having ended. I have tried everything, from more Debug.Log 's, to forcing the Coroutine housing the While loop to close, to numerous break; statements, but nothing seems to work. I have a feeling that I might have missed something simple, but I am clueless, and I can't find anything on the subject matter. Here is my code, as well as some bonus screenshots of the console, which shows some even more confusing message orders.
public void StartAllyAttack () { // This function is called by CombatDirector's AllyAttack() function, which is called via button.
director.ui.gameObject.SetActive(false);
StartCoroutine(AllyAttack());
}
IEnumerator AllyAttack () {
Debug.Log("IENumerator START");
bool choosingTarget = true;
while (choosingTarget) {
Debug.Log("RUNNING LOOP");
if (Input.GetKeyDown(KeyCode.Space)) {
Debug.Log(gameObject.name + " ATTACKED");
choosingTarget = false;
director.AdvanceTurn();
}
yield return null; // Pauses execution until next frame, to prevent infinite runs in one frame, and a crash.
}
Debug.Log("IENumerator END REACHED");
}
I hit Start, wait for my first turn, and hit the Attack button. Everything goes to plan. This is what the console looks like:
After that, without pressing the Attack button or anything, I do nothing else other than press the space bar one more time, and the console turns into this:
Notice the second send of the "IENumerator START" and initial "RUNNING LOOP" messages. (Ignore the main "RUNNING LOOP" spam number changing, these were screenshots from two different plays.) This confuses me majorly, as the coroutine only starts once the UI button is hit, or at least it should only start then.
Don't mind this edit, just bumping this thread up. Bump. Bump.
Answer by AndrewSLeake85 · Sep 12, 2018 at 06:08 AM
There isn't nearly enough context here to solve your issue unfortunately. The only thing I can guess from the screenshots and code provided is that hitting the spacebar is registering as a click on the button that calls AllyAttack(), thus starting it over again.
...That was literally the only problem. And it was literally the only thing I didn't think of. Welp, rookie mistake, I feel like an idiot now, but thanks for the help! That saved me hours of headache.
Follow this Question
Related Questions
How to change a variable used in a loop for the outside ? 1 Answer
Unity crashes when using while loop 0 Answers
,Crashing in do while loop when it should be exiting 0 Answers
Which one to use for, while or something else? 1 Answer
What is the best way to loop with very short and accurate interval? 0 Answers