Skip to main content

PL-ECS

Peach's Lightweight Entity-Component-System

Low code, low dependency, simple implementation of ECS.

Quick Start

pip install pl-ecs

Entity

A unique identifier used to hold components together.

Entities are created and deleted via the World that they exist.

ent = world.create_entity()
world.delete_entity(ent)

Component

Pure data aggregated to an entity

Components included in a components/ package at the root of the project are automatically imported to PL-ECS at start up. It is not a requirement that all Component definitions exist in one package but it is recommended as it makes the code cleaner and easier to manage. It is slighly more efficient as well since the penalty of dynamic class creation and registration are paid at initialization and not during run time mid-frame.

Components are created using the @component_dataclass decorator. The class declaration should be familiar as it is identical to python's dataclasses.

Example:

from ecs import component_dataclass

@component_dataclass
class MyComponent:
    name: str = "me"

By creating a Component it becomes available to all Systems in the World without requiring a dependency on the module that created the class.

To use a Component in a system, just get it or create it.

from ecs import Component

# Get the class
MyComponent = Component.get("MyComponent")
my_component = MyComponent(name="mine")

# Create the object
my_component = Component.create("MyComponent", name="mine")

System

Logic that acts on components, creates new ones, modifies entities

Systems included in a systems/ package at the root of the project are automatically imported to PL-ECS at start up. It is not a requirement that all Systems exist in one package but it is recommended as it makes the code cleaner and easier to manage. It also helps reduce the dependencies when the World is initialized and allows everything to be run/managed via strings in config files without requiring code changes.

It is best to keep Systems simple, do one thing and do it well.

To create a System create a subclass and provide an update(self, dt, world) method. If a custom __init__() is needed, it must have two keyword arguments, one for name: str another for config: dict.

from dataclasses import dataclass
from ecs import World, System

@dataclass
class Config:
    my_data: str

class MySystem(System):

    def __init__(self, name: str, config: dict)
        """ Optional overide """

        # Validate config
        try:
            config = Config(**config)
        except Exception as e:
            raise ValueError(f"Invalid config")

        self._data = config.my_data

    def update(self, dt: float, world: World):
        for ent, comp in world.entities_with("MyComponent"):
            do_something(comp)

Once a System is created it can be instantiated by name without requiring dependencies outside of PL-ECS

from ecs import System

my_config = {}
my_system = System(name="MySystem", config=my_config)

World

Manager of the registered World. Executes each System every frame, in the order that they're registered, and maintains all Entities and their Components.

A Frame is a single pass through all added Systems. Systems are run in the order that they are added.

Entity Management

Create an entity:

entity = world.create_entity()

Delete an entity:

world.delete_entity(ent)

Component Management

Add Component to an entity:

world.add_component(entity, component)

Get a Component for an entity:

component = world.get_component(ent, "MyComponent")

# Returns None if component does not exist for entity
if component is None:
    do_nothing()
else:
    do_something(component)

Remove a Component from an entity:

world.remove_component(ent, "MyComponent")

Query entities that match Component signatures:

# Query returns a tuple of all entities and the components in the request
for ent, my_comp in world.entities_with("MyComponent"):
    do_something(my_comp)

System Management

Add a system to the world:

world.add_system(System(name="MySystem"))

Remove a system from the world:

world.remove_system("MySystem")

Run a frame:

world.update(dt=1.0)

Metadata

Release files for pl-ecs 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pl-ecs 0.1.0
File Size Uploaded Provenance Actions
pl_ecs-0.1.0.tar.gz 10.0 kB Without provenance Details for pl_ecs-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pl-ecs 0.1.0
File Interpreter ABI Platform Actions
pl_ecs-0.1.0-py3-none-any.whl Python 3 none any Details for pl_ecs-0.1.0-py3-none-any.whl

Total release size: 19.6 kB

Release files / pl_ecs-0.1.0.tar.gz

Download URL pl_ecs-0.1.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
94504f80c0f2ca1a1d4ccdd2482b7133e821ba17cc4163e18597a9edadd9e00f
BLAKE2b-256 checksum
How to use checksums
8595facdaefc8cd32fd65f8c3b84cb28630e8ca6e844c97a02870beeb5d84a90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / pl_ecs-0.1.0-py3-none-any.whl

Download URL pl_ecs-0.1.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e493f91976d5f4fe31ead409d5a5902195809f05d034eb3ea61c82f878f6f9ec
BLAKE2b-256 checksum
How to use checksums
2098027b2f45a3c8a0170bc60fa4b1c664a9586994d4f95432b5e441b30d327f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for pl-ecs 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page