The ECS (Entity Component System) is the backbone of Hytale’s game engine. Every NPC, player, block entity, projectile, and effect is an entity made up of components. HRTK’s ECS surface lets you create entities, attach and remove components, look up entities, and check entity state - all within the live game.
If you are new to ECS, here are the key concepts:
- Entity Store - The database that holds all entities and their components. Every entity lives inside the store.
- Ref - A lightweight reference (like an ID) to an entity in the store. You use refs to look up, modify, or destroy entities.
- Component - A data object attached to an entity. Components hold state but no logic. For example,
TransformComponent holds position/rotation, HealthComponent holds HP values.
- Archetype - The set of component types an entity has. Entities with the same components share an archetype, which lets the engine look them up efficiently.
Understanding these concepts helps when writing mod tests, because nearly every game system in Hytale reads and writes components on entities through the store.
@EcsTest
Annotate a test method with @EcsTest to receive an EcsTestContext and automatically get the "ecs" tag. Tests marked with @EcsTest run on the world thread and have full access to the entity store.
Use IsolationStrategy.SNAPSHOT on your suite when your ECS tests mutate state. The snapshot captures the store before the suite and restores it after, so your tests do not pollute the live server.
EcsTestContext Methods
| Method | Description |
|---|
getStore() | Get the ECS store |
getCommandBuffer() | Get a command buffer for deferred operations |
createEntity() | Create a new empty entity, returns a reference |
flush() | Execute all deferred command buffer operations |
putComponent(ref, type, component) | Attach a component to an entity |
removeComponent(ref, type) | Remove a component from an entity |
getComponent(ref, type) | Get a component (returns null if absent) |
hasComponent(ref, type) | Check if entity has a component |
waitTicks(n) | Wait for N world ticks |
awaitCondition(supplier, maxTicks) | Poll each tick until non-null or timeout |
findEntities(componentType) | Find all entities with a given component |
countEntities(componentType) | Count entities with a given component |
EcsAssert Methods
| Method | Description |
|---|
assertHasComponent(store, ref, type) | Assert entity has the component |
assertNotHasComponent(store, ref, type) | Assert entity lacks the component |
assertGetComponent(store, ref, type) | Assert component exists and return it |
assertRefValid(ref) | Assert entity reference is non-null |
assertRefInvalid(ref) | Assert entity reference is null |
How EcsAssert Works Internally
Because hrtk-api is a compile-only dependency (it never ships with the server), all ECS assertions use reflection under the hood. You pass the store, refs, and component types as Object in your code, but they must be the correct Hytale types at runtime.
This design lets your mod compile against hrtk-api without needing the Hytale server JAR at compile time.
Why Test ECS Operations?
ECS testing matters for modders because:
- Component integrity - Verify that your custom components attach and detach correctly without corrupting the archetype.
- Query correctness - Confirm that your systems find the right entities. A missing component means your system silently skips entities it should process.
- Lifecycle safety - Catch bugs where entities are used after destruction, or components are read before they are attached.
- Regression detection - Server updates can change how the ECS handles edge cases. Tests catch these regressions early.
Next Steps