Still don't understand how for loop works

private void ManageState() {
    var nextStates = state.GetNextStates();

    for (int index = 0; index < nextStates.Length; index++)
    {
        if (Input.GetKeyDown(KeyCode.Alpha1 + index)) 
        {
            state = nextStates[index];
            textBlock.text = state.GetStoryState();
        }
    }
}

So this is our code.
At the very first loop, index = 0. Since we’re at starting state, the length of the array is 1(or 0?), therefore 0<1.
Then we can only press “1” key, which will be the only key to react and set our “state” variable to 0, which is the first and only option. (key 1)

Then I am completely lost and felt lost during that lecture.

So I have a couple of questions of my own.

  1. Lets say the length of the array is 1, so it has 2 elements in it.
    nextStates.Length equals to what? the amount of elements within the state or the array length of the state?

  2. We get to our next state, what is our index equals to? 1 or 0?
    I am confused because Rick said every time we loop through the “for”, index is set to 0… so in my mind what I understood was because we wrote the code where it updates every frame, this means ManageState() method is called every frame? which leads to index being continuously set to 0?

I still have more questions to ask but they depend on your answers so I’ll wait.

1 Like

Hi Nikolas,

Lets say the length of the array is 1, so it has 2 elements in it.

If the length of the array is 1, it has 1 element in it. The element is at index 0. If the length is 2, you have two elements in it. The first element is at index 0, the second one at index 1. If the length of the array is n, the first element is at index 0, and the last one at index (n - 1).

We get to our next state, what is our index equals to? 1 or 0?

It depends on what value index has. This changes during each iteration step. It starts with 0 because you defined it this way in the header of the for loop: int index = 0. When the code block of the loop has been executed. , index gets increased by 1. Then the header gets evaluated again. If (index < nextStates.Length), the code block gets executed, then index gets increased by 1. And so on. The loop is a little program.

this means ManageState() method is called every frame? which leads to index being continuously set to 0?

That’s right. Whenever the for loop gets executed, it starts at the beginning. The index variable gets created and initialised with 0. Then the program does its job.

I still have more questions to ask but they depend on your answers so I’ll wait.

Just ask! :slight_smile:

1 Like

It depends on what value index has. This changes during each iteration step. It starts with 0 because you defined it this way in the header of the for loop: int index = 0 . When the code block of the loop has been executed. , index gets increased by 1. Then the header gets evaluated again. If (index < nextStates.Length) , the code block gets executed, then index gets increased by 1. And so on. The loop is a little program.

I still don’t understand, can you please explain to me how the loop works step by step? I am confident that if you do that, then I will understand how this for loop works and it will answer all my questions. :slight_smile:
If possible explain to me how it works when we start from “Introduction” state which has only 1 option, and when we press 1 it takes us to the next state which is the first scene, that has 2 choices.

Thank you in advance!

1 Like

Given we have 3 State objects in our nextStates array, the length of the array is 3. Does that make sense so far?

If so, take a look at this table:

// SCRIPTABLE OBJECT: A
// index         object of type State
// ----------------------------------------
// [0]           Introduction scene
// [1]           Dining room scene
// [2]           Bed room scene

The program executes the for loop. It reads the header. index is created and set to 0. Then it evaluates if index < nextStates.Length. It is true because nextStates.Length is 3. The code block gets executed. index gets increased by 1.

In the second iteration step, the condition in the for loop header gets evaluated again. Since index is 1, index < nextStates.Length is still true. The code block gets executed again. index gets increased by 1.

In the third iteration step, the condition in the for loop header gets evaluated again. Since index is 2, index < nextStates.Length is still true. The code block gets executed again. index gets increased by 1.

In the forth iteration step, the condition in the for loop header gets evaluated again. Since index is 3, index < nextStates.Length is false. The code block does not get executed. Instead, the for loop program gets terminated.

When you change the State, you still have an array. Unity does not care about the names of the State objects. It works with the array. The next time, the loop gets executed, the array might be a different one but still an array. The code still works as described above.

// SCRIPTABLE OBJECT: B
// index         object of type State
// ----------------------------------------
// [0]           Bath room scene
// [1]           Swimming pool scene
// [2]           Television scene
// [3]           Class room scene

The nextStates array of object B has a length of 4.


See also:

1 Like

Okay thank you! now I understand.
So just to line it up…
Since the for loop is called every frame, this means that the index is set to 0, the loop iterates itself and until the given condition turns false and terminates itself until the next frame? So in the next frame it does the same thing over again and over again every frame until I press a button (that works) which takes me to the next state with a higher or lower array length?

1 Like

That’s correct. :slight_smile:

1 Like

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

Privacy & Terms