Entity Overview
In ECS architecture, an Entity is a fundamental object in the game world. Entities contain no game logic or data themselves—they are simply containers that combine different components to achieve various functionalities.
Basic Concepts
Section titled “Basic Concepts”An entity is a lightweight object primarily used for:
- Acting as a container for components
- Providing unique identifiers (ID and persistentId)
- Managing component lifecycles
Creating Entities
Section titled “Creating Entities”Entities must be created through the scene, not manually.
// Correct: Create entity through sceneconst player = scene.createEntity("Player");
// ❌ Wrong: Manual creation// const entity = new Entity("MyEntity", 1);Creating through the scene ensures:
- Entity is properly added to the scene’s entity management system
- Entity is added to the query system for use by systems
- Entity gets the correct scene reference
- Related lifecycle events are triggered
Batch Creation
Section titled “Batch Creation”The framework provides high-performance batch creation:
// Batch create 100 bullet entitiesconst bullets = scene.createEntities(100, "Bullet");
bullets.forEach((bullet, index) => { bullet.createComponent(Position, Math.random() * 800, Math.random() * 600); bullet.createComponent(Velocity, Math.random() * 100, Math.random() * 100);});createEntities() batches ID allocation, optimizes query system updates, and reduces system cache clearing.
Entity Identifiers
Section titled “Entity Identifiers”Each entity has three types of identifiers:
| Property | Type | Description |
|---|---|---|
id | number | Runtime unique identifier for fast lookups |
persistentId | string | GUID for maintaining reference consistency during serialization |
handle | EntityHandle | Lightweight handle, see Entity Handle |
const entity = scene.createEntity("Player");
console.log(entity.id); // 1console.log(entity.persistentId); // "a1b2c3d4-..."console.log(entity.handle); // Numeric handleEntity Properties
Section titled “Entity Properties”Name and Tag
Section titled “Name and Tag”// Name - for debugging and lookupentity.name = "Player";
// Tag - for fast categorization and queryingentity.tag = 1; // Player tagenemy.tag = 2; // Enemy tagState Control
Section titled “State Control”// Enable/disable stateentity.enabled = false;
// Active stateentity.active = false;
// Update order (lower values have higher priority)entity.updateOrder = 10;Finding Entities
Section titled “Finding Entities”The scene provides multiple ways to find entities:
// Find by nameconst player = scene.findEntity("Player");// Or use aliasconst player2 = scene.getEntityByName("Player");
// Find by IDconst entity = scene.findEntityById(123);
// Find all entities by tagconst enemies = scene.findEntitiesByTag(2);// Or use aliasconst allEnemies = scene.getEntitiesByTag(2);
// Find by handleconst entity = scene.findEntityByHandle(handle);Entity Events
Section titled “Entity Events”Entity changes trigger events:
// Listen for component additionsscene.eventSystem.on('component:added', (data) => { console.log(`${data.entityName} added ${data.componentType}`);});
// Listen for component removalsscene.eventSystem.on('component:removed', (data) => { console.log(`${data.entityName} removed ${data.componentType}`);});
// Listen for entity creationscene.eventSystem.on('entity:created', (data) => { console.log(`Entity created: ${data.entityName}`);});
// Listen for active state changesscene.eventSystem.on('entity:activeChanged', (data) => { console.log(`${data.entity.name} active: ${data.active}`);});Debugging
Section titled “Debugging”// Get entity debug infoconst debugInfo = entity.getDebugInfo();console.log(debugInfo);// {// name: "Player",// id: 1,// persistentId: "a1b2c3d4-...",// enabled: true,// active: true,// destroyed: false,// componentCount: 3,// componentTypes: ["Position", "Health", "Velocity"],// ...// }
// Entity string representationconsole.log(entity.toString());// "Entity[Player:1:a1b2c3d4]"Next Steps
Section titled “Next Steps”- Component Operations - Add, get, and remove components
- Entity Handle - Safe entity reference method
- Lifecycle - Destruction and persistence