keycode.Alpha1 + index is throwing me off

Ok so its going to be a bit hard for me to explain because i cant tell if i already know what is and how to use for loops or the button keys are throwing me off, or i just dont understand anything at all.
so in the for loop Rick is explaining something that seems mathematically chunky with words and it simply does not work. im trying to understand where the numbers for the index are changing and how the keys are working so ill just make due with what i understand now and get feedback on that. So right now i understand that the purpose of the forloop is to control our button press depending on the length of our array. the index starts off at zero making it true because our array length is 1. Then we run the code being if we push 1 then two things happen and this is where im completely thrown off. keycode.Alpha1 + index meaning the only key right now you can press is one and now the second loop comes in and adds 1 to the index. the array in the next state is = 1 and index is = 1 which in return should make the loop stop because 1 < 1 is false soooo yeah. thats as far as my understanding goes right now. rick says something like “The loop always makes the index start at zero” or something like that i really dont understand the whole iteration part but yes. also at first i thought keycode.Alpha1 + index was adding to the index but instead it adds to the key press number/button? idk i feel so close yet so far and i cant tell if it was just poorly explained and rushed or im slow but help would be appreaciated.

Hi Jayy,

The for-loop is a little program. You define the control variable, its initial value, the condition and how you want to increase it in each iteration step.

Whenever the for-loop program gets started, it starts with the initial values.

Variables are containers for data. You can reuse them wherever you want because C# does not care about the meaning of names and human logic. It’s common to use a for-loop to iterate over something and to do something with the control variable inside the loop block.

In our case, we add the value of the control variable to Alpha1. In the first iteration step, the condition becomes (Alpha1 + 0), in the second one (Alpha1 + 1), in the third one (Alpha1 + 2), and so on.

If you cannot understand the aforementioned condition, click on KeyCode in Visual Studio, then press the F12 key. You should now see a list with all KeyCode elements, and behind the KeyCodes, you should see a value.

for(int index = 0; index < array.length; index++)
{
       if(Input.GetKeyDown(KeyCode.Alpha1 + index)
      {
         //go to next state
      }
}

How it looks visually in update

for(int index = 0; index < array.length; index++)
{
       if(Input.GetKeyDown(KeyCode.Alpha1 + 0) //key 1
      {
         //go to next state
      }
}

for(int index = 0; index < array.length; index++)
{
       if(Input.GetKeyDown(KeyCode.Alpha1 + 1) //key 2
      {
         //go to next state
      }
}

for(int index = 0; index < array.length; index++)
{
       if(Input.GetKeyDown(KeyCode.Alpha1 + 2) //key 3
      {
         //go to next state
      }
}

i figured it out but forgot to post solve my apologies.
trying to understand a concept through words is a lot harder than actually being able to see whats happening but maybe thats just me. One thing is tho know that i know how to use it im not too sure what for besides controls like these.

trying to understand a concept through words is a lot harder than actually being able to see whats happening but maybe thats just me.

I feel the same. That’s why I suggested to click on KeyCode and to press F12. When you see the list you instantly know why adding the numbers behind Alpha1 results in a different KeyCode value.

You’ll probably never use THIS example again. However, the control variable in a for loop (here: index) is commonly used to read from or write to arrays or Lists or other collections.

Let me give you an example. I’m creating an array with names, and I want to log these names into my console with a message: “Hello” plus the name. Instead of hard-coding the greetings, I iterate over the array and log a message into the console.

// A lot of names
string[] names = { "Ben", "Rick", "David", "Marc", "Tim", "ABC", "Kitty", "John", "Bird", "Blah" };

for (int i = 0; i < names.Length; i++)
{
    Debug.Log("Hello " + names[i]);
}

These four lines of code will work for all names in the names array even if you add more names.

In the next example, I’m replacing the names in the array with their respective first letter. I’m overriding the elements in the array at index i.

// A lot of names
string[] names = { "Ben", "Rick", "David", "Marc", "Tim", "ABC", "Kitty", "John", "Bird", "Blah" };

for (int i = 0; i < names.Length; i++)
{
    Debug.Log(names[i]);
    names[i] = names[i][0].ToString();
    Debug.Log(names[i]);
}

Of course, these are fairly silly examples. However, imagine you had a varying number of enemies instead of names, and you wanted to do something with the enemies. In this case, a for-loop will become very handy.

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

Privacy & Terms