Unigine::Console Class
Header: | #include <UnigineConsole.h> |
Controls console-related parameters.
Onscreen Console Overlay#
By default the console overlay is disabled. To make it output console messages to the application screen it should be enabled. You can adjust the overlay’s behavior and appearance as well as specify the messages that will be printed exclusively to the onscreen overlay and will not be written to the console.
You can customize the console font by using the setFontSize(), setMessageColor(), setWarningColor(), and setErrorColor() methods. See the following example:
#include "AppWorldLogic.h"
#include <UnigineConsole.h>
int AppWorldLogic::init()
{
// enable the onscreen overlay
Unigine::Console::setOnscreen(true);
// increase the size of the console's font
Unigine::Console::setFontSize(20);
// change the color for different types of messages
Unigine::Console::setMessageColor(Unigine::Math::vec4::GREEN);
Unigine::Console::setWarningColor(Unigine::Math::vec4::RED);
Unigine::Console::setErrorColor(Unigine::Math::vec4::BLUE);
// send messages to the console
Unigine::Log::message("Message!\n");
Unigine::Log::warning("Warning!\n");
Unigine::Log::error("Error!\n");
return 1;
}
Adding Console Command with Several Arguments#
The Console class can be used to create custom user console commands with a different number of arguments. This section provides an example of how to create a custom console command with several arguments.
In the example below, we perform the following actions:
- Define and implement AppWorldLogic instance methods for console commands.
- Get the console instance (which has a singleton implementation) and add a new command.
1. Adding Instance Methods
In this example, we define three methods in the AppWorldLogic.h header file: one as a callback for a console command and another two methods for actions depending on the number of arguments:
// AppWorldLogic.h
/* ... */
class AppWorldLogic : public Unigine::WorldLogic {
public:
AppWorldLogic();
/* ... */
private:
// choose the method
void choose_command(int argc, char **argv);
// perform an action if there are no arguments
void action_no_args();
// perform another action if an argument was passed
void action_one_arg(const char *s);
};
/* ... */
- choose_command() selects the appropriate method.
- action_no_args() is called if there are no console arguments.
- action_one_arg() is called if an argument was passed.
In the AppWorldLogic.cpp file implement recently defined methods:
// AppWorldLogic.cpp
// check the number of arguments and call the corresponding method
void AppWorldLogic::choose_command(int argc, char **argv) {
for (int i = 0; i < argc; i++) {
Unigine::Log::message("arg[%d]: %s\n", i, argv[i]);
}
// note: the first element of argv is the name of console command
if (argc == 1) {
action_no_args();
}
else if (argc == 2) {
action_one_arg(argv[1]);
}
// for more arguments:
//else if (...) {
// // etc
//}
}
// write the message into console, if there are no arguments
void AppWorldLogic::action_no_args() {
Unigine::Log::message("first action! no arguments!\n");
}
// write the message into console, if an argument was passed
void AppWorldLogic::action_one_arg(const char *s) {
Unigine::Log::message("second action! the argument is:%s \n", s);
}
Arguments argc and argv are used to get the arguments count and arguments vector.
2. Adding Custom Console Command
Add custom command to the AppWorldLogic.cpp file by using addCommand() function. By adding this code into AppWorldLogic::init() function, the engine adds a new console command on AppWorldLogic class instance initialization.
// AppWorldLogic.cpp
#include "UnigineConsole.h"
#include "UnigineCallback.h"
/* ... */
int AppWorldLogic::init() {
// get the existing singleton Console instance and add a command
Unigine::Console::addCommand(
"console_action",
"Performs custom console action",
Unigine::MakeCallback(this, &AppWorldLogic::choose_command)
);
return 1;
}
/* ... */
3. Running Sample
After building the project, run it and open the console. Write recently created command to see the result:
#if you write "console_action"
arg[0]: console_action
first action! no arguments!
#if you write "console_action arg"
arg[0]: console action
arg[1]: arg
second action! the argument is:arg
To remove the added console command, use removeCommand() method.
See Also#
- C++ API sample located in the folder <UnigineSDK>/source/samples/Api/Systems/Console
- C# API sample located in the folder <UnigineSDK>/source/csharp/samples/Api/Systems/Console
- HowTo video on Printing User Messages to Console with C#
Console Class
Enums
LEVEL#
Name | Description |
---|---|
LEVEL_NORMAL = 0 | An ordinary message. |
LEVEL_WARNING = 1 | A warning. |
LEVEL_ERROR = 2 | An error message. |
Members
void setActive ( bool active ) #
Opens or closes the console. By default the console is closed.Arguments
- bool active - True to make the console active (opened); otherwise, false.
bool isActive ( ) const#
Returns a value indicating if the console is opened or closed.Return value
True if the console is active (opened); otherwise, false.void setBackgroundColor ( const Math::vec4 & color ) #
Sets a background color for the console.Arguments
- const Math::vec4 & color - Four-component vector specifying the color in the RGBA format.
void setFontSize ( int size ) #
Sets the size for the console font.Arguments
- int size - The size of the console font.
int getFontSize ( ) #
Returns the size for the console font.Return value
The size of the console font.bool isCommand ( const char * name ) const#
Returns a value indicating if a command with a given name exists.Arguments
- const char * name - The command name.
Return value
true if the command with a given name exists; otherwise, false.const char * getCommandDescription ( const char * name ) const#
Returns the description of the console command by its name. If the name isn't specified, an empty string will be returned.Arguments
- const char * name - The command name.
Return value
The command description if it exists; otherwise, an empty string.const char * getCommandName ( int num ) const#
Returns the name of the console command by its number in the array of the existing commands.Arguments
- int num - The command number.
Return value
The command name if it is found in the array of the existing commands; otherwise, an empty string.void setErrorColor ( const Math::vec4 & color ) #
Sets a color for error messages in the console.Arguments
- const Math::vec4 & color - Four-component vector specifying the color in the RGBA format.
void setFloat ( const char * name, float value ) #
Sets a float value for a given variable.Arguments
- const char * name - The variable name.
- float value - Float value of the variable.
float getFloat ( const char * name ) const#
Returns a float value of a given variable.Arguments
- const char * name - The variable name.
Return value
Float value of the variable.float getFloatMax ( const char * name ) const#
Returns a maximum float value for a given variable.Arguments
- const char * name - Variable name.
Return value
Maximum float value of the variable.float getFloatMin ( const char * name ) const#
Returns a minimum float value for a given variable.Arguments
- const char * name - Variable name.
Return value
Minimum float value of the variable.bool isFloat ( const char * name ) const#
Checks if the value set for the given console variable is of the float type.Arguments
- const char * name - The variable name.
Return value
True if the variable value is float; otherwise, false.void setInt ( const char * name, int value ) #
Sets an integer value for a given variable.Arguments
- const char * name - Name of the variable.
- int value - Integer value of the variable.
int getInt ( const char * name ) const#
Returns an integer value of a given variable.Arguments
- const char * name - Name of the variable.
Return value
Integer value of the variable.int getIntMax ( const char * name ) const#
Returns a maximum integer value for a given variable.Arguments
- const char * name - Variable name.
Return value
Maximum integer value of the variable.int getIntMin ( const char * name ) const#
Returns a minimum integer value for a given variable.Arguments
- const char * name - Variable name.
Return value
Minimum integer value of the variable.bool isInt ( const char * name ) const#
Checks if the value of the given console variable is of the integer type.Arguments
- const char * name - The variable name.
Return value
True if the variable value is int; otherwise, false.void setLock ( int lock ) #
Disables or enables the console. By default the console is enabled.Arguments
- int lock - Positive integer to disable the console; otherwise, 0.
int getLock ( ) const#
Checks if the console is disabled.Return value
1 if the console is disabled; otherwise, 0.void setMessageColor ( const Math::vec4 & color ) #
Sets a color for ordinary messages in the console.Arguments
- const Math::vec4 & color - Four-component vector specifying the color in the RGBA format.
int getNumCommands ( ) const#
Returns the number of all available console commands, including the custom ones.Return value
The number of available console commands.int getNumVariables ( ) const#
Returns the number of all available console variables.Return value
The number of available console variables.void * addOutputCallback ( Unigine::CallbackBase2< const char *, int > * func ) #
Adds a callback function that will be executed when a text is output to the console. The function is useful when you implement a custom console, for example. The callback function must receive 2 arguments:- const char *text - a text that is output to the console,
- int level - one of the LEVEL_* variables that indicate the type of the output text. The value can be important for setting a color for console messages, for example.
// the callback function
void AppWorldLogic::output_callback(const char *text, int level)
{
switch (level):
case Console::LEVEL_NORMAL:
// logic for ordinary messages
case Console::LEVEL_WARNING:
// logic for warnings
case Console::LEVEL_ERROR:
// logic for error messages
}
int AppWorldLogic::init()
{
// set the callback
Console::addOutputCallback(MakeCallback(this,&AppWorldLogic::output_callback));
return 1;
}
Arguments
- Unigine::CallbackBase2< const char *, int > * func - The callback pointer. The callback arguments must be: (const char *text, int level).
Return value
ID of the last added callback of the specified type, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeOutputCallback ( void * id ) #
Removes the specified callback from the list of output callbacks.Arguments
- void * id - Callback ID obtained when adding it.
Return value
True if the callback with the given ID was removed successfully; otherwise false.void clearOutputCallbacks ( ) #
Clears all added output callbacks.void setPrompt ( const char * prompt ) #
Updates the console prompt. The default prompt is Unigine~#.Arguments
- const char * prompt - New console prompt.
const char * getPrompt ( ) const#
Returns the console prompt.Return value
The prompt text.void setString ( const char * name, const char * value ) #
Sets a string value for a given variable.Arguments
- const char * name - The variable name.
- const char * value - String value of the variable.
const char * getString ( const char * name ) const#
Returns the string value of a given variable.Arguments
- const char * name - The variable name.
Return value
String value of the variable.bool isString ( const char * name ) const#
Checks if the value of the given console variable is of the string type.Arguments
- const char * name - The variable name.
Return value
True if the variable value is string; otherwise, false.void setTextColor ( const Math::vec4 & color ) #
Sets a common font color for the console.Arguments
- const Math::vec4 & color - Four-component vector specifying the color in the RGBA format.
bool isVariable ( const char * name ) const#
Returns a value indicating if a variable with a given name exists.Arguments
- const char * name - The variable name.
Return value
True if the variable exists; otherwise, false.const char * getVariableDescription ( const char * name ) const#
Returns the description of the console variable by its name. If the name isn't specified, an empty string will be returned.Arguments
- const char * name - The variable name.
Return value
The variable description if it exists; otherwise, an empty string.const char * getVariableName ( int num ) const#
Returns the name of the console variable by its number in the array of the existing variables.Arguments
- int num - The variable number.
Return value
The variable name if it is found in the array of the existing variables; otherwise, an empty string.void setVec2 ( const char * name, const Math::vec2 & value ) #
Sets a two component vector for the console variable.Arguments
- const char * name - Name of the variable.
- const Math::vec2 & value - Value of the variable.
Math::vec2 getVec2 ( const char * name ) const#
Returns the two component vector console variable.Arguments
- const char * name - Name of the variable.
Return value
Value of the variable.bool isVec2 ( const char * name ) const#
Returns a value indicating if the console variable is a two component vector.Arguments
- const char * name - Name of the variable.
Return value
True if the variable is a two component vector; otherwise, false.void setVec3 ( const char * name, const Math::vec3 & value ) #
Sets a three component vector for the console variable.Arguments
- const char * name - Name of the variable.
- const Math::vec3 & value - Value of the variable.
Math::vec3 getVec3 ( const char * name ) const#
Returns the three component vector console variable.Arguments
- const char * name - Name of the variable.
Return value
Value of the variable.bool isVec3 ( const char * name ) const#
Returns a value indicating if the console variable is a three component vector.Arguments
- const char * name - Name of the variable.
Return value
True if the variable is a three component vector; otherwise, false.void setVec4 ( const char * name, const Math::vec4 & value ) #
Sets a four component vector for the console variable.Arguments
- const char * name - Name of the variable.
- const Math::vec4 & value - Value of the variable.
Math::vec4 getVec4 ( const char * name ) const#
Returns the four component vector console variable.Arguments
- const char * name - Name of the variable.
Return value
Value of the variable.bool isVec4 ( const char * name ) const#
Returns a value indicating if the console variable is a four component vector.Arguments
- const char * name
Return value
True if the variable is a three component vector; otherwise, false.void setWarningColor ( const Math::vec4 & color ) #
Sets a color for warning messages in the console.Arguments
- const Math::vec4 & color - Four-component vector specifying the color in the RGBA format.
int addCommand ( const char * name, const char * desc, CallbackBase * callback ) #
Adds a custom console command bound to a given callback function.Arguments
- const char * name - Name of the new console command.
- const char * desc - Short description to be displayed in the console.
- CallbackBase * callback - The callback pointer. The callback arguments must be (int argc, char** argv,...).
Return value
1 if the custom command is added successfully; otherwise, 0.void flush ( ) #
Forces to execute all queued console commands.int removeCommand ( const char * name ) #
Removes a custom console command.Arguments
- const char * name - Name of the custom console command.
Return value
1 if the custom command has been removed successfully; otherwise, 0.void run ( const char * command ) #
Runs the specified console command.Arguments
- const char * command - A console command with arguments.
Math::vec4 getBackgroundColor ( ) const#
Returns the background color for the console.Return value
Four-component vector specifying the color in the RGBA format.Math::vec4 getMessageColor ( ) const#
Returns the color for ordinary messages in the console.Return value
Four-component vector specifying the color in the RGBA format.Math::vec4 getWarningColor ( ) const#
Returns the color for warning messages in the console.Return value
Four-component vector specifying the color in the RGBA format.Math::vec4 getErrorColor ( ) const#
Returns the color for error messages in the console.Return value
Four-component vector specifying the color in the RGBA format.Math::vec4 getTextColor ( ) const#
Returns the common font color for the console.Return value
Four-component vector specifying the color in the RGBA format.void setFontSize ( int size ) #
Returns the current font size used for console messages.Arguments
- int size - New font size to be set for console messages
int getFontSize ( ) const#
Returns the current font size used for console messages.Return value
Current font size set for console messagesvoid setHeight ( int height ) #
Sets the height of the console.Arguments
- int height - Height of the console as percentage of the window height.
int getHeight ( ) const#
Returns the console height in percentage of the window height.void setLimit ( int limit ) #
Sets the maximum number of messages the console can output.Arguments
- int limit - Maximum number of messages the console can output.
int getLimit ( ) const#
Returns the maximum number of messages the console can output. The default value is 16384.void setWrapping ( bool wrapping ) #
Enables the text wrapping to the next line.Arguments
- bool wrapping - true to set the text wrapping; otherwise, false.
bool isWrapping ( ) const#
Returns a value indicating if the text wrapping is enabled for the console.void setOnscreen ( bool onscreen ) #
Shows or hides the onscreen overlay.Arguments
- bool onscreen - true to show the overlay; otherwise, false.
bool isOnscreen ( ) const#
Returns a value indicating if the onscreen overlay is shown or hidden.Return value
True if the overlay is shown; otherwise, false.void setOnscreenFontSize ( int scale ) #
Sets the size for the onscreen overlay font.Arguments
- int scale - Size for the onscreen overlay font.
int getOnscreenFontSize ( ) const#
Returns the size of the onscreen overlay font.Return value
The size of the console font.void setOnscreenHeight ( int height ) #
Sets the height value for the onscreen overlay.Arguments
- int height - Height of the onscreen overlay in percentage of the window height.
int getOnscreenHeight ( ) const#
Returns the height for the onscreen overlay.Return value
The height of the onscreen overlay in percentage of the window height.void setOnscreenTime ( float time ) #
Sets the time during which the text is displayed on the screen.Arguments
- float time - Time in seconds.
float getOnscreenTime ( ) const#
Returns the time during which the text is displayed on the screen.Return value
The time in seconds.void write ( const char * text ) #
Writes the specified text to the console and displays it on the screen (onscreen overlay).Arguments
- const char * text - Text to output to the console.
void write ( const Math::vec4 & color, const char * text ) #
Writes the specified text to the console and displays it on the screen (onscreen overlay).Arguments
- const Math::vec4 & color - Four-component vector specifying the text color in the RGBA format.
- const char * text - Text to output to the console.
void write ( Console::LEVEL level, const char * text ) #
Writes the specified text to the console and displays it on the screen (onscreen overlay).Arguments
- Console::LEVEL level - Type of the message.
- const char * text - Text to output to the console.
static void getMessages ( Vector < String > & messages, int limit ) #
Gets the messages written to the console and the onscreen overlay.Arguments
- Vector < String > & messages - Vector to populate with messages.
- int limit - Maximum number of messages to retrieve.
void getMessages ( Vector< String > & messages, Vector< int > & levels, int limit ) const#
Arguments
- Vector< String > & messages - Vector to populate with messages.
- Vector< int > & levels - Types of messages to retrieve.
- int limit - Maximum number of messages to retrieve.
static void getWarnings ( Vector < String > & messages, int limit ) #
Gets the warning messages written to the console and the onscreen overlay.Arguments
- Vector < String > & messages - Vector to populate with messages.
- int limit - Maximum number of messages to retrieve.
static void getErrors ( Vector < String > & messages, int limit ) #
Gets the error messages written to the console and the onscreen overlay.Arguments
- Vector < String > & messages - Vector to populate with messages.
- int limit - Maximum number of messages to retrieve.
const char * getLastMessage ( ) const#
Returns the last message printed to the console.Return value
Last message printed to the console.const char * getLastWarning ( ) const#
Returns the last warning message printed to the console.Return value
Last warning message printed to the console.const char * getLastError ( ) const#
Returns the last error message printed to the console.Return value
Last error message printed to the console.static void writeLine ( const char * text ) #
Writes the text followed by the line terminator to the console and the onscreen overlay.Arguments
- const char * text - Text to output to the console.
static void writeLine ( const Math::vec4 & color, const char * text ) #
Writes the text followed by the line terminator to the console and the onscreen overlay.Arguments
- const Math::vec4 & color - Four-component vector specifying the text color in the RGBA format.
- const char * text - Text to output to the console.
static void writeLine ( Console::LEVEL level, const char * text ) #
Writes the text followed by the line terminator to the console and the onscreen overlay.Arguments
- Console::LEVEL level - Type of the message.
- const char * text - Text to output to the console.
static void onscreenWrite ( const char * text ) #
Writes the text to the onscreen overlay only.Arguments
- const char * text - Text to output to the console.
static void onscreenWrite ( const Math::vec4 color, const char * text ) #
Writes the text to the onscreen overlay only.Arguments
- const Math::vec4 color - Four-component vector specifying the text color in the RGBA format.
- const char * text - Text to output to the console.
static void onscreenWrite ( Console::LEVEL level, const char * text ) #
Writes the text to the onscreen overlay only.Arguments
- Console::LEVEL level - Type of the message.
- const char * text - Text to output to the console.
static void onscreenWriteLine ( const char * text ) #
Writes the text only to the onscreen overlay followed by the line terminator.Arguments
- const char * text - Text to output to the console.
static void onscreenWriteLine ( Math::vec4 & color, const char * text ) #
Writes the text only to the onscreen overlay followed by the line terminator.Arguments
- Math::vec4 & color - Four-component vector specifying the text color in the RGBA format.
- const char * text - Text to output to the console.
static void onscreenWriteLine ( Console::LEVEL level, const char * text ) #
Writes the text only to the onscreen overlay followed by the line terminator.Arguments
- Console::LEVEL level - Type of the message.
- const char * text - Text to output to the console.
static void message ( const Unigine:: Math::vec4 & color, const char * format, ... ) #
Writes the ordinary message to the console and the onscreen overlay.Arguments
- const Unigine:: Math::vec4 & color - Four-component vector specifying the text color in the RGBA format.
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void message ( const char * format, ... ) #
Writes the ordinary message to the console and the onscreen overlay.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void warning ( const char * format, ... ) #
Writes the warning message to the console and the onscreen overlay.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void error ( const char * format, ... ) #
Writes the error message to the console and the onscreen overlay.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void messageLine ( const Unigine:: Math::vec4 & color, const char * format, ... ) #
Writes the ordinary message to the console and the onscreen overlay followed by the line terminator.Arguments
- const Unigine:: Math::vec4 & color - Four-component vector specifying the text color in the RGBA format.
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void messageLine ( const char * format, ... ) #
Writes the ordinary message to the console and the onscreen overlay followed by the line terminator.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void warningLine ( const char * format, ... ) #
Writes the warning message to the console and the onscreen overlay followed by the line terminator.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void errorLine ( const char * format, ... ) #
Writes the error message to the console and the onscreen overlay followed by the line terminator.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void onscreenMessage ( const Unigine:: Math::vec4 & color, const char * format, ... ) #
Writes the ordinary message to the onscreen overlay only.Arguments
- const Unigine:: Math::vec4 & color - Four-component vector specifying the text color in the RGBA format.
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void onscreenMessage ( const char * format, ... ) #
Writes the ordinary message to the onscreen overlay only.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void onscreenWarning ( const char * format, ... ) #
Writes the warning message to the onscreen overlay only.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void onscreenError ( const char * format, ... ) #
Writes the error message to the onscreen overlay only.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void onscreenMessageLine ( const Unigine:: Math::vec4 & color, const char * format, ... ) #
Writes the ordinary message only to the onscreen overlay followed by the line terminator.Arguments
- const Unigine:: Math::vec4 & color - Four-component vector specifying the text color in the RGBA format.
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void onscreenMessageLine ( const char * format, ... ) #
Writes the ordinary message only to the onscreen overlay followed by the line terminator.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void onscreenWarningLine ( const char * format, ... ) #
Writes the warning message only to the onscreen overlay followed by the line terminator.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.
static void onscreenErrorLine ( const char * format, ... ) #
Writes the error message only to the onscreen overlay followed by the line terminator.Arguments
- const char * format - Formatted text.
- ... - Arguments, multiple allowed.