Skip to content

Script SDK

port/scriptsdk/include/openblood/ holds the headers scene scripts are written against:

  • engine.h, generated from the engine's headers by tools/port/script_sdk.py headers, declares the 716 engine interfaces with their own names (Zactor__Isdead, Zlink__Playanimsegment, ...), their enums and structs, MSG_<name> for every script message, and the SF service table.
  • decompiled.h, generated by the same command, is what the level scripts in src/scripts/ build against (docs/internals/scripts/scene-scripts.md): m2c's type names, the pointer-width macros (uptr, SC_POINTER_SLOT, SC_POINTER_TYPED_WORD, SC_HOST_ALIGN4), SC_FOLDED, and ScriptImports as a struct of typed function pointers.
  • script.h declares the script structs, the translator's runtime under the names the decompiler uses (the less certain names describe what the code appears to do), and macros for writing a new script in the form IOI's translator produced.

script_sdk.py headers --check fails when a generated header is out of date; tools/tests/test_script_sdk.py runs the same check.

Building new scripts

There is no build path for new scripts at the moment. The old one compiled SDK sources with ee-gcc into a MIPS module the port loaded beside the level's on its emulated CPU (script_sdk.py mod and build), and it was removed with the emulator on 24 November 2026. The port now builds each level's scripts into its executable as native code (docs/development/port/native-scripts.md). A native mod would be a library with its own creator table, loaded beside the level's and listed in front of it, so that a script of the same name replaces the original as ZScriptC::FindScript takes the first match. That loader is not written.

Writing a script

#include "openblood/script.h"

typedef struct { REF rTarget; int lCount; } Vars;           /* the script's variables */
static const SCRIPTIMPORT Properties[] = { PROPERTY(SIT_REF), END_PROPERTIES };
static void Imports(ScriptState *pSS) { READ_PROPERTY(VARIABLES(Vars)->rTarget); }

DECLARE_STATE(Alarm);

FUNCTION(PatrolRun) {
    BEGIN
    for (;;) {
        WAIT(2.0f);
        if (Zactor__Isdead(VARIABLES(Vars)->rTarget))
            GOTO_STATE(Alarm);
    }
    END_RUN
}

ROOT_STATE(Root, 0, Patrol, 0);
STATE(Patrol, Root, 2, F(PatrolRun), 0, 0, 0);
STATE(Alarm, Root, 2, 0, 0, 0, 0);
SCRIPT(M00_MyScript, Vars, Root, Properties, Imports, 0);
Macro Meaning
FUNCTION(name), FUNCTION_ARGS(name, ArgsStruct) A script function and its controller (name_fc); F(name) is its address. Inside, pSS is the script state and pFrame its frame.
BEGIN ... END_RUN The body of a state's run function. When it reaches the end it waits for messages.
BEGIN ... END The body of an enter, destroy or called function. When it reaches the end it returns.
WAIT(seconds), YIELD() Wait, then continue on the next line.
CALL(function, setup) Call a script function, which may wait, and continue when it returns. setup fills its arguments through pCallee.
GOTO_STATE(state) Change state. The states being left run their destroy, the new one its enter.
ROOT_STATE(name, enter, initial, messages) The root: runs enter, then goes to initial.
STATE(name, parent, level, run, enter, destroy, messages) A state. level is 1 for the root and one more than the parent's for each nesting; run, enter and destroy are F(...) or 0.
MESSAGES(name) ON(MSG_x, function) ... END_MESSAGES A state's message handler. function runs at once with the message's data in ARGUMENTS(MessageArgs)->pData; it may change state but not wait. Messages a state doesn't handle go to the script's handlers.
SCRIPT(name, Vars, root, properties, imports, messages) The script. Its name is what scenes use to attach it.
PROPERTY(SIT_type) ... END_PROPERTIES, READ_PROPERTY(field) The values a scene stores for each instance, read in order in imports.
VARIABLES(Vars), ARGUMENTS(Args), THIS The script's variables, the function's arguments, and the object the script runs on.

openblood/engine.h is generated from the engine's headers (script_sdk.py headers). It declares the 716 engine interfaces with their own names (Zactor__Isdead, Zlink__Playanimsegment, ...), their enums and structs, and MSG_<name> for every script message. Each call goes through ScriptImports, as the original scripts' calls do. SF is the service table (SF.SendCommand, SF.Alloc, ...).

Rules

  • Waiting: a local variable does not keep its value across WAIT, CALL or GOTO_STATE; keep such values in the script's variables. Declare locals before BEGIN.
  • One per line: use at most one WAIT, CALL or GOTO_STATE per source line, because the line number is the resume point.
  • Replacing a script: the new script must declare the original's property list exactly. The scene stores the values in that order, and a different list misreads the scene. tools/port/script_explorer.py lists every script's properties.
  • No memory changes: scripts take memory from the level's script allocator, whose size the engine fixes per level, so keep variables small.
  • Floats only: the EE has no double-precision unit, and the modules import only memset, so a script that should also build for the PS2 uses no double and no C library function.

Not done

  • Native mods: the loader described above.
  • Extending a script: a new script cannot extend an existing one (extends Alllevels_Guard), use or override virtual functions, or call original script functions.
  • Saves: strings and REFs kept in a new script's variables would be saved as raw values, without the save fixups the originals declare.
  • New objects: a scene can only use a new script through an existing object's script name, since there is no scene editing yet.