From OpenTTD
Jump to: navigation, search

This document explains how to add console functions.


the first step

the first thing you ll need if you want to do anything within the console is

include "console.h"

this gives you the ability to access all IConsole functions and structures you will have to use if you are intending to add Commands/Functions/Variables to the Console.

creating the commands c++ function

static _iconsole_var * MyLittleConsoleFunction(byte argc, byte* argv[], byte argt[]) {
	if (argc<2) return NULL;
	// i am a dummy :D
	return NULL;
parameter meaning
argc the count of parameters the parser overloaded to the function
argv the parameters itself

parameters passed directly from out of the console have the type ICONSOLE_VAR_UNKNOWN by default. ICONSOLE_VAR_UNKNOWN is an string but it could contain an integer or something like that. it depends on what way the c++ function is handling it. if someone has used something like this:

]printf "%i" *temp_uint_16

the first parameter is passed as ICONSOLE_VAR_UNKNOWN and the second one is passed as ICONSOLE_VAR_UINT16. only variables which were passed to the commands have the ability to be in an other type as ICONSOLE_VAR_UNKNOWN.

using variable references

if someone has passed a variable by its reference:

]dosomething @*temp_uint_16
did something with temp_uint_16

the type of this first parameter will be ICONSOLE_VAR_REFERENCE. example how to write dosomething command:

static _iconsole_var * IConsoleStdLibRandom(byte argc, byte* argv[], byte argt[]) {
    _iconsole_var * item;
    item = (_iconsole_var *) argv[1];
    IConsolePrintF("did something with %s",item->name);
return NULL;

returning a value within your function

static _iconsole_var * IConsoleStdLibRandom(byte argc, byte* argv[], byte argt[]) {
_iconsole_var * result;
result = IConsoleVarAlloc(ICONSOLE_VAR_UINT16);
return result;
_iconsole_var * IConsoleVarAlloc(byte type);

used to allocate some memory for an _iconsole_var structure.

void IConsoleVarSetValue(_iconsole_var * var, int value);

used to easyly set a value of an _iconsole_var structure

void IConsoleVarSetString(_iconsole_var * var, byte * string);

IConsoleVarSetString is used to assign a string to the value

registering the command

somewhere in your code you should add a line like this ... this should be done during the initalization of openttd: [review the first example]


this registers your command as an console command named mylittlefunction. you also can add one c++ function with different console command names... just give it a try... if everything went fine you should see the following:

Personal tools