EIPs Explained

ERC-7509 · Draft · ERC · 2023-09-05

ERC-7509: Entity Component System

Standard interfaces for building on-chain apps and games with the Entity Component System pattern: a World registry, data-holding Component contracts, and free-form System contracts that operate on them.

What problem does it solve?

Entity Component System (ECS) is a common architecture in game engines. Instead of deep class hierarchies, it splits a program into three things:

  • Entities: plain IDs ("player #42", "sword #7").
  • Components: reusable data attached to entities (position, HP, inventory).
  • Systems: logic that reads and writes component data (movement, combat).

Fully on-chain games and multi-contract apps face the same problems ECS solves: data and logic tangled together, painful upgrades, and little reuse between projects. ERC-7509 defines a minimal set of contract interfaces so that ECS-style apps share one vocabulary, and so that components written by one team can be plugged into another's world.

How it works

A World contract (IWorld) is the container and registry:

  • createEntity() returns a new unique uint256 ID; setEntityState toggles it available/unavailable.
  • registerComponent(addr) / registerSystem(addr) add contracts to the world, each with its own on/off state.
  • addComponent(entityId, componentAddr) / removeComponent / hasComponent / getEntityComponents manage which components an entity carries.

A Component contract (IComponent) stores data per entity:

  • world() returns the world it belongs to.
  • types() returns type descriptors (from a shared Types library) for how its bytes data is encoded, so outside callers know how to decode it.
  • set(entityId, data) and get(entityId) read and write the encoded data. A second get(entityId, params) allows parameterised lookups, such as "HP for this entity at level N".

A System is any contract with no required interface. Once registered and enabled, it can call component set functions to change state.

Typical flow: deploy World → create entities → deploy and register components → attach components to entities → deploy and register systems → call systems.

Sample code

A simple position component and a movement system:

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

interface IWorld {
    function entityExists(uint256) external view returns (bool);
    function getEntityState(uint256) external view returns (bool);
    function hasComponent(uint256, address) external view returns (bool);
    function getSystemState(address) external view returns (bool);
}

contract PositionComponent {
    address public immutable world;
    mapping(uint256 => bytes) private data;

    constructor(address _world) { world = _world; }

    function set(uint256 id, bytes memory d) external {
        IWorld w = IWorld(world);
        require(w.getSystemState(msg.sender), "system not enabled");
        require(w.getEntityState(id) && w.hasComponent(id, address(this)), "entity");
        data[id] = d;
    }

    function get(uint256 id) external view returns (bytes memory) { return data[id]; }
    // types() and get(id, params) omitted for brevity
}

contract MoveSystem {
    PositionComponent public immutable pos;
    constructor(PositionComponent p) { pos = p; }

    function move(uint256 id, int32 dx, int32 dy) external {
        (int32 x, int32 y) = abi.decode(pos.get(id), (int32, int32));
        pos.set(id, abi.encode(x + dx, y + dy));
    }
}

Things to know

  • Status is Draft.
  • Administrative functions (createEntity, addComponent, registerSystem, the set...State toggles, etc.) should normally be owner-only. Exposing them to ordinary users is a security risk.
  • Any registered, enabled system can modify component data for every entity in the world, so audit systems before registering them. The spec suggests registering third-party contributions in a disabled state until reviewed.
  • Components should check entity and system availability inside set() and expose no other write path.
  • The ECS split overlaps in spirit with modular upgrade patterns like the ERC-2535 Diamond, but targets data modelling rather than function routing.

#gaming #architecture #ecs #composability

Details

Authors
Rickey (@HelloRickey)
Status
Draft
Type
Standards Track / ERC
Created
2023-09-05

Machine-readable: Markdown · JSON