Programming commands

Started by
3 comments, last by dirmp 8 years, 9 months ago
Good night, all right?
I am new to Unity, despite having already completed some basic projects, and I am thinking about the best way to organize the code I use to commands, for example, when you press the key "i", open inventory, among others...
The way I'm doing it: I put almost all codes in a script within the CharacterController, but not sure if I'm doing the right way... For example, were various functions within the Update ():

if(Input.GetKeyDown(KeyCode.I))
{
//Open inventory
}

if(Input.GetKeyDown(KeyCode.Escape))
{
//Close the application
}
And there are many commands as well, I've thought about putting this script into an empty gameObject so I can import into any scene. And one more question: So, each frame is scanned all if/else to find some valid to run, but does not use a lot of memory? Or is there another more correct way to use scripts for each keyboard command?
Thank you!!
Advertisement

It is fairly common to have a key mapper. This allows players to customize the keys in use, and also works with a state machine to ensure only the valid keys are processed.

So you can detect if you are in the main game, and then map a variable key (which by default you mapped to "i") to an event that triggers the inventory UI. Elsewhere in your game, or in unity's built-in setup tool, allow the player to map the key to anything else, perhaps they like the "p" key or "\" key because it is similar to what they've got in another game.

Similarly, ESC could be mapped to many different events based on the state of the game. On some screens it could close the dialog, inside some transactions dialogs it could cancel the transaction, inside the main game it could pop up the menu, inside some menus it could back out one level or prompt to quit the game if at the root level.

Edit: Here's a link to Unity's Input Mapper class, if you need it. Again, note that instead of looking for a specific KeyCode value you would name a button, perhaps one called "Inventory" mapped to 'i' by default. You would test it with Input.GetButtonDown("Inventory").

It is fairly common to have a key mapper. This allows players to customize the keys in use, and also works with a state machine to ensure only the valid keys are processed.

So you can detect if you are in the main game, and then map a variable key (which by default you mapped to "i") to an event that triggers the inventory UI. Elsewhere in your game, or in unity's built-in setup tool, allow the player to map the key to anything else, perhaps they like the "p" key or "\" key because it is similar to what they've got in another game.

Similarly, ESC could be mapped to many different events based on the state of the game. On some screens it could close the dialog, inside some transactions dialogs it could cancel the transaction, inside the main game it could pop up the menu, inside some menus it could back out one level or prompt to quit the game if at the root level.

Edit: Here's a link to Unity's Input Mapper class, if you need it. Again, note that instead of looking for a specific KeyCode value you would name a button, perhaps one called "Inventory" mapped to 'i' by default. You would test it with Input.GetButtonDown("Inventory").

Ok, I got it, thanks for the tip .. I will find out more about key mapper! But on my other questions, I keep using the same algorithm I'm using?

Every update do a big switch statement that jumps between each of your game modes, in each game mode check for the button down status, and trigger the events as necessary.

If you're in the main menus, generate the menu events and broadcast them. If you're in the main game play, generate those events, if you're in the pause menu, generate those events. If you are in some other mode, maybe a dialog mode or interactive movie mode or qte event mode, generate and broadcast those appropriate events.

Ok, thank you for the answer !! Was of great help ... Then share the final result!

Greetings!

This topic is closed to new replies.

Advertisement