Externals
Externals are functions defined by the Gothic engine that can be called from scripts. Union SDK provides symbols for pointers to global zCParser
instances that we can use to interact with the parser and to define a custom external function.
Creating custom external
To create an external we need to define a function handler and register it in the parser. Before we start, it's good to write down a Daedalus function signature so we can see the return and argument types that will be important later.
Function handler
External function handler signature must:
- return
int
orbool
- use
__cdecl
calling convention (default in C++) - take no arguments
Inside the handler, we can use the global parser
to pop function arguments and push the return value from/to the stack. It's important to pop the arguments in reverse order and to pop all of them even if we are not going to use them. Similarly, the return value must always be set if any and must never be set if the function returns void
. If we don't follow the rules, the stack may get corrupted and lead to the Gothic crash.
Register external
Externals should be registered in the parser during the Game_DefineExternals
game event. We need to call parser->DefineExternal
with variadic arguments:
- external function name in Daedalus
- reference to function handler
- return type
- ...argument types
zPAR_TYPE_VOID
indicates the end of the argument types list
Available types are defined by an enum: