Skip to content

Adding Entity Components

Christian Oeing edited this page Jan 8, 2017 · 1 revision

Other than in traditional game logic architectures, there is no game object or actor class in the Slash Framework. Game entities are nothing more than just a unique id. Actual game entities are created by mapping these ids to game components using an entity manager, like this:

// Create new game entity.
var entityId = game.EntityManager.CreateEntity();
  
// Option A: Create and attach new health component.
var healthComponent = new HealthComponent();
game.EntityManager.AddComponent(entityId, healthComponent);
 
// Option B: Use generic method to create and attach in one step.
var healthComponent = game.EntityManager.AddComponent<HealthComponent>(entityId);

Game logic will operate on these components by accessing and modifying their properties through the entity manager:

// Get health component.
var healthComponent = game.EntityManager.GetComponent<HealthComponent>(entityId);
  
// Change health.
healthComponent.Health = 18;
Clone this wiki locally