# Console-type interface in game

I am currently programming a very simple 2d game in C# with XNA.

I was wondering how to implement a console-type interface in a game or graphical software like there is in Skyrim or Counter-strike or some many more games?

These "consoles" can allow you through various command to change many parameters directly in-game.

I find this very interesting for debugging purposes and I was hoping to implement something similar in my program. Is there a particular way to achieve this?

The graphical side should be easy, so i'll omit that.

You have to be able to parse your input properly. If you want to go all out you can read on compiler theory, though i don't think this is what you're looking for.

In general what you want to be able to do is lexical analysis of your input. This means being able to take a string and output tokens. Tokens are objects that say what a particular text is, so you'll have an "id" object for variables or an "operator" object, etc. It doesn't have to be so "official", but you want to be able to tell what each part of the string does/is. Then, you'd build a syntax tree, maybe do syntax analysis to check for errors and put that through your handling code. If you were hardcore, that is. I don't think this is what you're looking for, so i'll just suggest that for now you try coding an FSM and look for key words.

For example, you could just read a line one word at a time (you could tokenize the string and put each token in a list, it's the same thing, but safer) and for the first word check if it's a key word like "bind" or "modify" using if or switch statements. Then in each if/case statement you'd just have the code that handles this command.

So a string "modify fov 90" would create a list of 3 tokens "modify", "fov" and "90" and in if( stringlist[0] == "modify" ) you could have more if statements or you could have a global map that takes a string and returns a pointer to the fov variable (or the variable itself) and you'd stringtofloat the 3rd token and use that to set fov. Just don't forget to error check everything, like if stringtofloat returns an error or if you enter this "if" and the stringlist has only 2 elements, etc.

If you want a robust parser, there are a few availible, for example http://www.antlr.org/. The site looks like any terribly boring IT site, but the library is easy to use, just i don't think you actually need that.

• +1 for a comprehensive response, but it's a lot simpler to take advantage of the various scripting and REPL tools freely available. Building a parser/compiler/interpreter is a very valuable learning exercise, though. – 3Dave Jul 14 '12 at 15:21

Check out the performance monitoring sample, It contains a nice console that you can easily use. (Just take the console class and manager). It contains some nice code to register commands, have arguments, and alot of stuff.

http://create.msdn.com/en-US/education/catalog/sample/performance_sample

Also, This may be abit overkill, But I personally use Neoforce Controls, Which has a nice UI library, including a Console.

https://neoforce.codeplex.com/

What is your actual problem? Did you encounter any errors? Your question is pretty much on par with the "how do I maek gaem?" ones. First, try to do it yourself. When you get to any errors, come here or to Stackoverflow.

You can take a look at how I did the following in Python: https://github.com/Yannbane/Pysole.

Generally, I'd suggest you make a Console class. This class accepts user input (you'll have to be listening to it), and then translates what has been written to a function name, and then executes the function passing anything written after the name as an argument. You can have a dictionary look-up, that's what I did.

I suggest you also implement a Command class. That class would store the function to be executed, the function's name from the console's perspective (the thing you need to write for the function to be executed), number of arguments it gets, a description, and so on. These commands can be added to the console, and I strongly suggest that you don't hardcode them, but provide a way for the user (yourself, in this case) to console.addCommand them (or just push them to an array).

It's usually wise to have the console generate a help command by its own, which lists all the known commands and their descriptions. All other commands should be user supplied.

All that is written or outputted (as text) also needs to be stored inside the console in a, say, .text variable. Each frame, you just render all that text and add a background to it. Alternatively you can use some kind of markup to identify the color of a word and so on...

Other built-in commands you can have:

• clear
• description [command]
• nargs [command]
• hide (don't forget to provide another way of bringing it up once it's hidden, such as a keypress)

General suggestions:

• You will probably want to re-use this, so don't make it dependent on your game. The key of software design is code separation, so it become re-usable.

• It would be great if the console knew how to draw itself, but provided a way to set things up (font, background, color) to the user, this way you achieve the above point and don't have to recode it each time.

• Having a history array is very useful.

This answer is vague and from my experience, there are really tonnes of ways of doing this, I believe.

I did this awhile back by embedding IronPython, and redirecting standard in/out/error to a DrawableGameComponent class that maintained a list of strings for display, command history, etc with standard SpriteBatch textcommands, and a "hotkey" to toggle the control's Visible property. It's pretty easy to expose all of the classes and objects in your XNA project so that you can tweak things at run time, provides scripting, classes, etc.

Also, never underestimate the power of the Visual Studio command window. (though I think that's only available in VS Pro or better). For simple debugging, I added a case to my keyboard handler to call

Debugger.Break();


When I needed to stop at some point in execution, then used the command window to make changes to game state.