Skip to content

Component beta ​

Interface of @v12-engine/engine

ts
export interface Component<T, S = T>

Components are typed state a game module or a mod keeps where the engine has no field for it: on an entity (entity.get(key)), for the whole world (world.state(key)) or for each player (player.state(key)). The engine's own parts are components too (core.: what a shop sells, an illusion's source), so that a game pays for what it uses and nothing else.

A component is plain data, so that it goes into a save with the rest of the world (see saveWorld): numbers, text, lists and plain objects, with other entities by id. One kept otherwise while the game runs (a Map, bytes) says how it is saved and loaded (save, load), wherever it is held.

T is what the component holds; S is what a player's snapshot carries of it (snapshot), T itself when it is sent as it is

Members ​

default ​

ts
readonly default?: () => T;

What it starts as. Needed for world and player state, which is there from the moment it is asked for

field ​

ts
readonly field?: ComponentFieldDef;

A field of the map editor's for it: what is placed on a map starts with the component set to what the field says (MapObject.data[id], under the component's own id), the editor's Inspector shows it for what for names, and the game has it from the moment the world is made, with nothing to copy over in a mapObject hook

id ​

ts
readonly id: string;

The component's id, which saves and snapshots keep it under

index ​

ts
readonly index?: boolean | 'owner';

Filed in the world's index (world.index.with(key), always kept), and by owner too ('owner': world.index.with(key, player)) for what a player asks for often (their heroes), so a system goes through what has the component and nothing else

load ​

ts
load?(saved: unknown, registry: Registry): T;

It again, of what save gave, as a save is loaded: the save's value as it is when left out. Given a copy of the save's, its own to keep or change

onLeave ​

ts
readonly onLeave?: (entity: Entity, world: World) => void;

What is called off of it as the entity leaves its owner: it dies (before the death hook), or is given to another (World.setOwner). Run for an entity that has it

onSpawn ​

ts
readonly onSpawn?: (entity: Entity, world: World) => void;

What an entity starts with of it, as it is made (World.spawn), by its type: a building that makes things gets its queue, a unit that carries its slots. Run for every entity made, the engine's parts first and in their order, before the made hook; what it does not want of an entity, it leaves alone

passable ​

ts
passable?(value: T, entity: Entity): boolean;

Whether a structure that has it stands open now, in nobody's way (a gate opened): what puts the world back from a save leaves it out of the ways as it was. The part that opens and shuts it takes it out and puts it back as it goes (World.nav). Run for an entity that has it

portable ​

ts
readonly portable?: boolean;

false for what is the app's, not the game's rules (where the screen is played from, a tool's own): kept in the app's saves, but left out of a scenario (scenarioOfWorld), which is loaded where only the game is (a test)

refs ​

ts
refs?(saved: unknown): SavedRefs;

The ids of the game's definitions a save of it names (of what save gave, or its value): what a save is checked for before a world is made of it, so that one naming what the game no longer has is refused all at once, saying what (SaveMissing)

replicate ​

ts
readonly replicate?: Audience;

Who gets an entity's component in their snapshot (EntityState.components), for a game's view to show: the player it belongs to, their allies too, or everyone who sees the entity. Nobody when left out

same ​

ts
same?(a: S, b: S): boolean;

Whether two snapshots of it say the same, for the delta a player is sent (same): field by field, as the component knows its shape. Compared as text (JSON.stringify) when left out, which costs with a big value

save ​

ts
save?(value: T, world: World): unknown;

What a save keeps of it, where that is not its value as it is (plain data: numbers, text, lists, plain objects): a Map as its entries, an effect as where it is in its ability, bytes kept for speed as the record a save has always had. Its value as it is when left out, which has to be plain data then. For each of its holders: an entity, the world (world.state) and a player (player.state)

snapshot ​

ts
snapshot?(value: T, entity: Entity, view: SnapshotView): S | undefined;

What a player's snapshot carries of it, worked out for each player told (replicate): a part of it, rounded, or shaped for the screen (a shop's stock with how far the next one is, not the seconds left). A copy of the whole value when left out. What it returns is plain data; undefined sends nothing to that player