Skip to main content

KEG

KEG is an Engine for Games. More specifically, it is a small, typed, archetype-based ECS for Python 3.12 and newer.

KEG does not require components to inherit from anything. A dataclass will do. So will any other arbitrary object, although please exercise some judgment.

from dataclasses import dataclass

from keg import World


@dataclass(slots=True)
class Position:
    x: float
    y: float


@dataclass(slots=True)
class Velocity:
    x: float
    y: float


world = World()
player = world.spawn(
    Position(10.0, 20.0),
    Velocity(4.0, -2.0),
)

for entities, positions, velocities in world.query(Position, Velocity):
    for row in range(len(entities)):
        position = positions[row]
        velocity = velocities[row]
        position.x += velocity.x
        position.y += velocity.y

Queries return columns

A query yields one batch per matching archetype. Each batch contains the entity IDs followed by the requested component columns, in request order:

for entities, positions, velocities in world.query(Position, Velocity):
    ...

The sequences in a batch are aligned: entities[row], positions[row], and velocities[row] all belong to the same entity. They expose KEG's underlying column storage and should be treated as read-only. The component objects remain yours to mutate.

This avoids manufacturing a tuple for every entity merely to take it apart again in a hot loop. Queries are precisely typed for up to sixteen component types.

An entity may contain at most one component of each exact runtime type. Component inheritance has no special meaning to KEG.

Structural changes

Spawning and despawning entities, or adding and removing components, can move entities between archetypes. KEG applies those operations immediately during ordinary use and defers them automatically while a query is being iterated. The pending changes are committed after the last active query finishes.

You can request the same behaviour explicitly when applying a batch of changes:

with world.defer_structural_changes():
    projectile = world.spawn(Position(0.0, 0.0))
    world.add_component(projectile, Velocity(12.0, 3.0))

Pending entities and components remain accessible through get_component() and set_component(), while queries continue to see the committed archetype layout until the changes are flushed.

Queries release their structural guard when exhausted or explicitly closed. If you retain a query iterator and stop consuming it early, whether through break or an exception in surrounding code, call its close() method so pending structural work can be committed. Do not rely on garbage collection.

Installation

KEG currently has no runtime dependencies:

python -m pip install keg-engine

Example

The bouncing-balls example uses Kivy to exercise a small position and velocity system in a real update loop:

git clone https://github.com/Cheaterman/KEG.git
cd KEG
python -m pip install -e '.[examples]'
python examples/bouncing_balls/main.py

License

KEG is distributed under the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keg_engine-0.2.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keg_engine-0.2.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file keg_engine-0.2.0.tar.gz.

File metadata

  • Download URL: keg_engine-0.2.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for keg_engine-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b84d520dfb749fd1e2c3dfba3d160a7363b764d2d57f6563c0f48c4ce72dab01
MD5 15ceaf7796b148adb1de791cc26bb2ab
BLAKE2b-256 6fe60f005f60770a1bb8f9e9e9e5342531f18d3e6ad1917f401a622a93e9728e

See more details on using hashes here.

File details

Details for the file keg_engine-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: keg_engine-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for keg_engine-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e89d188594610aacf46b6cd3ee646c9dfec51762c8b8d65258ceff00d545e1f8
MD5 e83dce2f0ef39c6fd4ab6a6515b8a626
BLAKE2b-256 233857d8a97afd41796ace721e0f9355f070277f53bf255f1e9d82601adb2ed2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page