Plugin System Overview
Relevant source files
The following files were used as context for generating this wiki page:
The World Engine plugin system provides a modular architecture for extending cardinal.World functionality. Plugins encapsulate specific domain logic, such as physics simulation, data configuration loading, or lobby management, and integrate directly into the Cardinal execution lifecycle.
Each plugin implements the cardinal.Plugin interface, allowing it to register internal systems, components, and commands during world initialization. This architecture ensures that complex features remain isolated while maintaining access to the shared ECS (Entity Component System) state.
Sources: pkg/plugin/data/plugin.go:26-45, pkg/plugin/physics2d/plugin.go:193-211
Plugin Lifecycle and Integration
Plugins follow a strict lifecycle that begins before the game loop starts. When you register a plugin, Cardinal invokes the plugin's Register method, providing access to the cardinal.World instance.
- Instantiation: You create a plugin instance with a specific
Config. - Registration: You call
cardinal.RegisterPlugin(world, plugin). - Initialization: The plugin registers its own components and systems.
- Execution: The plugin's systems run during specific tick hooks (e.g.,
PreUpdate,Update,PostUpdate).
The diagram shows the sequence from plugin configuration to integration within the Cardinal tick loop.
Sources: pkg/plugin/data/plugin.go:132-143, pkg/plugin/physics2d/plugin.go:207-211
Key Plugin Implementations
The system currently supports several core plugins that handle different aspects of game chain logic.
Data Plugin
The Data Plugin loads JSON configuration into a process-local catalog during world initialization. It does not store raw data on ECS components; instead, it uses a ConfigManifest component to track content hashes for snapshot consistency.
| Feature | Description |
|---|---|
| Catalog | In-memory storage for unmarshaled JSON definitions. |
| Reconcile | System that ensures data consistency after snapshot restores. |
| Source | Interface for fetching data from embed.FS or external operators. |
Sources: pkg/plugin/data/plugin.go:1-25, pkg/plugin/data/system/state.go:102-126
Physics2D Plugin
The Physics2D Plugin integrates a Box2D-backed simulation into the ECS. It synchronizes ECS components like Transform2D and Velocity2D with an internal C-side physics world.
- InitPhysicsSystem: Runs once during the
Inithook to set up the simulation environment. - PhysicsPipelineSystem: Runs during
PreUpdateto perform simulation steps and reconcile state. - ResetRuntime: Discards derived simulation state, forcing a rebuild from ECS components.
Sources: pkg/plugin/physics2d/plugin.go:1-18, pkg/plugin/physics2d/plugin.go:207-211
Lobby Plugin
The Lobby Plugin manages player groups, team assignments, and session transitions. It facilitates cross-shard communication by notifying game shards when sessions start.
This diagram illustrates the asynchronous session start flow involving external orchestrators.
Sources: pkg/plugin/lobby/README.md:144-160, pkg/plugin/lobby/README.md:165-180
Core Data Structures
Plugins define their own internal state and external interfaces through Go types and Protobuf messages.
Plugin Registration and Access
The Data plugin uses a process-global handle to allow systems to access configuration without passing pointers.
// From pkg/plugin/data/plugin.go
func Get[T Definition](https://github.com/Argus-Labs/world-engine/blob/828ee5e248437d8ba8cb8e86d3751c0250e95e39/T%20Definition) T {
if registered == nil {
panic("data: no plugin registered")
}
var zero T
v, ok := registered.state.MustGet(zero.Name()).(T)
return v
}
Sources: pkg/plugin/data/plugin.go:111-125
Manifest and Reconcile State
The ConfigManifest tracks the state of loaded data per file. During reconciliation, the system compares the snapshot hash with the current in-memory hash.
Sources: pkg/plugin/data/system/state.go:128-142
Error Handling and Constraints
Plugins enforce strict validation during registration and execution:
- Duplicate Registration: Registering two kinds with the same
Name()orJSONFile()triggers a panic. - Fetch Failures: If a
Sourcefails to provide required files at startup, the plugin panics to ensure a "fail-loud" initialization. - Snapshot Mismatch: If a versioned source cannot serve a specific hash required by a restored snapshot, the system panics to prevent inconsistent state.
Sources: pkg/plugin/data/system/state.go:66-77, pkg/plugin/data/data_test.go:210-220
Conclusion
The Plugin System enables the World Engine to remain extensible by providing a standardized interface for domain-specific logic. By utilizing ECS hooks and process-global catalogs, plugins like Data and Physics2D provide high-performance features while ensuring state consistency across sharded environments and snapshot restores.