pyecs.containers.ComponentStorage#

class pyecs.containers.ComponentStorage[source]#
__init__()[source]#

Methods

__init__()

add_component(entity, component)

Add or update a component for an entity.

get_component(entity, component_type)

Retrieve a specific component from an entity.

get_entity_components(entity)

Retrieve all components for an entity.

has_component(entity, component_type)

Check if an entity has a specific component type.

move_entity_to_archetype(entity, new_mask[, ...])

Move an entity to a different archetype.

remove_component(entity, component_type)

Remove a component type from an entity.

remove_entity(entity)

Remove an entity and all its components from storage.

__init__()[source]#
add_component(entity, component)[source]#

Add or update a component for an entity.

If the component type doesn’t exist on the entity, adds it and transitions the entity to a new archetype. If the component type already exists, updates the existing component in place.

Returns COMPONENT_ADDED for new components, COMPONENT_UPDATED for existing components, or FAILURE if the entity doesn’t exist.

Return type:

Literal[<StatusCodes.COMPONENT_ADDED: 200>, <StatusCodes.COMPONENT_UPDATED: 202>, <StatusCodes.FAILURE: -1>]

Parameters:
  • entity (Entity)

  • component (Component)

remove_component(entity, component_type)[source]#

Remove a component type from an entity.

Transitions the entity to a new archetype without the specified component. If this was the last component, removes the entity entirely.

Returns COMPONENT_REMOVED on success, or FAILURE if the entity doesn’t exist or doesn’t have the specified component type.

Return type:

Literal[<StatusCodes.COMPONENT_REMOVED: 201>, <StatusCodes.FAILURE: -1>]

Parameters:
  • entity (Entity)

  • component_type (type[T])

get_component(entity, component_type)[source]#

Retrieve a specific component from an entity.

Returns the component instance if found, or FAILURE if the entity doesn’t exist or doesn’t have the specified component type.

Return type:

Union[TypeAliasType, Literal[<StatusCodes.FAILURE: -1>]]

Parameters:
  • entity (Entity)

  • component_type (type[T])

has_component(entity, component_type)[source]#

Check if an entity has a specific component type.

Returns True if the entity exists and has the component type, False otherwise.

Return type:

bool

Parameters:
  • entity (Entity)

  • component_type (type[T])

move_entity_to_archetype(entity, new_mask, components=None)[source]#

Move an entity to a different archetype.

Handles the transition of an entity between archetypes when components are added or removed. Creates the target archetype if it doesn’t exist.

Returns SUCCESS after moving the entity to the new archetype.

Return type:

TypeAliasType

Parameters:
remove_entity(entity)[source]#

Remove an entity and all its components from storage.

Removes the entity from its current archetype and clears all component data associated with it.

Returns SUCCESS on removal, or FAILURE if the entity doesn’t exist.

Return type:

TypeAliasType

Parameters:

entity (Entity)

get_entity_components(entity)[source]#

Retrieve all components for an entity.

Returns a list containing all component instances attached to the entity, in the order they appear in the archetype mask.

Return type:

list[TypeAliasType]

Parameters:

entity (Entity)