Getting Started with Templates
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- pkg/template/basic/shards/game/gen/pkg/template/basic/shards/game/component/component.pb.go
- pkg/template/basic/shards/game/gen/pkg/template/basic/shards/game/system/system.pb.go
- pkg/template/multi-shard/shards/game/gen/pkg/template/multi-shard/shards/game/command/command.pb.go
- coverage_ecs.html
- pkg/cardinal/internal/ecs/world.go
World Engine templates provide a structured foundation for building gamechains using a sharded rollup architecture. These templates demonstrate the core Entity Component System (ECS) patterns required to implement game logic, including commands, components, and systems.
Templates serve as the primary entry point for developers to understand how the Cardinal engine manages state and execution. They provide pre-generated Protobuf structures for common game actions like player movement, combat, and health management.
Core Template Architecture
World Engine templates implement a strict separation between data (Components), logic (Systems), and user intent (Commands). The architecture relies on the Cardinal ECS to manage archetypes, which are collections of entities sharing the same component signatures.
Template Components
The following table describes standard components found in the basic and multi-shard templates:
| Component | Description | Fields |
|---|---|---|
Health | Tracks entity vitality. | HP (int64) |
PlayerTag | Identifies player entities in the world. | Nickname (string) |
Gravestone | Represents a remnant of a defeated player. | Nickname (string) |
ConfigManifest | Manages file-based configuration hashes. | Files (map[string]string) |
Sources: pkg/template/basic/shards/game/gen/pkg/template/basic/shards/game/component/component.pb.go, pkg/plugin/data/gen/pkg/plugin/data/component/component.pb.go
Data Flow and Commands
Templates use Commands to process external input. These commands are received from the micro layer, validated, and then passed to the ECS for execution during a tick.
The diagram shows the transition from user intent to state updates within the template structure.
Sources: coverage_ecs.html:file2, pkg/cardinal/internal/ecs/world.go
Implementation Patterns
Command Handling
Templates define specific command structures generated via Protobuf. Developers implement logic within systems that iterate over these commands using WithCommand[T].
| Command | Purpose | Parameters |
|---|---|---|
AttackPlayerCommand | Initiates combat between entities. | Target (string), Damage (uint32) |
CreatePlayerCommand | Spawns a new player entity. | Nickname (string) |
MovePlayer | Updates player coordinates. | ArgusAuthID, X, Y |
Sources: pkg/template/basic/shards/game/gen/pkg/template/basic/shards/game/system/system.pb.go, pkg/template/multi-shard/shards/game/gen/pkg/template/multi-shard/shards/game/command/command.pb.go
System Execution Lifecycle
Templates register systems to specific "hooks" that determine execution order. The Cardinal engine executes these hooks sequentially during each tick.
This lifecycle ensures that logic like physics (PreUpdate) occurs before game rules (Update) and cleanup (PostUpdate).
Sources: pkg/cardinal/internal/ecs/world.go:42-55, coverage_ecs.html:file11
State Management
Archetypes and Storage
Templates leverage the "Archetype" storage model. An archetype groups all entities that share an identical set of components into dense columns. This allows for efficient memory access during system iteration.
- voidArchetype: An archetype without components, used for entities newly created before components are attached.
- Columns: Dense arrays of serialized component data stored within an archetype.
- Sparse Sets: Used to map
EntityIDto specific row indices within an archetype's columns.
Sources: coverage_ecs.html:file0, coverage_ecs.html:file15:15-30
World Serialization
Templates support snapshotting of the world state. The engine converts the current WorldState—including entity IDs, free IDs, and archetype data—into a Protobuf-based CardinalSnapshot.
This diagram illustrates the hierarchy of the snapshot data structure used for persistence and state synchronization.
Sources: proto/gen/ts/worldengine/cardinal/v1/snapshot_pb.ts, pkg/cardinal/internal/ecs/world.go:106-115
Conclusion
Getting started with World Engine templates requires understanding the relationship between generated Protobuf messages and the ECS lifecycle. By defining Components in .proto files and implementing game logic in Systems, you create a performant and sharded game state. Templates provide the necessary boilerplate for commands like MovePlayer and components like Health, allowing you to focus on unique game mechanics.
Sources: README.md, pkg/template/basic/shards/game/gen/pkg/template/basic/shards/game/component/component.pb.go