Skip to content

Components

Open Javadoc

A component stores the data of an entity and can be modified by systems. Components classes usually only have public properties, because most properties need to be read and set by systems.

Components are created by extending the Component class.

public class SpriteRenderer extends Component {
public Texture texture;
}

Components can be attached to entities. This will trigger any initialisation system that requires a component of the component’s class.

// Attaches the sprite renderer component to the entity
SpriteRenderer spriteRenderer = entity.addComponent(new SpriteRenderer());

After a component is removed, it can be attached to another entity, unless it was also destroyed.

// Detaches the sprite renderer component from the entity
entity.removeComponent(spriteRenderer);
// Destroys the component and detaches it
spriteRenderer.destroy();

Components of entities can be retrieved by referencing the component’s class. However, usage of this method in performance-critical contexts is discouraged, since it is quite slow. Instead, it is recommended to create a system that requires this component.

SpriteRenderer spriteRenderer = entity.getComponent(SpriteRenderer.class);
SpriteRenderer spriteRenderer = childEntity.getComponentInParent(SpriteRenderer.class);
List<SpriteRenderer> spriteRenderers = parentEntity.getComponentsInChildren(SpriteRenderer.class);
boolean hasSpriteRenderer = entity.hasComponent(SpriteRenderer.class);

Components can be activated and deactivated, which will make all systems temporarily ignore them.

component.setActive(false);
component.isActive; // false
component.setActive(true);
component.isActive; // true