Trouble understanding state = nextState[index];

I kind of understand how the for loop works thanks to all the questions asked before, but I really cannot understand state = nextState[index];
So the code runs like this:
for (int index = 0; index < nextStates.Length, index++)
{
if (Input.GetKeydown(Keycode.Alpha1 + index))
{
state = nextState[index];
}
For example, If I have three choices, then the index will probably be 2 (I dont really know).If I want to choose the first option and press 1, how does that take me to the outcome of the first option instead of the third option?
I know that this is not clearly explained, but any reply would be highly appreciated

I have understood your Question pretty well I think. I guess to make things much simpler for everyone on a general basis the instructors haven’t dived deep, or should I say they couldn’t dive deep.

Before we begin, Keep the following image in mind:

1

If you want to understand the true concept behind then we need to go back to understanding first how KeyCode’s in unity actually work. Once you understand that you will be able to grasp why it works.

Let me explain your confusion first:

You are taking into the fact that keyboard KeyCode 1 equivalents to index 1 in array and hence at this point, Keycode.Alpha1 + index it becomes 2 and hence according to your understanding if we press 1 then it should take me to State 3 since state = nextState[index]; // Evaluates to nextState[2] as per your current understanding

I hope I got that right.

Now let understand the True CONCEPT. For that I would like you to keep visualizing the image attached above.

How are KeyCode’s in Unity Mapped.

Alpha0 = 48,
Alpha1 = 49,
Alpha2 = 50,
Alpha3 = 51,
Alpha4 = 52,
Alpha5 = 53,
Alpha6 = 54,
Alpha7 = 55,
Alpha8 = 56,
Alpha9 = 57,

KeyCode.Alpha these are nothing but in the backend they have static values mapped as given in the above table. KeyCode is basically what we call in enum in OOPs. You can google what is an enum if you’re interested but not required, you can go with the analogy that KeyCode.Alpha represents a static which the compiler understands that which key was pressed. All keys on a keyboard are mapped to a value.

You may now ask why the value starts with 48, well that’s because its the ASCII representation of the numeric character ‘1’. Mind it that I have called it as a character and not the number itself. All inputs received by a program / software / languages or anything in general in taken in the form of characters and strings which needs to be converted into numeric and followed by hex & binary for our computer to interpret instructions on a system level.

I hope this much is clear. Now let’s dive into the Actually working of the code. If you remember on Unity we created multiple state objects / entities and then set the next states on it. Have a look:

Consider StartingState as your first state. So when we call state.GetNextStates() we are essentially getting an Array of two elements only and we are looping through them. [Check Image 1]

Now lets understand the if section

if (Input.GetKeydown(Keycode.Alpha1 + index))

Initially in the loop when index = 0

So the if condition becomes:

if (Input.GetKeydown(Keycode.Alpha1 + 0))

which internally translates to 49+0 = 49 [Check the Table above] and this value is returned to GetKeyDown

if (Input.GetKeydown(49))

Input.GetKeyDown() ->Checks and returns a boolean value if Key was pressed which in our case Key 1 was pressed.

So only when Key 1 is pressed the condition becomes true then the following is executed

state = nextState[index] This stores the next state which is State 2 [Refer the above unity diagram and first diagram] and hence new current state is set of State 2 or second choice that is the next screen which is reachable from state one.

Now in the loop when index = 1,

The if condition becomes:

if (Input.GetKeydown(Keycode.Alpha1 + 1))

which internally translates to 49+1 = 50 which is basically the value for Numeric Key 2 on your keyboard [Check the Table above] and this value is returned to GetKeyDown

if (Input.GetKeydown(50))

Input.GetKeyDown() ->Checks and returns a boolean value if Key 2 was pressed which in our case if Key 2 was pressed.

So only when Key 2 is pressed the condition becomes true then the following is executed

state = nextState[index] This stores the next state which is State 3 [Refer the above unity diagram and first diagram] and hence new current state is set to State 3 or third choice that is the next screen which is reachable from State 2.

This entire process repeats for every single state in the current scene and all checks are done in every single frame of the game

I hope this really clears out your confusion.

If you take my personal opinion, Understanding states and identifying individual independent pluggable components as Data / States objects is a little confusing. But in the course we are actually following MVC design pattern. I am not entirely sure but I believe the entire unity engine and game building process revolves around this concept. If you want you can google for it but if it seems confusing leave it for later.

I would like @Rick_Davidson or someone’s from the gamedev team to check if this explanation was appropriate.

4 Likes

Thank you SO much to take the time to write this explanation. I was really lost in this section of the course but you are a lifesaver. I REALLY appreciate you taking your time to write this explanation.

Thanks again!

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

Privacy & Terms