Skip to content
Axiom/SDK
SDK / Frames and fuel

Frames and fuel

A mod runs in frames. The host calls its on_frame once a frame, and everything the mod does happens inside that call or inside the on_event calls that follow it. This page is what a frame is on each backend, what a mod may spend in one, and what happens to a mod that misbehaves.

What a frame is

The backend owns timing, because only it knows what a frame is for its game. In the Sandbox and on the headless and Unity backends a frame is a tick on Axiom's side, 20 a second by default on headless; on a backend that draws in the game, it would be the game's own present.

frame@1 says where a mod is:

Call
Answers
axiom_frame_index()
The frame's number, counting up from the first frame of the session.
axiom_frame_delta()
How long the last frame really took, in seconds. Not always the period: a tick that ran late is longer.
axiom_frame_staleness_ms()
How late this frame started past its boundary, in milliseconds.
FRAME.Ctime, not frames
static float s_Angle = 0.0f;

__attribute__((export_name("on_frame"))) void OnFrame(void) {
  const uint64_t frame = axiom_frame_index();
  const float seconds = axiom_frame_delta();
  const uint32_t late = axiom_frame_staleness_ms();

  /* Move at a speed per second, not per frame: a frame on the headless
   * backend is a twentieth of a second, and in a game it is whatever the
   * game drew at. */
  s_Angle += 90.0f * seconds;
}

Work in seconds, not frames. A tick that overran is not followed by a burst of catch-up ticks: the missed ones are skipped, and delta says how much time went by.

The order inside a frame

  1. The host refills every mod's fuel.
  2. It calls each mod's on_frame, in load order.
  3. For each mod, it calls on_event once for each event waiting, up to sixteen, out of whatever fuel that mod has left (Events).
  4. Between frames, the backend does its own work, such as drawing the overlay from what the mods described.

Handles a mod was given are good for the frame they were given in (Handles and statuses).

Fuel

A mod runs on a budget it cannot change. Every frame the host fills its tank, by default with 100,000 units, and the runtime meters the mod's WebAssembly instructions against it and charges every call into the host on top. A mod does not count its own fuel and cannot opt out: the runtime enforces it.

  • Entering a callback is all or nothing. A callback starts only if the mod can afford its reserve, 1,000 units by default. One that cannot is not entered, and nothing partial runs.
  • Running out stops the callback. The call that would have gone past the budget does not happen, the callback is unwound, and it counts as a fault (below). The game's frame carries on.
  • Fuel does not carry over. An idle frame does not buy a more expensive one later.
  • Holding the frame is reported, not killed. A callback that takes longer than a millisecond is reported as having held the frame. Stopping it part way is how a mod gets left holding something.

A mod that loops forever is stopped by fuel, and the frame survives it. Keep a frame's work in proportion to what changed: a mod that walks ten thousand entities every frame will find its limit.

Memory

A mod has one linear memory, at most 64 MiB, and a table of at most 10,000 elements. Fuel bounds how long a mod runs, not how much it asks for, so memory has its own cap. A real mod is a panel and some state, far below either.

Faults and suspension

A fault is a callback that did not return: a trap, running out of fuel, or a call the host refused because no correct mod makes it, such as an out pointer outside the mod's own memory, a string that is not UTF-8 or a span that runs past the end of its memory. A fault ends the callback it happened in, whether that was on_frame or on_event.

A mod's first fault in a session is forgiven. Its second suspends it for the rest of the session: taken out of the frame loop the same frame, with everything it registered released, and named in the frame's report so the overlay and the log say so. The other mods carry on. A status code is not a fault; a mod that gets back a stale handle has done nothing wrong.

Not built yet

  • A profiler view of each mod's fuel. The host records what every callback spent; nothing shows it yet.
  • Changing the budget per mod or per game.