Programming
Fundamentals
Setting Up Development Environment
UnigineScript
High-Level Systems
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
Rendering-Related Classes

Unigine::Console Class

Header:#include <UnigineConsole.h>

Controls console-related parameters.

Adding Console Command with Several Arguments

Console class can be used to create custom user console commands with different number of arguments. This section provides an example on how to create a custom console command with several arguments.

Prior Knowledge
It is supposed that you have already created an empty C++ project by using Unigine SDK Browser.

In the example below, we perform the following actions:

  • Define and implement AppWorldLogicinstance methods for console commands.
  • Get the console instance (which has a singleton implementation) and add a new command.
Adding Instance Methods

In the AppWorldLogic.h header file create new methods. In this example we define 3 methods: one as a callback for Console command and another two methods for actions depending on the number of arguments.

Inside the already generated AppWorldLogic class, define the following methods:

Source code (C++)
/* ... */
class AppWorldLogic : public Unigine::WorldLogic {
	
public:
	AppWorldLogic();
	/* other methods */
	virtual int restore(const Unigine::StreamPtr &stream);

private:
	// choose the method
	void choose_command(int argc, char **argv);
	// perform action if there is 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's no console arguments.
  • action_one_arg() is called if an argument was passed.

In the AppWorldLogic.cpp file implement these methods. We added the following code to existing AppWorldLogic class methods:

Source code (C++)
// check the number of arguments and call the necessary 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's 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 argcandargvare used to get thearguments count and arguments vector.

Notice
The first element of argv always keeps the name of console command. Thus, argc is always >= 1 and to get the first passed argument, you should use argv[1].
Adding Custom Console Command

As all the methods above were correctly implemented, add the following code in the AppWorldLogic.cpp. Add custom command by using addCommand() function. By adding this code into AppWorldLogic::init() function, the engine adds a new console command on AppWorldLogic class instance initialization.

Source code (C++)
#include "UnigineConsole.h"
#include "UnigineCallback.h"

/* ... */

int AppWorldLogic::init() {
	// get the existing singleton Console instance and add a command
	Unigine::Console::get()->addCommand(
		"console_action",
		"Performs custom console action",
		Unigine::MakeCallback(this, &AppWorldLogic::choose_command)
	);

	return 1;
}

/* ... */
Running Sample

After building the project, run it and open the console.

Write recently creating command and see the result:

Output
#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

Console Class

Members


Console * get()

void setActivity(int activity)

Opens or closes the console. By default the console is closed.

Arguments

  • int activity - 1 to make the console active (opened); otherwise, 0.

int getActivity()

Returns a value indicating if the console is opened or closed.

Return value

Returns 1 if the console is active (opened); otherwise, 0.

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.

int isCommand(const char * name)

Returns a value indicating if a command with a given name exists.

Arguments

  • const char * name - Name of the command.

Return value

Returns 1 if the command with a given name exists; otherwise, 0.

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 - Name of the variable.
  • float value - Float value of the variable.

float getFloat(const char * name)

Returns a float value of a given variable.

Arguments

  • const char * name - Name of the variable.

Return value

Float value of the variable.

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)

Returns an integer value of a given variable.

Arguments

  • const char * name - Name of the variable.

Return value

Integer value of the variable.

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()

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.

void setPrompt(const char * str)

Updates the console prompt. The default prompt is Unigine~#.

Arguments

  • const char * str - New console prompt.

void setString(const char * name, const char * value)

Sets a string value for a given variable.

Arguments

  • const char * name - Name of the variable.
  • const char * value - String value of the variable.

const char * getString(const char * name)

Returns the string value of a given variable.

Arguments

  • const char * name - Name of the variable.

Return value

String value of the variable.

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.

int isVariable(const char * name)

Returns a value indicating if a variable with a given name exists.

Arguments

  • const char * name - Name of the variable.

Return value

Returns 1 if the variable exists; otherwise, 0.

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.

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

Returns 1 if the custom command is removed successfully; otherwise, 0.

void run(const char * command)

Runs a console command.

Arguments

  • const char * command - Console command with arguments, a string.
Last update: 2017-07-03