# Input/Output console window in XNA

I am currently making a simple game in XNA but am at a point where testing various aspect gets a bit tricky, especially when you have to wait till you have 1000 score to see if your animation is playing correctly etc. Of course i could just edit the starting variable in the code before I launched but I have recently been interested in trying to implement a console style window which can print out values and take input to alter public variables during run-time.

I am aware that VS has the immediate window which achieves a similar thing but i would prefer mine is an actual part of the game with the intention that the user may have limited access to it in the future.

Some of the key things i have yet to find an answer to after looking around for a while are:

• how i would support free text entry
• how i would access variables during runtime
• how i would edit these variable

I have also read about using a property grid from windows form aps (and partially reflection) which looked like it could simplify a lot of things but i am not sure how I would get that running inside my XNA game window or how i would get it to not look out of place (as the visual aspect of is seems to be aimed just for development time viewing).

All in all I'm quite open to any suggestions on how to approach this task as currently I'm not sure where to start. Thanks in advance.

I use XNAGameConsole.

It was written for XNA 3.1, but I updated it to XNA 4.0. You can find those changes here:

You could also simply use AllocConsole.

AllocConsole is a Win32 Windows native function. In short, the AllocConsole method allows you to allocate a standard Windows console to the calling process. Once you have done this, you can use standard C# console input/output within your application using Console.ReadLine(), Console.WriteLine(), etc.

To use this method you need to use using System.Runtime.InteropServices;

Shortened Example:

using System.Runtime.InteropServices;

[DllImport("kernel32")]
static extern bool AllocConsole();

private void Form1_Load(object sender, EventArgs e)
{
AllocConsole();
Console.WriteLine("Hello Console!");
}


More info regarding the native function is here: MSDN

• I took a look at the link you sent me but cant figure out how to add it to my project properly. Would you be able to explain what the site is trying to tell me in a bit better detail? Dec 7 '12 at 7:20
• @WillBagley Please see edits to the answer. Hope this helps. Dec 7 '12 at 15:21
• Thanks for the edit, i was able to get it working fine. Just wondering if there is any way to embed the console window into my game window or is it stuck as a standalone console? Dec 9 '12 at 23:34

All of the below is assuming Windows development.

1. Right click your game in the solution explorer and click properties.
2. Click the application tab.
3. Change the Output Type to Console Application.

You would have to write a command parser or use an existing one. Your code that takes in the command will have to parse it and then perform some action that you have given it. I've used this project for command paring and found it very useful. I don't think there is any automated way to change the variables.

You could add a scripting engine that can change your variables on the fly and contains an immediate mode. There are implementations of LUA and other scripting languages that are portable to XNA.

Not to self advertise but I had this same problem so I've recently added a JavaScript interpreter to XNA Its not 100% bug free, but you can take it for a spin and then port it to your game. I think it's exactly what you need. http://roy-t.nl/index.php/2012/06/05/using-javascript-as-a-script-engine-in-xnac/

A very simple approach to this is displaying the desired variables as text on the game screen, using SpriteBatch.DrawString(). Of course, this does not readily allow for input. You can, however implement an in-game console if you want to give the player access to your commands.