Skip to content
Daan van Yperen edited this page May 30, 2019 · 32 revisions

Components are minimal, pure data classes that are plugged into entities to support some behavior. Components give entities data. A component itself implements no behavior.

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

Components are added/fetched and removed from entities using Component mappers.

In these examples we use ComponentMapper<Position> mPosition;

Add / Get

Adds component to entity, or returns pre-existing.

Position position = mPosition.create(myEntity);
position.x = 10;

The component will now be added to the entity (right during the create() call).

Alternatively see #get(myEntity) and getSafe(myEntity,default). Since 2.0.0 #get() will return null for missing components and can safely be used instead of getSafe(myEntity).

Remove

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

mPosition.remove(myEntity);

Check

Returns true if part of entity composition.

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

Toggle

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

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

Pooling

Does your target platform suffer from garbage collection freezes? pool your components!

Clone this wiki locally