---
eip: 7509
label: ERC-7509
title: Entity Component System
status: Draft
type: Standards Track
category: ERC
created: 2023-09-05
official: https://eips.ethereum.org/EIPS/eip-7509
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7509.md
canonical: https://eips.huatuo.xyz/zh/eip-7509/
lang: zh
tldr: 用实体组件系统（Entity Component System）模式构建链上应用和游戏的标准接口：World 注册表、保存数据的 Component 合约，以及对其进行操作的自由形式 System 合约。
tags: [gaming, architecture, ecs, composability]
---

# ERC-7509: Entity Component System

> Represent entities comprising components of data, with systems which operate on entities' components.

**TL;DR:** 用实体组件系统（Entity Component System）模式构建链上应用和游戏的标准接口：World 注册表、保存数据的 Component 合约，以及对其进行操作的自由形式 System 合约。

## 它解决什么问题？

实体组件系统（ECS）是游戏引擎中常见的架构。它不使用很深的类继承层次，而是把程序拆分为三部分：

- **Entity（实体）**：纯粹的 ID（"玩家 #42"、"剑 #7"）。
- **Component（组件）**：附加在实体上的可复用数据（位置、HP、背包）。
- **System（系统）**：读写组件数据的逻辑（移动、战斗）。

完全链上的游戏和多合约应用面临 ECS 所要解决的同样问题：数据和逻辑纠缠在一起、升级痛苦、项目之间很少复用。ERC-7509 定义了一组最小的合约接口，使 ECS 风格的应用共享同一套词汇，也使一个团队编写的组件可以接入另一个团队的 world。

## 它如何运作

**World** 合约（`IWorld`）是容器和注册表：

- `createEntity()` 返回一个新的唯一 `uint256` ID；`setEntityState` 将其切换为可用或不可用。
- `registerComponent(addr)` / `registerSystem(addr)` 向 world 添加合约，每个合约有各自的开关状态。
- `addComponent(entityId, componentAddr)` / `removeComponent` / `hasComponent` / `getEntityComponents` 管理实体携带哪些组件。

**Component** 合约（`IComponent`）按实体存储数据：

- `world()` 返回它所属的 world。
- `types()` 返回类型描述符（来自共享的 `Types` 库），说明其 `bytes` 数据的编码方式，让外部调用者知道如何解码。
- `set(entityId, data)` 和 `get(entityId)` 读写编码后的数据。第二个 `get(entityId, params)` 支持带参数的查询，例如"该实体在等级 N 时的 HP"。

**System** 是任何不要求实现特定接口的合约。一旦注册并启用，它就可以调用组件的 `set` 函数来改变状态。

典型流程：部署 World → 创建实体 → 部署并注册组件 → 把组件附加到实体 → 部署并注册系统 → 调用系统。

## 示例代码

一个简单的位置组件和一个移动系统：

```solidity
// 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));
    }
}
```

## 需要注意

- 状态为 **Draft**。
- 管理类函数（`createEntity`、`addComponent`、`registerSystem`、`set...State` 开关等）通常应仅限所有者调用。向普通用户开放它们是安全风险。
- 任何已注册并启用的系统都可以修改 world 中每个实体的组件数据，因此在注册系统之前请先审计。规范建议，第三方贡献在审核通过之前以禁用状态注册。
- 组件应在 `set()` 内部检查实体和系统的可用性，并且不暴露其他写入路径。
- ECS 的拆分在精神上与 [ERC-2535](/eip-2535/) Diamond 等模块化升级模式有重叠，但它针对的是数据建模，而不是函数路由。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7509
