pyxen.world.all()

Query entities by component.

world.all(*components, without=None)

Returns an iterator over all entities that match the query.

This is the core of Pyxen’s ECS system.


Basic Usage

for e in world.all("enemy"):
    e.health.value -= 1

This iterates over all entities that have the "enemy" component.

Components are identified by name (string).


Multiple Components

You can require multiple components:

for e in world.all("position", "velocity"):
    e.position.x += e.velocity.x

This returns only entities that have both components.

Think of it as:

AND query

Excluding Components (without)

You can exclude entities that have specific components using the without keyword.

for e in world.all("enemy", without=("dead",)):
    attack(e)

This means:

  • Must have "enemy"
  • Must NOT have "dead"

Full Syntax

world.all(
    "required_component_1",
    "required_component_2",
    ...,
    without=("excluded_component_1", "excluded_component_2")
)
  • Required components are positional arguments
  • without must be a tuple

Example: Game of Life

for cell in world.all("cell"):
    if cell.alive and cell.next_alive:
        cell.color = (0.25, 0.15, 0.85, 1.0)

Example: Only Living Cells

for cell in world.all("cell", without=("dead",)):
    update(cell)

Marker Components in Queries

Marker components (boolean-only components) work exactly the same.

e.enemy = True

Then:

for e in world.all("enemy"):
    ...

Markers are extremely efficient in queries.


Built-in Components in Queries

You can query entities by their built-in components:

for e in world.all("sprite"):
    e.color = (1, 0, 0, 1)

The following built-in component names are supported:

NameMatches entities with
"sprite"A sprite (image assigned)
"body"A grid body (collision body)
"camera"A camera
"sound"A sound effect
"music"A music stream
"map"A grid map (tilemap)

You can mix built-in and custom components in the same query:

for e in world.all("sprite", "enemy"):
    e.color = (1, 0, 0, 1)

Exclusion works too:

for e in world.all("sprite", without=("body",)):
    ...

Query Behavior

Internally:

  1. The query collects required component schemas
  2. Collects excluded component schemas
  3. Iterates matching entities
  4. Yields Entity handles

If a component name does not exist:

  • A warning is logged
  • Query still proceeds

Important Notes

Component Names Must Be Strings

world.all("health")

Correct.

world.all(health)

❌ Incorrect.


without Must Be a Tuple

world.all("enemy", without=("dead",))

Correct.

world.all("enemy", without="dead")

❌ Incorrect.


What It Returns

world.all() returns an iterator.

You can:

list(world.all("enemy"))

Or:

for e in world.all("enemy"):
    ...

Safety Notes

If a required component does not exist:

  • A warning is logged
  • Query continues

If too many components are passed, the engine’s internal cap applies.