Skip to content

Components

Components are data containers attached to entities. They define what an entity "is" and "has".

Accessing Components

java
// From a Ref (entity reference)
Player player = (Player) commandBuffer.getComponent(ref, Player.getComponentType());

// From an ArchetypeChunk (in tick systems)
Player player = (Player) chunk.getComponent(index, Player.getComponentType());

Common Components

Entity Data

ComponentDescriptionKey Methods
PlayerPlayer-specific datagetInventory(), getGameMode(), sendMessage()
TransformComponentPosition & rotationgetPosition(), getRotation()
VelocityMovement velocitygetVelocity()
HeadRotationHead directiongetRotation()
NameplateDisplay namegetText()
ModelComponentEntity modelgetModel()

Stats & Effects

ComponentDescriptionKey Methods
EntityStatMapHealth, stamina, etc.get(statType), setStatValue()
EffectControllerComponentActive effectsgetActiveEffects(), clearEffects()
DamageDataComponentCombat trackinggetLastCombatAction()

State Components

ComponentDescriptionWhen Present
DeathComponentDeath infoEntity is dead
InvulnerableMarkerEntity can't be damaged
IntangibleMarkerEntity can't be hit

Checking Component Existence

java
// Check if entity has a component
Archetype<EntityStore> archetype = chunk.getArchetype();
boolean isDead = archetype.contains(DeathComponent.getComponentType());
boolean isPlayer = archetype.contains(Player.getComponentType());

Adding/Removing Components

java
// Add a component
commandBuffer.putComponent(ref, SomeComponent.getComponentType(), new SomeComponent());

// Remove a component
commandBuffer.removeComponent(ref, SomeComponent.getComponentType());

// Try to remove (no error if missing)
commandBuffer.tryRemoveComponent(ref, SomeComponent.getComponentType());

Component Types in Queries

Use components to filter which entities your system processes:

java
@Override
public Query getQuery() {
    return Query.and(
        Player.getComponentType(),           // Must have Player
        TransformComponent.getComponentType(), // Must have Transform
        Query.not(DeathComponent.getComponentType())  // Must NOT be dead
    );
}

See Also

Unofficial documentation · Any questions? Found a mistake? Have something you want documented? Join the Discord server at the top and let us know in #hytale!