How does the loop work conceptually?

image
EDIT: Nevermind I realize the if statement occurs before and after the index is added to therefore making it possible to trigger the if statement even when it is 0 and since the index only goes up to the amount of nextStates there are then nothing will happen if the wrong key is pressed. And since it loops through each iteration per frame then resets, that means we can press any key and will be valid for each of those iterations. Correct?

I’ve seen some posts here but I still do not understand. When the for statement is active it creates an integer called ‘index’ and if it is lesser than the amount of nexStates available then it will add 1 to the for statement until it is null? If so then when exactly does the ‘if’ statement occur? when the loop is null or when it is looping before it becomes null? And when it does trigger then the if statement checks if I am pressing the corresponding key number plus the index (Like 1+2 is 3 so it only checks for 3?)
or does it check for the key number and the index number?
And even if the ‘if’ statement is true does it change the current amount of available states to choose from to the amount of the index? if so is it the current index amount from the for loop or the current index amount from the ‘if’ statement?

And wouldn’t make it so it will eventually stop at the maximum amount of nextStates available therefore making the very last option the only one that can be selected??

Hi Xarce,

Thank you for your question. I hope I understood everything correctly.

That’s exactly what’s happening. GetKeyDown returns true for the entire frame in which we pressed the key down. That’s enough time for our code to check a bunch of keycodes with the GetKeyDown method.

I’m not sure if I understood you correctly but our exit condition is defined in the for-loop, and the instruction per iteration step is on the right side. The exit condition gets checked. If true, the loop program gets terminated. If false, the code block gets executed, then index gets incremented by 1. In the next iteration step, the exit condition gets checked again. If true, the loop program gets terminated. If false, the code block gets executed, then index gets incremented by 1. And so on.

Since we are dealing with integers, we do not have null here unless nextStates is null, which would throw a NullReferenceException error, though.

Press the Ctrl key, keep it pressed, then click on KeyCode. A long list with all KeyCode items will open. Scroll down to Alpha1. You’ll find a value behind Alpha1. That value gets used in our code. I currently don’t remember which value Alpha1 has but assuming it’s 47, our if-condition would check 47+0, 47+1, 47+2, etc. In the list, you’ll see that Alpha2 is 48. Alpha3 is 49, and so on. We are exploiting the nature of enums here.

For further information on enums, please see here:
https://www.dotnetperls.com/enum

No, it doesn’t. GetKeyDown just evaluates the KeyCode we are passing on and returns either true or false. It does not override anything. We also do not manipulate the nextStates array in the if code block. We just assign one of the elements from the array to a variable.

The for-loop does not know anything about the content of the nextStates array. All it does is checking a condition and increments a variable. And the if-statement only checks the value returned by GetKeyDown. It does not know anything about the nextStates array either.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms