Skip to main content

Player metrics

A player metric is a named number or set of integer keys stored for each character. A declaration specifies how the value is collected and aggregated. The catalog is the collection of declared metrics. A push is a call that supplies a value or key; a signal is a native event carrying operands that can feed declarations.

Quick start​

Declare a signal counter and a custom counter in metrics.yaml:

metrics:
kill.stone: { from: kill, where: { type: [STONE] }, agg: count }
crafts.attempted: { from: custom, agg: count }

A forge can push the custom counter and log its value when its code handles an attempt:

local function record_attempt(ch)
local metrics = forge.sys.game.metrics
metrics.add(ch, "crafts.attempted", 1)
sys_log(0, "crafts.attempted = " .. metrics.get(ch, "crafts.attempted"))
end

Call record_attempt from the code handling the attempt. The native kill signal feeds kill.stone; it needs no separate push. Restart the game core after changing declarations, then use /metric get <pg_name> kill.stone to inspect a character.

Glossary​

TermMeaning
DeclarationA named metric's source, aggregation, and options.
CatalogThe collection of declared metrics.
SignalA native event with operands that can feed metrics.
PushA call that supplies a value or key to a metric.
ConsumerCode that reads metrics or subscribes to changes.
SeededThe metric was ever written for this character.
BlockThe per-character record of metric values.
Dirty queueThe queue of characters with unpublished changes.
HeartbeatThe periodic game-core tick.
SweepThe pass that drains the dirty queue each heartbeat and publishes the change event.
PredicateA Lua function named in where.predicate that is called last.
Virtual sessionA session described in player virtualization.

Files and setup​

ItemLocation or purpose
Declarationsmetrics.yaml in the locale folder.
Lua APIforge.sys.game.metrics in forges and quests.
PredicatesForges register functions in global METRIC_PREDICATES.
Scalar storageplayer_stat stores character, metric name, and value.
Set storageplayer_stat_set stores character, metric name, and key.

Forges load and register predicates before the engine reads metrics.yaml. Consumers can subscribe to the change event and inspect the catalog in when boot, after declarations load. A predicate is a Lua function named in where.predicate that runs after the integer filters. See declaring metrics for the schema and the Lua API for subscription.

The engine reads the file at boot. Restart the game core to apply changes. Metric names are persistent storage keys; renaming one requires a data migration.

Aggregations and values​

aggResult of a matching signal or push
countAdds one per signal or the pushed delta.
sumAdds the measured signal value or pushed delta.
maxKeeps the highest value.
minKeeps the lowest value.
distinctKeeps a set of integer keys.

damage supplies its measured damage; other signals supply one. A custom push supplies its delta. For distinct, get returns the set size and keys returns the keys. An unwritten metric reads zero, so check is_seeded before interpreting zero for min. Zero can itself be a written minimum or maximum.

The engine ignores a zero delta for sum. A push that leaves the value unchanged returns false and does not enqueue a change. The aggregations retain totals, extremes, or set membership; they do not provide a reset operation.

Behaviour​

A block is the per-character record of metric values. The engine saves changed values and newly added keys. A row with an undeclared name is logged and ignored on load. In-memory blocks belong to one game core; database rows provide persistent storage.

A virtual session is described in player virtualization. Pushes from one return false unless a custom declaration opts in with allow_virtual. Signal-derived declarations cannot opt in. Use the option only when the value belongs to the virtual session and the code that owns the episode makes the push.

A consumer is code that reads metrics or subscribes to their change event. Changed characters enter the dirty queue, the queue of characters with unpublished changes. Each heartbeat, the periodic game-core tick, runs a sweep: the pass that drains the queue and publishes the change event. Publication and database saves have separate schedules. See examples and pitfalls for setup checks and initial data.