Components
Open JavadocA 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.
Creating components
Section titled “Creating components”Components are created by extending the Component
class.
Example
Section titled “Example”public class SpriteRenderer extends Component {
public Texture texture;
}
Adding components to entities
Section titled “Adding components to entities”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 entitySpriteRenderer spriteRenderer = entity.addComponent(new SpriteRenderer());
Removing components from entities
Section titled “Removing components from entities”After a component is removed, it can be attached to another entity, unless it was also destroyed.
// Detaches the sprite renderer component from the entityentity.removeComponent(spriteRenderer);
// Destroys the component and detaches itspriteRenderer.destroy();
Getting components of entities
Section titled “Getting components of entities”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);
Querying components of entities
Section titled “Querying components of entities”boolean hasSpriteRenderer = entity.hasComponent(SpriteRenderer.class);
Toggling components
Section titled “Toggling components”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