How to Console Commands?

Hello guys,

TLDR: how to make console commands/cheatcodes

I am working on a game in which one of player’s task is operating computer terminal (very similar to just cmd.exe) with different commands. My problem lies at the very base - how to manage these commands?
The way it goes is: player types something inside InputField → script checks if ‘typedString’ is inside Some Commands List → checks command arguments → processes command.

Problem I have is how to create Some Commands List? It can’t be just list or array of strings, I need some way to put arguments inside, if player , for example, types “color(50, 40, 30)”, script needs to not only check if there is a command ‘color’ but also what arguments this command has and accepts.

I was thinking about structs or maybe JSON file but still, I don’t know how exactly this should be done

Thanks in advance

That rather depends on how complex the commands are that the user can ‘execute’, and how complex the responses will be (for example, if a command later in a sequence of commands can access the result of previous commands, or if the user can define names/variables, etc.)

In cases like this, it usually pays to define the entire ‘language’ before you look at implementing it. If you can formalize it in any way (I’m a fan of EBNF, other may advocate other notations), you are already 90% there.
Once you have a good definition of your language/command codes, you’ll usually build a small state machine (a.k.a. FSM), and transition betwen the sates accoring to the commands entered by the player. If the complexity warrants it, you may want to build a small scanner/parser, but usually that’s just showing off.

csofranz, thanks for the answer.
I will look at EBNF and how to work with it (never done similar things before), any advices? Your suggestion seems like exactly what I am looking for.
Btw I’m also waiting for more other responses

Speaking of consoles, I just happened to notice this asset on the store yesterday:

I haven’t bought it yet, so I can’t recommend it or not, but it looks like it makes it easy to wire-up any number of your own methods into instant console commands. It’s on my wish list, anyway.

Edit: The link that I posted above has expired so I updated it with the current version. At this point I can say that I have integrated Quantum Console in my current project and it’s very useful and easy to use.

That looks really slick!

It sounds like you’re wanting to use a Dictionary where the key is an enum type and it stores events that you can then invoke. You convert the command that the user enters into an enum type that you then look up in the dictionary.

Let’s say you can give some simple commands of the form , like ‘open door’, ‘attack dog’. ‘eat apple’, ‘format disk’, with perhaps some refinements that you can apply the verb to multiple objects like ‘reload phaser, torpedos’. You need to identify how the workds are separated (here blanks and commas), and you can - with this simple example - derive the verb (command) from the position in the line; and know that all objects come after the verb.

So imagine you have a line entered by the user, you can then use some readily availabe methods to break down the line into ‘atoms’ that you can analyse by how they appear in sequence:

string text = "take apple";
char[] delimiters= { ' ', ',' };
string[] words = text.Split(delimiters);
// results in:
// words[0] is "take"
// words[1] is "apple"

The word array now contains all and in the same sequence as they appeared in the string.

You now know (because that is how you defined the ‘command language’) that words[0] is the command, and any further items in the words array are the objects that the user wants to apply the command on. Use a switch statement on the command, and you are all set for a small, simple command interpreter.

Obvious refinements are: remove all whitespace, convert to all upper or lowercase to remove case sensitivity, remove/convert diacriticals etc. Above example may have a hickup with parsing ‘take apple, pear’ because of the two delimiters ‘,’ and ’ ’ following directly after another (it depends on how Split() is implemented when it should add an empty string).
Now, as an exercise to the smart student: write a command interpreter that can correctly parse “take apple” AND ‘take 5 apples’ and add the correct number of apples to the inventory

Won’t even take half an hour :slight_smile:

1 Like

Okay, thank you guys for all the responses.
I came up with a pretty simple system in which player’s input is processed by few methods (namely: ParseCommand → RunCommand) and then command is executed in given command method (for example, void Color()). ParseCommand splits input string into cmdString[ ] where [0] is command and [1] are arguments. Then RunCommand compares cmdString[0] with CommandsList (which is filled automatically, not by me however I migh change it to enum if command’s count will start to be overwhelming or too complicated) and after finding right command scripts run given command method (as I said earlier).
Might seem crude but works for me, very likely that I’ll upgrade this hole system (for example, method that parses and interprets arguments)