Skip to content
Daan van Yperen edited this page Sep 27, 2015 · 32 revisions

Components are pure data classes with optionally some helper methods.

Example

public class Health extends Component {
    public int health;
    public int damage;
}

See Component best practices.

Working with components

Components are added/fetched and removed from entities using component mappers. Component mappers are high performance component accessors and mutators.

In these example we use ComponentMapper<Pos> mPos;

Add / Get

Adds component to entity, or returns pre-existing.

Pos pos = mPos.create(myEntity);

Remove

Removes component from entity, does nothing if it lacks given component.

Pos pos = mPos.remove(myEntity);

Check

Returns true if part of entity composition.

  if ( mPos.has(myEntity) ) { .. }

Toggle

Toggle component. Especially useful for empty tagging components (example: Flaming Invisible etc).

     mPos.set(myEntity,true); // add (if missing)
     mPos.set(myEntity,false); // remove (if missing)

Pooling

Do you target platforms that suffer from garbage collection freezes? pool your components!

Clone this wiki locally