ECS programs and system hooks
An ECS program runs inside the game's scheduler on a worker thread. Declare its access in kiln.programs.json, next
to kiln.mod.json. The native world service reads this file before the server creates its scheduler. Bind the callback
in your normal OnLoad. Kiln.Sdk copies the file and includes it in KilnPack and KilnDeploy. Since
0.2.0-dev.29 the typed way is Systems.Define over a [Query] struct: the build writes its entry into
kiln.programs.json for you (../services/systems.md).
{
"format": 1,
"programs": {
"pulse": {
"otherRead": ["Health"],
"otherWrite": ["Health"],
"eventsIn": ["TestEvent2", "TestEvent1"],
"eventsOut": ["TestEvent1"]
}
}
}
private IDisposable? program;
public override void OnLoad()
{
program = EcsPrograms.Bind("pulse", context =>
{
foreach (var bytes in context.ReadEvents("TestEvent1"))
Interlocked.Increment(ref eventsSeen);
});
}
public override void OnUnload() => program?.Dispose();
See WorldPrograms for an event round trip. EcsPrograms.Available and
SupportsOtherWrites report native capabilities. An undeclared key, an unknown component/event, or a late declaration
fails explicitly. The declaration remains inert until its mod binds it. Disabled mods and client-only mods do not
reserve server programs; profile entries take precedence over drop-ins, including disabled profile entries.
A mod may declare 32 programs in one file of at most 64 KiB (eight before 0.2.0-dev.29). The file uses strict JSON: duplicate keys, unknown
fields and repeated access names are refused. The schema is kiln-programs.schema.json.
kiln lint discovers it alongside the manifest. The native service also checks engine limits (template slots 8,
read/write slots 42, other-template slots 9, other accessors 92, total event inputs/outputs 24). Programs share the
world service's 64 slots (32 before 0.2.0-dev.29) with Kiln's own services, and the game's remaining scheduler capacity
(its table of 896, which kiln.world checks live before each registration); mods' declarations leave the last 8 slots to
Kiln's services, and refusals appear in kiln.log. kiln.log "kiln.world program slots: 21 of 64 taken, … by Kiln's
services and loaders, … by mods' declarations: kiln_world, …, writer x1" names who holds each slot.
Access lists are tpl, tplOptional, read, readOptional, write, writeOptional, singletonsRead,
singletonsWrite, otherTpl, otherRead, otherWrite, with, without, anyOf, eventsIn, eventsOut. Names
are engine short names; singleton names may contain ::. Optional components return no data. Filters only select
entities. flags is an optional array of system, init, hostOnly, exclusive, chunked, pinned, barrier;
its default is system, hostOnly. A system program gets entity zero and cannot declare entity query lists. For a
per-entity query, supply flags such as ["hostOnly"] and at least one required component/filter.
context.TryRead<T> and TryWrite<T> access the current entity's declared slots; TryReadWritable<T> (since
0.2.0-dev.29.18, mod API 0.2.10) reads a write/writeOptional slot, so a component you read and change is declared
once, under write. TryReadOther<T> and TryWriteOther<T> use declared accessors for another entity. Sizes must match the game's reflected component size.
TryRead/TryWrite get that size from kiln.types: on a build without it they return false (the mod log says so once
per program). Each name is looked up once per program, so calling them per entity costs no allocation or lock.
Writes mark the game's replication/save tracker. otherWrite is passed to the engine's descriptor before it builds
the dependency plan. A component may appear in both other-read and other-write lists. Use QueueAdd and
QueueRemove for changes to the component set at the end of the step.
With Kiln.Game (KilnServices Game) the same four calls take a generated type and no name (since 0.2.0-dev.27):
context.TryGet(out Ecs.Health h), context.TrySet(h), context.TryGetOther(entity, out Ecs.Fuel f) and
context.TrySetOther(entity, f); context.TryGetWritable(out Ecs.SlotAttachment a) reads a write slot (since
0.2.0-dev.29.18). The declaration in kiln.programs.json is still by name, and the size check is the
same. A mod that only wants to change a component now and then needs no program of its own: World.Write<T> and
World.Update<T> queue the change into Kiln's write program (../services/world.md).
ReadEvents returns copies of every event of one declared input type. Multiple types work in one program, regardless
of declaration order. EmitEvent copies an exact event payload into a declared output stream and stamps GameEvent
timestamps. The payload must have the game's reflected size and be smaller than 256 bytes. A full event buffer
returns false. Layouts depend on the game build; use reflected types or structs whose sizes you have checked.
Callbacks cannot call ordinary tick-thread services. Use thread-safe counters or Context.Scheduler.Post to hand work
to the tick thread. The program context is a ref struct; component data is valid only during the callback. An
exception disables that callback. Dispose on the tick thread. Mod unload drains callbacks before freeing their roots.
The reserved scheduler entry remains, so the same declaration can bind after hot reload. Add/change declarations,
restart the server, then reload code. Editing the file while the server runs does not change its scheduler.
Hooks around game systems
A program of the mod's own can also run in this player's client world or prediction storage (since
0.2.0-dev.29): "world": "client", "both" or "prediction" in kiln.programs.json, bound with
EcsPrograms.TryBind(key, callback, WorldKind.Client) on the game's main thread; see
services/world.md (template kiln-client-program).
A client program runs after the game's client programs it conflicts with (so a write to what slot_attachment reads is
drawn from the next client step), and the entities the server sends keep the server's ids in your client world: see
services/world.md.
systems.wraps.client is open since 0.2.0-dev.29. Managed callers select WrapOptions.World = WorldKind.Client; native plugins can select the client world too, when symbols and lifecycle support resolve and no conflicting runtime disables wraps. Native callers must release and drain before freeing callback memory. The implemented 06a-wraps.client guided check counts callbacks and filtered admissions on workers; real-game confirmation is still pending.
hook = GameSystems.Hook("combat_experience_source",
before: () => Interlocked.Increment(ref beforeCount),
after: () => Interlocked.Increment(ref afterCount));
The callbacks run around the real update once per slice, on game worker threads. Dispose on the tick thread; mod
unload removes and drains the hook. A second hook for the same update function fails, including systems which share
that function. At most 64 distinct update functions can be hooked during one process. After disposal the native
trampoline remains passive until process exit; rebinding the same function reuses it. Retirement drains user
callbacks, and an old invocation cannot call a new owner's after callback. The original update remains valid while
another world retires the callbacks. Return promptly: unload waits for a callback currently running.
Hook callbacks observe the original system's access rights; they cannot add scheduler permissions.
GameSystems.Profile measures the existing runner independently. EMF's hook_program uses these same native hooks
and receives the real program context. Missing symbols or a conflicting runtime make hooks unavailable.
The dedicated-server probes cover scheduler registration, other-entity health readback, both input types, spawn-time components, hook/profiler counts and callback removal. A joined client is still needed to confirm replicated visual changes and client/prediction-world behaviour. Managed declarations currently target the server world only.
Wrapping the game's programs
A world program of your own runs in a slot of its own, with the access you declared. To change what one of the
game's own programs does, wrap it instead (Kiln.Systems, since 0.2.0-dev.29; the full page is
../services/systems.md):
var wrap = Systems.Wrap("fuel_setup").Wrap!;
wrap.AfterEach((ref EntityView e) =>
{
var fuel = e.Attribute("Fuel");
fuel[0] = fuel[1]; // full
fuel.Changed(); // the game's attribute formulas
});
A world program (EcsPrograms.Bind) |
GameSystems.Hook |
A wrap (Systems.Wrap) |
|
|---|---|---|---|
| Runs | in its own scheduler slot | around a game update function, per slice | in the wrapped program's slot and thread: Before, After, Replace, per slice or per entity |
| Access | what kiln.programs.json declares (needs a restart) |
none of its own | the wrapped program's own components (EntityView), no declaration |
| Several mods | each has its own | one per update function | any number of Befores and Afters, one Replace per program |
| Writes | marked; ComponentWrittenEvent not raised |
none | marked; ComponentWrittenEvent with Writer = "<modid>/wrap:<program>" |
The same thread rules hold for all three: handlers run on the game's workers once a player is in the world, tick-thread
APIs throw inside them, Slice.Post (or Context.Scheduler.Post) hands work to the tick thread. A wrap's fault or
exception turns off that stage only. Wraps are refused while native SET is loaded, and target the server world by
default; a game client's own world takes WrapOptions.World = WorldKind.Client (systems.wraps.client), and the
prediction storage, which runs the local player's server programs again on the player's own screen, takes
WorldKind.Prediction since 0.2.0-dev.29.18 (systems.wraps.prediction, Before and After only:
services/systems.md).