Questions on For Loop & INDEX - Lesson 32

Hi LundresTaldre,

Welcome to our community! :slight_smile:

What exactly is your question? Please do not post screenshots of texts because they are very hard to read, and it is impossible to copy and paste something from them.

Hi Nina,

How is it that the for loop/if function is able to allow me to press the Alpha1 or Alpha2 key, though the INDEX has been added up to the amount of states within currentState (in this example there are 3 elements in the array within currentState).

So, what i want to know is due to the for loop and the if function being listed in the UPDATE function, they are being called upon every frame, so given that the ManageStates function is called upon every frame, would INDEX not always be the highest version of itself, depending on the length of the array within currentState?

How am i able to still press Alpha1, Alpha2, or Alpha2 and be taken to each respective state though INDEX would always equal 2 (because ManageStates is called upon every frame and a computer operating at 60fps woudl immediately run through the 3rd iteration of the for loop in a second).

I know the for loop resets once it is taken to a new state…but what i’m not understanding is when the for loop stops on the 3rd iteration (in this example) and INDEX is equal to 2…If i pressed Alpha2 wouldn’t it be entering a keycode of 4? or if i pressed alpha3 wouldn’t that be a keycode of 5? Both 4 and 5 would be outside the array bounds, but the error doesn’t show up in the console.

I know my questions are probably hard to interpret, and i apologize, as its hard for me to express. Thank you for any help you can provide.

The for-loop gets “reset” each frame because each frame, the Update method gets executed. In C#, everything inside a method gets destroyed when the code block was executed unless you stored the data outside the code block. For example: currentState will not get destroyed because the variable is an instance variable, not a local variable. It gets destroyed when the instance gets destroyed.

I’m not sure if I understood your question correctly but we have a for-loop in the Update method. The value of index starts at 0 and increments in each iteration step until the condition, index < nextStates.Length, becomes false. In each iteration step, the if-condition gets checked. The for-loop program gets executed each frame because Update gets executed each frame. If you have a framerate of 60, the for-loop will run 60 times per second.

index will not always equal 2.


When you press 2 (Alpha2), the if-statement gets evaluated to true in the second iteration step of the for-loop in this frame because Alpha1 + 1 = Alpha2.

Press the ctrl key in VS and click on KeyCode. A long list with all KeyCode enum items will get opened. Scroll down to Alpha1. I’m sure Rick’s concept will become clear when you see that. :slight_smile:

No worries. I’m trying my best to understand what you mean and to answer your questions if I can. As long as you are also patient with me, I don’t mind explaining things multiple times.

Hi Nina, after reading an explanation about this from a user named Rob, i’m still confused on how/when the for-loop gets iterated – how is it that when I press Alpha1 the for-loop knows that i’m trying to go to array[0] (room1) within the state and not array[1] (room 2), or array[2] (room 3)?

since the for-loop is within the ManageState(), and ManageState() is within the Update(), the for-loop is being called every frame…which would mean that the for-loop is constantly trying to be iterated and index is constantly being added to (until the for-loop becomes false and index is no longer < nextStates.Length – in this case when index becomes 2 (after the 3rd iteration)?

i’m missing / not understanding how Alpha1 + index will always bring me to array[0] and not another array, could you provide any additional information on why this is the case?

Does the for-loop wait after the first iteration of index = 0 for the user to make an input of Alpha1?

That’s exactly what is happening.

Check the header of your for-loop. The first value of index is 0 because index = 0. In the first iteration, array[index] translates to array[0] because index == 0.

No, it does not wait. You press the 1 (Alpha1) in frame 267. Update gets executed. The for-loop program runs. It “sees” that you pressed Alpha1, it executes the code, then it continues. The for-loop ends at some points. Update completes executing its code-block. Done.

In frame 268, the same happens again. If you didn’t press Alpha1 in that frame, the for-loop will not execute the code which requires Alpha1 to be pressed. But the rest of the code will be executed.

Do you know the proverb “Even a blind squirrel will find an acorn once in a while”? The same is happening in our code. The for-loop is the blind squirrel. It performs the same tasks over and over again until the acorn/Alpha1 key lands right before its feet.

Thank you, Nina!

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

Privacy & Terms