Documentation/Programming Articles/Engine Console

From NeoAxis 3D Engine Wiki

Jump to: navigation, search
Go to higher level
Engine Console

Contents

Introduction

The console of the NeoAxis Engine is a convenient way of receiving debug information and entering commands. The console will provide you significant aid in developing you application.

To call the console you have to press the ~ key. Pressing this key for the second time will make the console disappear.

You can retrieve the list of available commands by typing commands.

Using the console in your application

Console code and API are defined by the EngineConsole stored in the GameCommon.dll file. Now you will examine the methods of this class.

Output to console

You can output string values to the console. For this purpose the Print method is used.

EngineConsole.Instance.Print( "Hello Console!" );
Output of a string value to console

You can also highlight text with color. In this case the Print method also receives color value.

EngineConsole.Instance.Print( "Hello Console!", new ColorValue( 1, 0, 0 ) );
Highlighting text with color

Adding new commands

You can add your own commands to the console using the AddCommand method EngineConsole class. To create a command you have to specify its name and the method this command calls.

EngineConsole.Instance.AddCommand("my_command", MyCommand);

The method implementing should receive a single string value parameter. This string will contain the symbols following the command name. I.e. a console command looks like the following: command_name arguments.

In the example below you can see a method printing a sum of two given arguments to the console.

public void MyCommand(string arguments)
{
    //CONVERTING an argument string into a string array.
    char[] splitter = new char[1] {' '};
    string[] argumentsArray = arguments.Split(splitter);
 
    //Checking the number of arguments.
    if (argumentsArray.Length < 2)
        return;
 
    //Attempting to convert string variables into numeric
    int arg1, arg2;
    if (!int.TryParse(argumentsArray[0], out arg1) || !int.TryParse(argumentsArray[1], out arg2))
        return;
 
    //Outputting the result to console
    EngineConsole.Instance.Print((arg1 + arg2).ToString());
}

Now, compile and start the project. Type: "my_command 2 2", and you will get 4!

Using your own command

Calling a command from code

You can also call a console command directly from the application. To do this use the ExecuteString method that executes a command without the user having to enter it.

EngineConsole.Instance.ExecuteString( "my_command" );