If index = 2 is Unity casting the same State again?

So, I have 3 questions actually:

  1. If I am typing 3 as my choice but there are only 2 options, for example, does that mean that Unity casts the current state again? Because in the for loop the Input condition is false, it does not enter the if statement’s block - and that means that we are not updating the state array variable (=nextState[index]) so this variable should be the same - equal to the current state. Is what I’m saying true? If so, how could we overcome this if the state contains not just text but animation / audio or something that needs to load and consumes frames or time as opposed to text and pictures. Will it cast the same state again and again after pressing number that is not in the options although it’s not just text.?

  2. In question 1 I assumed that after the user inputs some value, C# stores this input under Input type in the temporary memory and then we can have some statements on this Input value that was stored later on. Is my assumption true?

  3. The last question: is Input type waits until the user actually type in something like in other languages (Python for example)? There the program does not continue until the user enters something.

I am coming from Python programming so that’s why I asked those questions - to understand better how C# works.

Thanks, I know it’s long.

1 Like

Hi there! Welcome to the community!

  1. Yes, it keeps casting the same state. To deal with you can simply flip the If statement and the loop, I mean putting the loop inside the if statement, that would solve everything. Why Rick didn’t do it that way? Who knows, I suppose to make the loop the “main attraction” for those that are new to coding.

  2. To be honest I don’t know, C# uses Read method to read input, I have no idea how Unity does it since it has it’s own class for that, which is Input, same goes for the new input system, and since Unity tends to do some crazy (with crazy I mean convoluted) things in the background I rather not dig into that, if you do please share your discoveries, I’m actually quite curious about this but I’m just not in the mood to go and see Alice in that rabbit hole.

  3. Yes, it waits until the user types something.

Thanks for the quick answer :slight_smile:
Anyways for question 2 I did not find answer yet, if i’ll find i will share it, but what I could find is that Input is Unity’s class.
And are you sure about 3?
From my understanding if I type nothing the loop will keep on iterating until it breaks - casting the same State again. And from another perspective this little loop with the Input that runs under the Update method will keep on running and running since Unity tries to catch the user’s actions and mouse movements (I thinks that this is the Update method? like some sort of main loop?) => from that I concluded that Unity’s Input class instance stores the last value it collected in some memory and that it does not necessarily mean that the Input waits for the user to type something - cause if it had to you had to type twice on 2 to get 2 actually done - the first 2 that is typed gives false in the if statement and the second iteration gives true but you still have to type it once. Probably Input holds some None value for not typing anything and None is not equal to true so it keeps on iterating without running the if statement.
I am just having some weird assumptions that I thought of here.
Sorry for the too long answer :nerd_face:

Thanks,

1 Like

I assumed something completely different there, now that you put it that way, no, it does not wait for the player to input a value. Also yes, Update is basically a loop, it runs once per frame, there are other loops like FixedUpdate that run, as the name implies, on a fixed timer.

Here are the docs for the Read method I mentioned previously (Oh, I didn’t mention it…) and as you said it does store a value which is -1 when there’s nothing to read.

Nice to have this kind of conversations :nerd_face: Now I need a “nerd first bump” emoji.

1 Like

Yep thanks :upside_down_face:
Sometimes I get into the theories

Hi @Niv,

Have your questions been answered?


See also:

yes

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

Privacy & Terms