HOW TO: Add custom console commands
Step 1: Create a new class that inherits from the
xGameConsoleXNA.BaseCommands class and call it
CustomCommands.
Step 2: Create an override for the
OnConnect method that also calls the base objects
OnConnect method.
public override void OnConnect(xGameConsoleXNA.CommandManager Manager, xGameConsoleXNA.CommandExtenderManager ExtenderManager)
{
// be sure to call the base OnConnect method
base.OnConnect(Manager, ExtenderManager);
}
Step 3: Add a "quit" command after the base.OnConnect statement
// add a quit command
DoAddCommand("quit", new xGameConsoleXNA.CommandCallback(this.DoQuit));
Step 4: Create a new method in the class called DoQuit
private string DoQuit(string[] args, bool usedAsFunction)
{
// exit the app.
this.game.Exit();
// return an empty string
return string.Empty;
}
The DoQuit method will be called when the user types “quit” into the console. The only thing the DoQuit method does is call the Game.Exit method that will intern quit the game. That’s all there is to adding your own commands.
Your code should look something like this…
public class CustomCommands : xGameConsoleXNA.BaseCommands
{
private Game1 game;
public CustomCommands(Game1 g, xGameConsoleXNA.Console console) :
base(console)
{
this.game = g;
}
#region " Commands ... "
[Category("Example"), Description("Quits the applicaion/game.")]
private string DoQuit(string[] args, bool usedAsFunction)
{
// exit the app.
this.game.Exit();
// return an empty string
return string.Empty;
}
#endregion
public override void OnConnect(xGameConsoleXNA.CommandManager Manager, _
xGameConsoleXNA.CommandExtenderManager ExtenderManager)
{
// be sire to call the base OnConnect method
base.OnConnect(Manager, ExtenderManager);
// add a quit command
DoAddCommand("quit", new xGameConsoleXNA.CommandCallback(this.DoQuit));
}
}