Skip to content
Axiom/SDK
SDK / Entities and the camera

Entities and the camera

Most mods are about things in the game: where they are, how many there are, and sometimes moving one. These are the capabilities for that. Their answers come from the game's pack, so the same calls work on any game whose pack answers them.

Listing what is alive

entity.query@1 hands out this frame's entities.

Call
Does
axiom_entity_query_alive(out, count)
Writes up to count handles into out and returns how many it wrote. The host reads only as many as you made room for.
axiom_entity_query_count()
How many entities are alive, without handing out handles.
axiom_entity_query_is_alive(index, generation)
Whether a handle from this frame still names a live entity.

alive is the only way a mod comes by an entity handle. Size the array for what the frame will actually use, because a mod has no allocator: the host never writes past count, and entities past it are left for another frame or a larger array.

LISTINGhandles, then a position
AxiomEntityQueryEntityId ids[32];
uint32_t alive = axiom_entity_query_alive(ids, 32);

AxiomVec3 position;
if (alive > 0 &&
    axiom_entity_transform_position(ids[0].index, ids[0].generation,
                                    &position) != 0) {
  /* A stale id, or a pack that does not answer positions. */
}

What an entity is depends on the pack. The Cities: Skylines pack answers vehicles: one entity per live vehicle slot.

Reading where things are

entity.transform@1 reads a handle's place in the world. It requires entity.query@1, so declare both.

Call
Writes
axiom_entity_transform_position(index, generation, &out)
An AxiomVec3, the position.
axiom_entity_transform_transform(index, generation, &out)
An AxiomTransform: position, rotation as a quaternion, and scale.

Each returns 0 and writes through the pointer, or returns 1 and writes nothing. Units and axes are the game's own; Axiom does not convert them. A pack that answers only position fills the rest of a transform with an identity rotation and a scale of one.

Moving things

axiom_entity_transform_set_position(index, generation, x, y, z) moves an entity. It is the one write a mod can make to a game today, and four things all have to hold for it to reach the game:

  1. The pack names the field it writes, with writes = ["position"] in its pack.toml.
  2. The game build is vouched for, single-player. A vouched private server reads and does not write.
  3. The handle is this frame's. A stale or forged one is refused, never written to whatever holds the slot now.
  4. The bytes are the field's. The backend lets the write touch that record's position and no other byte.

Ask first with axiom_entity_transform_writable(), which is 1 only when the first two hold. When any of them does not, set_position returns 2, ReadOnly, and the mod keeps running, so one mod can run read only on one game and write on another.

MOVER.Clifts one vehicle ten units
#include "surface.h"

static int32_t s_Done = 0;

__attribute__((export_name("on_frame"))) void OnFrame(void) {
  if (s_Done) {
    return;
  }
  /* Ask first. A game the pack does not write, or a build that is not
   * single-player, says no here and the mod still runs. */
  if (!axiom_entity_transform_writable()) {
    return;
  }

  AxiomEntityQueryEntityId ids[1];
  if (axiom_entity_query_alive(ids, 1) != 1) {
    return;
  }
  AxiomVec3 at;
  if (axiom_entity_transform_position(ids[0].index, ids[0].generation, &at) !=
      0) {
    return;
  }

  /* Lift it ten units. A status of 2 is ReadOnly. */
  if (axiom_entity_transform_set_position(ids[0].index, ids[0].generation,
                                          at.x, at.y + 10.0f, at.z) == 0) {
    s_Done = 1;
  }
}

On Cities: Skylines, a vehicle keeps four interpolation frames. set_position writes the position into all four, so the game has no old frame to slide back from, and touches nothing else in the record. The game's simulation keeps running: a moving vehicle carries on from where you put it.

The camera and the screen

camera@1 reads the game's view, and project@1 turns a world point into a screen pixel. Both are answered by a pack with a camera.

Call
Writes
axiom_camera_view(&out)
The view matrix, an AxiomMat4 as four columns.
axiom_camera_projection(&out)
The projection matrix.
axiom_camera_view_projection(&out)
The two multiplied.
axiom_camera_position(&out)
The eye, an AxiomVec3.
axiom_project_world_to_screen(x, y, z, &out)
An AxiomVec2 in screen pixels, DPI scaled, from the top left.

Use world_to_screen rather than multiplying matrices yourself: it runs on the host, so a matrix convention never reaches your mod. A point behind the camera is status 1, Offscreen, rather than a pixel that looks real.

MARKERS.Cfrom waypoints.c
#define MARKERS 32

__attribute__((export_name("on_frame"))) void OnFrame(void) {
  AxiomEntityQueryEntityId ids[MARKERS];
  const uint32_t alive = axiom_entity_query_alive(ids, MARKERS);

  for (uint32_t at = 0; at < alive; ++at) {
    AxiomVec3 position;
    if (axiom_entity_transform_position(ids[at].index, ids[at].generation,
                                        &position) != 0) {
      continue;
    }

    AxiomVec2 screen;
    if (axiom_project_world_to_screen(position.x, position.y, position.z,
                                      &screen) != 0) {
      /* Behind the camera, or no camera: not a point on the screen. */
      continue;
    }
    /* screen.x and screen.y are pixels from the top left. */
  }
}

What a game answers

Game
Pack
Answers
Cities: Skylines 1.21.1-f9
com.axiommods.packs.citiesskylines
entities (vehicles), transform (position), and writes position

The Cities: Skylines pack is silent on the camera: in a Unity game the camera is the Unity backend's to read, and that is not built yet. A mod that requires camera@1 or project@1 there is refused at load. The two Cities: Skylines example mods in the Axiom repository, vehicle-observer and vehicle-mover, require only entity.query@1, entity.transform@1 and frame@1.

Not built yet

  • by_kind, within, transforms and positions are in the surface and no pack answers them. A mod that imports them does not load.
  • camera@1's fov and clip, and the camera on the Unity backend.
  • The spawned, despawned and moved events.