For loop in the game loop

Hi, I’ve got a question about how this for loop is executed in the game loop. Sorry for the following message, it might be confusing, but I’ll give some context, so it would be easier to understand my question:
Every game has a game loop, which is executed step-by-step, line by line every frame. In unity it’s the Update method. So if we have 60 fps, update is called 60 times per second and is executed from the first till the last line. The game does not stop on the if statement waiting for the player to press the button, it just checks whether it was pressed in this particular frame, right? So if I understand this conception correctly, then for loop is run 3 times per frame (if for instance we have 3 elements in the array), and the game checks for the key code to be received from hardware in that frame. If it wasn’t, the execution just moves on. Is there a slight possibility, that I pressed key 2 in frame 43, but the for loop is on it’s 3rd iteration in frame 43, and it missed my key 2 pressed and checks for the key 3 already? Is the input method is somehow executed in parallel, or it remembers what keys were pressed in previous frames and executes them in following frames?

Second question is easier, do I understand correctly that this piece of code will not work if I want player to press E, or W, or Enter keys? They have different codes that can’t be “reached” by just incrementing key 1 code? What would be your proposal for implementing such input? I’m thinking only about if else statements, but that would cause the bug to return.

Thank you!

Hi Phil,

Hypothetically, yes, but in reality you will never be able to type, or, hold a key down and then change to another key faster than the frame’s execution so this really shouldn’t be a noticeable issue.

The KeyCode is an enum, the values of each item relate to the ASCII decimal value of the keyboard key. In order to “reach” a character with a higher number, you’d have to increase the range of the for loop, this obviously wouldn’t work if the value of the key was lower that Alpha1, as such you’d also then need to increase the range again but starting with a KeyCode that had a lower value than Alpha1.

If you are trying to allow for any character to be pressed you would probably be better of checking to see if any key was pressed, then if it was, which one, and then compare that to what you were expecting. That process would require the use of if statements. Alternatively I suppose you could iterate through the for loop 256 times and check for every character but that would be somewhat inefficient.

There are other, more complicated, ways to achieve what you are trying to achieve which could allow for the detection of only the specific keys that the specific state was using, iterating through only the specific number of options that were available within that state, but as I say, its some what more complicated than the solution provided in this introduction section - where you already have a bit of an issue having the options which are available mashed in with the story text.

Hope this helps :slight_smile:

1 Like

Privacy & Terms