How to add custom console commands

Introduction

This is a quick tutorial demonstrating how to add custom commands to the console.

Adding the function to Doom 3

Open up src/game/gamesys/SysCmds.cpp (source file) . The largest thing to note is near the bottom:

void idGameLocal::InitConsoleCommands( void ) {

Inside here, there are many calls to cmdSystem->AddCommand. Pick a line, and add one of your own.

cmdSystem->AddCommand( "testcommand", Cmd_testcommand, CMD_FL_GAME, "test command" );

The parameters are as follows:

cmdSystem->AddCommand( <name of command to be used in the Doom 3 console>, <the name of the C++ function to be called>, <Various flags>, <description of command>

The two exampled flags in the file are CMD_FL_GAME and CMD_FL_CHEAT. Use CMD_FL_GAME for everything, and bitwise OR ( | ) it with CMD_FL_CHEAT for anything that allows the player to cheat . If anyone knows any more about these flags, please update the wiki!

Writing the function

Now, scroll up a bit, and add your own function.

void Cmd_testcommand ( const idCmdArgs &args ) {
 
 common->Printf("This is an example!");
 
 }

Arguments to functions are counted with args.Argc() and referenced using args.Argv();

void Cmd_testcommand ( const idCmdArgs &args) {
 
 common->Printf(args.Argc());
 common->Printf(args.Argv( 1 );
 
 }