OpenTTDDevBlackBook/IConsole/AddFunctions

From OpenTTD
Jump to: navigation, search
External Links

OpenTTD GitHub
Contributing to OpenTTD - guidelines
OpenTTD Doxygen

General Reference

Coding style
Compiling OpenTTD
Debugging
Add a setting
Add a squirrel function
Understanding the SaveGame handler
Bumping the savegame version
Doing an OpenTTD release

Language and Strings

Format of langfiles
Using OpenTTD strings
List of special strings

Window System

Using the window system
Colour codes that exist in OpenTTD
Adding a text box
Understanding the widget focus system
GUI style guide

Multiplayer

The OpenTTD TCP protocol
The OpenTTD UDP protocol
Debugging desyncs
Server Admin Port development

Ingame Console

The console window
Console commands
Console variables
Using console scripting
Adding functions/commands to the console
Adding variables to the console
Console development history

Content APIs (modding frameworks)

Graphics and similar (NewGRF)
AI framework (NoAI)
GameScript framework (NoGO)

Other Reference

Map array (landscape grid)
Vehicles
Pathfinding
Train acceleration


This document explains how to add console functions.

Contents

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
argt the parameters types (ICONSOLE_VAR_UNKNOWN, ICONSOLE_VAR_BYTE, ICONSOLE_VAR_STRING, ...)

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
31291

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[]) {
if (argt[1]==ICONSOLE_VAR_REFERENCE) {
    _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);
IConsoleVarSetValue(result,rand());
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]

IConsoleCmdRegister("mylittlefunction",MyLittleConsoleFunction);

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:

]list_cmds
echo
echof
printf
mylittlefunction
Personal tools