This documentation will guide you through creating a new class for scripts, inside the game code.
Creating source files
The best practice is for each class to have their own source file.
Let's call our class ExampleObject
, derived from SimpleEntity
. inside the code/fgame
, create 2 source files:
exampleobject.h
for the header
exampleobject.cpp
for the source
Preparing the header file
The header file will contain the class prototype and will use CLASS_PROTOTYPE(classname)
to define the information about the class for scripts to use it. Sample code for the class:
#include "simpleentity.h"
CLASS_PROTOTYPE(ExampleObject);
public:
ExampleObject();
~ExampleObject();
};
Definition simpleentity.h:42
Declaring the class in the source file
The next step is to declare the class. The class declaration defines the information for the class, such as the parent class, the class name, the class id (which is an alternate name) and the list of events that the class supports.
#include "exampleobject.h"
#include "g_main.h"
CLASS_DECLARATION(
SimpleEntity, ExampleObject,
"info_exampleobject")
{
{NULL, NULL}
};
ExampleObject::ExampleObject()
{
gi.Printf("Hello, world!\n");
}
ExampleObject::~ExampleObject()
{
gi.Printf("I'm being deleted!\n");
}
Creating script commands for the class
Each class has a response list, containing a list of Event
followed by the associated method for each. This is the constructor for Event
, inside code/qcommon/listener.h
:
Event(
const char *command,
int flags,
const char * formatspec, cons
char *argument_names,
const char *documentation,
byte type = EV_NORMAL);
Definition listener.h:246
Let's tweak ExampleObject prototype inside exampleobject.h
to add a test method.
#include "simpleentity.h"
{
CLASS_PROTOTYPE(ExampleObject);
public:
ExampleObject();
~ExampleObject();
void TestMethod(
Event *ev);
};
In exampleobject.cpp
, instantiate a new Event
and link it in the response list of the ExampleObject class:
#include "exampleobject.h"
Event EV_ExampleObject_TestMethod
(
"test_method",
EV_DEFAULT,
"i",
"num_to_print",
"This is a test method.",
EV_NORMAL
);
CLASS_DECLARATION(
SimpleEntity, ExampleObject,
"info_exampleobject")
{
{&EV_ExampleObject_TestMethod, &ExampleObject::TestMethod},
{NULL, NULL}
};
ExampleObject::ExampleObject()
{
gi.Printf("Hello, world!\n");
}
ExampleObject::~ExampleObject()
{
gi.Printf("I'm being deleted!\n");
}
void ExampleObject::TestMethod(
Event *ev)
{
int value;
value = ev->GetInteger(1);
gi.Printf("TestMethod called! With value: %d\n", value);
}
Testing the class
Time to test this class.
- Create a folder
tests
inside the main
folder of the game.
- Create a file
test_exampleobject.scr
with the following content:
main:
local.ent = spawn ExampleObject
local.ent test_method 3
local.ent delete
end
- Start the game
- In the main menu, open the console and type the following commands to enable cheats:
set cheats 1
set thereisnomonkey 1
- Start a map:
set g_gametype 1;devmap dm/mohdm6
- In the console, type:
testthread tests/test_exampleobject.scr
You should see some stuff being printed into console.