How did our for loop do what it did?

I seem to be struggling with the concept of for loops and how, in this lecture, it did what it did.

Could someone try to take a crack at explaining for loops and how it helped us fix the bug in our CYOA game? I didn’t get Rick’s explanation and when I asked the teaching assistant in Udemy, their explanation didn’t clear things up either.

I understand the different elements and the syntax of the for loop: it declares and initializes a variable, it establishes conditions, if the condition is true it executes the code block then increases by one and cycles around again until the condition is no longer being met and essentially terminates the loop. But that’s the extent of my understanding. I don’t see how the loop we made allowed us to influence key commands and fix the bug and such.

Maybe I’m trying to understand more than I’m expected to at this time, but I’m afraid if I continue with the course without fully understanding this lecture, I’ll be setting myself up for further confusion…

Thank you in advance to anyone that can perhaps explain this concept in a way my brain might be able to internalize!

1 Like

Here’s what I think is happening. Though I’m not positive myself The method we’re calling is Input.GetKeyDown(). That’s looking for a key being pressed. My guess is that it translates KeyCode.Alpha1 to the integer ‘1’. So if we’re on the second loop, then Input.GetKeyDown (KeyCode.Alpha1 + index) becomes Input.GetKeyDown (1 + 1) registering the ‘2’ key as being pressed. And since the loop is running multiple times a second it’s sure to catch the player’s input at the right time at least once.

Or, at least that’s how I’m making sense of it.

1 Like

I agree the explanation in the video is lacking. According to the original code KeyCode.Alpha1 is a keypress of “1” and KeyCode.Alpha2 is a keypress of “2”, etc.

How is KeyCode.Alpha1 + 0 now equal to a keypress of “1” and KeyCode.Alpha1 + 1 now equal to a keypress of “2”?? I thought KeyCode.Alpha2 was a keypress of “2”. Does it somehow add the index value to the number at the end of KeyCode.Alpha1? Such that KeyCode.Alpha1 + 1 = KeyCode.Alpha(1 +1) = KeyCode.Alpha2 ?

This needs some explanation.

I came here to ask this question, wondering how we would know what to expect for non-numeric KeyCodes, for example:

if (Input.GetKeyDown(KeyCode.Return + index))

What is 1 + “enter key”?
What is 2 + “E”? Is it an ascii translation?

Hi Andrew,

KeyCode is an enum, each item has a value. If you hover over Alpha1 in your code, you’ll see this;

image

These values map to their ascii decimal value (https://www.ascii-code.com/);

Because these values are integers, you can add to them, so if I was to execute this code;

Debug.Log(KeyCode.Alpha1 + 1);

…I would see “Alpha2” output to the console. What you have there is the value 49 + 1 which is 50, if you then look at the ascii chart above you’ll see that the decimal value 50 maps to “2” on the keyboard.

The increments are being handled by the for loop.

Each time the loop iterates the value has been incremented by 1. It is reasonably tight as a solution because it is looking at the size of the array you have for your options, so it will only iterate this many times. Where it isn’t so great as a solution is that it has no relation to the options you just type in to the story text.

So, if we assume you have three states, thus, three options, in your story text you’ll have numbered these 1, 2 and 3.

for(int index = 0; index < nextStates.Length; index++)

in the above, we initialise index to be zero, so on the first iteration you add zero to the value of KeyCode.Alpha1, so 49 + 0 = 49. Thus, still “1” as a keyboard character/input.

if (Input.GetKeyDown(KeyCode.Alpha1 + index))

On the second iteration, index is now 1, so its 49 + 1 = 50, which is KeyCode.Alpha2, thus “2” as a keyboard character/input.

On the third iteration, index is now 2, so its 49 + 2 = 51, which is KeyCode.Alpha3, thus “3” as a keyboard character/input.

At this point the index is no longer less than the length of the nextStates array so the for loop exits.

Hope this helps :slight_smile:

4 Likes

Hi Anthony,

You wouldn’t be able to use the existing solution as-is to support the enter key or a “E” for example, hopefully my reply above to Andrew explains why.

You would need to build something more robust for the use of additional characters that do not follow on in the sequence.

Hope this helps :slight_smile:

1 Like

ok so thats what i was thinking but…its simply still doesnt add up. like my game works fine but it isnt mentioned anywhere how both key presses are being used? if 50 is equal to the key press of 2 then how are we still able to use key press 1? but thats the least confusing part of it all. How does our index variable go back to being 0 if its adding up to 1 every time its ran? this is my main confusion

1 Like

Honestly the problem is that we are trying to learn a concept without actually being able to see whats happening. like a teacher teaching math without doing the math in front of you. They just do that math in their head and then explain it. It would have been clearer if we debugged and printed more to understand how the loop is running through the update and how the index breaks and restarts aka “loop”

Privacy & Terms