Table of Contents
Server-side Lua scripting
This page documents the game-server Lua scripting system and how to bind scripts through ScriptComponent.
Related pages:
Where scripts live
- Script resources are loaded from
server/game-server/res/scripts. - A script resource name is its path relative to
res/scriptswithout the.luaextension.- Example:
res/scripts/player-init.luais referenced asplayer-init.luain component data and resolved to resource nameplayer-initinternally.
- Files under
res/scripts/include/are preloaded as Lua modules and can be used withrequire(“…”).- Example:
res/scripts/include/common.luacan be loaded asrequire(“common”).
Script execution model
The ScriptSystem runs once per server tick and drains queued ScriptEvent values.
Main execution paths:
ScriptComponent.on_initon entity spawn.ScriptComponent.on_hitonOnEntityAttackedEventfor the target entity.- Interaction, dialog, item, and other systems can also queue script execution with a
ScriptContext.
The global Context userdata is set before each script run. The concrete fields depend on which ScriptContext variant triggered execution.
Using ScriptComponent
Add a [script] table to an entity resource (res/ents/*.toml):
[script] on_init = "player-init.lua" on_hit = "tutorial/training-dummy-tutorial-hit.lua"
on_init
- Runs when the entity is spawned.
- Context type:
DefaultScriptContextDataContext:ent_id()Context:target_ent_id()(typicallynilhere)
on_hit
- Runs when the entity is attacked.
- Context type:
OnEntityAttackedScriptContextDataContext:src_ent_id()Context:tgt_ent_id()Context:damage_amount()Context:damage_type()(bitflags; seeinclude/damage_type.lua)
Inline vs resource scripts
Both on_init and on_hit support two forms:
- Resource reference (string ending in
.lua):- The server looks up the resource and executes it.
- Inline Lua source (any other string):
- The string itself is executed as Lua code.
For maintainability, prefer resource scripts over inline Lua.
Script module (include/) usage
Modules under res/scripts/include are preloaded and added to Lua package.preload at startup and script reload.
Example:
local common = require("common") local tutorial = require("tutorial") local DamageType = require("damage_type")
Useful built-in modules currently in the repo:
common(helper wrappers, e.g.common.open_modal)damage_type(damage bit constants)stat(stat IDs)tutorial(tutorial constants/content)
Resource validation
On resource load/reload, the server validates script references in entity and item resources.
For ScriptComponent, only values ending in .lua are validated as resource names. Missing resources are logged as validation errors.
Reload and iteration workflow
Recommended flow while developing scripts:
- Edit files in
res/scriptsor entity resources inres/ents. - Use
/reload_resources script(dev command) to reloadScriptDb. - Scripts under
include/are reloaded and re-preloaded. - Trigger the entity behavior in-game.
For quick one-off evaluation, developers can use /lua [code] which queues ad-hoc script execution with default context.
