*.system.py
Any file ending in .system.py under src/ is a system: one function decorated with
@system.
# src/room/warmth.system.py — the system room/warmth
from cognet import system, Run
from schema.room.room import Heating, Temperature
from .comfort import cold
@system(reads=["temperature"], writes=["heating"])
def warmth(run: Run) -> None:
for room, temperature in run.ecs.all("temperature", Temperature):
run.ecs.set(room).put("heating", Heating(on=cold(temperature)))
@system takes the same fields as defineSystem. Inside, run.ecs is the run's world:
| Call | Does |
|---|---|
run.ecs.first(c, T) | c on the first entity holding it, or None |
run.ecs.get(entity, c, T) | c on one entity |
run.ecs.all(c, T) | every (entity, value) |
run.ecs.set(entity).put(c, value) | write — chain more .puts |
run.since(c, T) | everything written to a streamed c since the last run |
Make it async def for an async system.
Imports
Files beside a system import as a package — from .comfort import cold. Component types are
generated at schema.…, mirroring types/. Large arrays arrive as NumPy arrays.
Errors
An exception fails the run like any other, recorded with its traceback.
Runs on
Its own process, beside the engine, so a slow model or a crash never stops the tick. Packages
go in pyproject.toml.