Getting Started

This article will guide you through the first steps of how to use OpiConsole and will teach you how to implement it within your own XNA applications.

Download OpiConsole

  1. Click here to download the latest version of OpiConsole and extract the contents to a location of your choice.

Adding the reference

  1. While in Visual Studio, right click your project and select 'Add Reference...'.
  2. Select the browse tab and find the OpiConsole.dll in the build/x86 or build/Xbox360 folders depending on whether this is a Xbox or Windows XNA project.

Adding the console to your Game

Create the console

public Game1()
{
     graphics = new GraphicsDeviceManager(this);
     Content.RootDirectory = "Content";

     //Create our console
     m_pConsole = new OpiConsole.Console(this, spriteBatch, this);      
}

Setup and add the component to the game

protected override void Initialize()
{
     //Setup The Console
     m_pConsole.Enabled = true;
     m_pConsole.DrawConsole = true;
     m_pConsole.ShowDebug = true;

     //Add the Console as a component
     this.Components.Add(m_pConsole);

     base.Initialize();
}

Adding custom commands

Creating the command

    public class Command_Add : Command
    {
        public Command_Add()
            : base("add", "Adds two numbers (Usage: a b)", UsageLevel.NORMAL)
        {
        }

        public override bool Invoke(List<string> args)
        {
            if (args.Count == 2)
            {
                int output= Parser.toInt(args[0]) + Parser.toInt(args[1]);
                m_pConsole.Print("Answer: " + output.ToString());
                return true;
            }

            return false;
        }
    }

Register the command

protected override void LoadContent()
{
     //Create our Commands
     Command cmd = new Command_Add();
     m_pConsole.RegisterCommand(cmd);
}

Creating the cvar

    public class CVar_Language : CVar
    {
        public CVar_Language() : base("Language", "English", CVarType.STRING, UsageLevel.NORMAL, true) { }

        public override bool Invoke(List<string> args)
        {
            if (args.Count < 1 || args.Count > 1)
                return false;

            return setValue(args);
        }
    }

Register the cvar

protected override void LoadContent()
{
     //Create our CVars
     CVar_Language cvar = new CVar_Language();
     m_pConsole.RegisterCVar(cvar);
}

Enjoy!

This guide should have gotten the console up and running within your game, if you have any questions please post them in the discussions section or check the FAQs.

Last edited Feb 12, 2010 at 4:16 PM by opdude, version 11

Comments

No comments yet.