[SOLVED] Assignment of KeyCodes

Is a complete newb at this so if I don’t speak the correct jargon, please forgive me. This is a lot to learn and comprehend for someone totally new, especially the vocabulary and where all the bits reside in what part of the script and why. :slight_smile: While my script works fine, I have a couple of general questions hopefully to better understand what we are doing and why we are doing it.

First question I have is this. When creating each of the keycodes in the lecture for input selection, why were some of the keycode characters reused in some of the if/else if void statements rather than assigning a unique keycode for each item listed in the enumerator?

I am assuming, since the reused keycode characters are being redefined in different void statements, it doesn’t matter if the keycodes are unique to each item set forth in the enumerator since you are defining each keycode in each void statement. And, my assumption is there is some sort of consistency rule regarding the player’s input is in place for reusing the keycodes.

Second question I have, is if you give each keycode a unique character for each item in the enumorator list, couldn’t you create a state function of all your keycodes to be used in the void Update portions of the script so all keycodes are available to all the void statements? I don’t know if this is stated correctly or if my thought processes are even logical at this point.

I understand that you are declaring specific keys to be pressed in your void statements to call for a specific action to happen. But, wouldn’t it be easier to do some type of exclusion under your void statements of what should NOT be pressed from the state you initially created of all pre-fined keycodes for the game?

It seems like copying and pasting of correctly formatted items in the beginning of a script would make more sense as far as limiting error creation with all your void statements, albeit you probably would have more lines codes. Of course, you could create said list with a //note function and still copy and paste the items as well. I am just wondering if there is a way to make the script less confusing or easier to read and work with without having to redefine keycodes in each of the void states.

LOL I hope my logic makes sense. Thanks in advance for any input as I try to understand this!

Hi Morgaine,

If I understand right, you want to know:

  1. Why some reused KeyCodes work for different states
    (
    e.g.
    if (Input.GetKeyDown(KeyCode.R)) { myState = <A State>; }
    )
  2. … Sorry, I don’t understand. :confused:

First, we don’t create KeyCodes - they’re actually a predefined Enumeration of all the keys that can be pressed on most standard keyboards (even some that can’t!). When we say “KeyCode.R”, we’re not defining a new KeyCode, we’re accessing value “R” from the KeyCodes Enumeration.

MOAR INFO: https://docs.unity3d.com/ScriptReference/KeyCode.html

In Text101, “KeyCode.R” has basically been designated as the “Return” Key, so this should appear very frequently in the different state methods; imagine the confusion if we had the same instruction (“Return”) but different Keys (“R”, “T”, “Y”…)!
Which state we go to after pressing “KeyCode.R” is entirely dependent on which state we’re in.

For example, going by the Game Design Document:


If we are in the Mirror state, “R” takes us to Cell state; but if we are in the Cell_Mirror state, “R” does nothing, because we aren’t keeping track of it, or “listening for it”.

Hopefully that made sense. Now we’ll move on to “Input.GetKeyDown()”. Input.GetKeyDown() returns true if the KeyCode in the brackets is pressed in the same frame as the check is made (it differs from Input.GetKey(), which checks if the key is down at all).

MOAR INFO: https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
Addendum: The docs here say “You need to call this function from the Update function”. Actually, as long as this method is called in a function that is called from the Update function, this method will still work.

e.g. In Update, we find what state we’re in, and call a function based on this state:

if      (myState == States.cell) 		{cell();}
else if (myState == States....

If we go to the cell() method, we can still use Input.GetKeyDown(), because after we go through the method, we “return” to the update function. Then, the Update function is called on the next frame. And the next. And the next…

We decide if a button does something with:

// if "key" was pressed this frame, EXECUTE CODE.
if (Input.GetKeyDown(KeyCode key))  { /*CODE GOES HERE*/ }

where “KeyCode key” is a value from the KeyCode enumeration. (such as “KeyCode.R”, “KeyCode.Space”, “KeyCode.Alpha0”…)

For Example:

// myState = States.mirror
// if "R" was pressed this frame, Change myState to States.cell.
if (Input.GetKeyDown(KeyCode.R))  { myState = States.cell }

// myState = States.floor
// if "R" was pressed this frame, Change myState to States.corridor_0 .
if (Input.GetKeyDown(KeyCode.R))  { myState = States.corridor_0 }

// etc.

Hope this helps.

PLEASE correct me if I’m wrong - I assume I’m correct, which is always dangerous in programming…

Thanks Dean for your answer.

Being a newb and unfamiliar with all the concepts and terms, it makes things difficult to put into terms others may understand. I apologize for being unclear. :slight_smile:

My questions were poorly crafted. I understand we are choosing from a predefined value set for KeyCodes. My use of the word creating was a poor choice to use since my thought were running via we are creating (i.e. writing) if/else if statements. But, you gave me the answer I was looking for. Reusing KeyCodes (like R for example) is for consistency of the game and for the play throughout the game. It is what I thought but wanted to be sure.

While I was thinking about consistency, I was also thinking KeyCodes (R = Return to Cell, X = Return to Corridor, etc.) might be confusing for writing the script. I was thinking that like the States you could assign a different KeyCode character to each of the States and not repeat using R for Return or other characters and assign to each of the states in the public enum States.

Something similar to the following, which is a VISUAL ONLY of what I am trying to poorly convey originally. I was looking at ways to do multiple assignments with less lines of code.

if (myState == States.cell) {cell ( ) (Input.GetKeyDown(KeyCode C)); }
if (myState == States.cell_mirror) {(cell_mirror) (Input.GetKeyDown(KeyCode B)); }

LOL I didn’t say my logic was good but, at least, I am thinking about how it all works, which is a positive step for me In the past, I just looked at a script and run away screaming!

Again, thank you for taking the time to try to answer my weird questions.

Glad I could help Morgaine. :relieved:

If you really, REALLY wanted, you could probably pass the KeyCode value to the Method:

cell(KeyCode.C);
// ...

// Method declaration
void cell(KeyCode key){ // Here, key is the value you passed in above
    // ... Story and stuff

    // text.text += "\n\nTo go to <LOCATION>, press " + key: This automagically appends
    // the string to your text.
    // \n is an escape character for "newline"
    text.text += "\n\nTo go to <LOCATION>, press " + key;
    if (Input.GetKeyDown(key)){ // Use passed in KeyCode instead of hard-coded KeyCode
        myState = <TODO: STATE ASSIGNMENT>;
    }
}

This will print your story, and then tell the user to press “C” to move to ???

This COULD potentially be used to set a unique button for each method, but it could get VERY confusing for you and the player.

… also, you can pass in more than one key:

cell(KeyCode.C, KeyCode.S, KeyCode.M, KeyCode.L);
// ...

// Method declaration
void cell(KeyCode key1, KeyCode key2, KeyCode key3, KeyCode key4){ 
// Here, key1, key2, key3 and key4 are the values you passed in above
// They are C, S, M, and L respectively.
// ...
}

Thanks you very much, Dean!

Is exactly what I wished to know!

No problem, Happy to help. :smile:

Is what I like about this forum community, everyone, regardless of skill level, is welcome. And, I think we all do our best to help one another. :slight_smile: Which is a a lot to be said in comparison with other places!

Privacy & Terms