---
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/ja/eip-7509/
lang: ja
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 コントラクトで構成される。

## 何を解決するのか

Entity Component System（ECS）は、ゲームエンジンでよく使われるアーキテクチャです。深いクラス階層の代わりに、プログラムを 3 つに分けます。

- **Entity**: 単なる ID（「プレイヤー #42」「剣 #7」）。
- **Component**: Entity に付与される再利用可能なデータ（位置、HP、インベントリ）。
- **System**: Component のデータを読み書きするロジック（移動、戦闘）。

完全オンチェーンのゲームや複数コントラクトのアプリは、ECS が解決するのと同じ問題に直面します。データとロジックの絡み合い、面倒なアップグレード、プロジェクト間でほとんど再利用できないことです。ERC-7509 は、ECS スタイルのアプリが共通の語彙を持ち、あるチームが書いた Component を別のチームの World に差し込めるように、最小限のコントラクトインターフェースの集合を定義します。

## 仕組み

**World** コントラクト（`IWorld`）はコンテナ兼レジストリです。

- `createEntity()` は新しい一意の `uint256` ID を返します。`setEntityState` は、それを利用可能・利用不可に切り替えます。
- `registerComponent(addr)` / `registerSystem(addr)` は World にコントラクトを追加し、それぞれ独自のオン・オフ状態を持ちます。
- `addComponent(entityId, componentAddr)` / `removeComponent` / `hasComponent` / `getEntityComponents` は、Entity がどの Component を持つかを管理します。

**Component** コントラクト（`IComponent`）は Entity ごとにデータを保存します。

- `world()` は、それが属する World を返します。
- `types()` は、`bytes` データがどうエンコードされているかを示す型記述子（共有の `Types` ライブラリのもの）を返し、外部の呼び出し側がデコード方法を分かるようにします。
- `set(entityId, data)` と `get(entityId)` は、エンコードされたデータを読み書きします。2 つ目の `get(entityId, params)` は、「この Entity のレベル N での HP」のようなパラメータ付きの取得を可能にします。

**System** は、必須のインターフェースを持たない任意のコントラクトです。登録されて有効になれば、Component の `set` 関数を呼んで状態を変更できます。

典型的な流れ: World をデプロイ → Entity を作成 → Component をデプロイして登録 → Component を Entity に付与 → System をデプロイして登録 → System を呼び出す。

## サンプルコード

単純な位置 Component と移動 System:

```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` の切り替えなど）は、通常は所有者専用にすべきです。一般ユーザーに公開するとセキュリティリスクになります。
- 登録され有効になった System は、その World 内のすべての Entity の Component データを変更できるため、登録前に System を監査してください。仕様は、第三者の貢献はレビューされるまで無効状態で登録することを提案しています。
- Component は `set()` の内部で Entity と System の利用可否を確認し、それ以外の書き込み経路を公開してはいけません。
- ECS の分割は、[ERC-2535](/eip-2535/) Diamond のようなモジュール式アップグレードパターンと趣旨が重なりますが、対象は関数のルーティングではなくデータモデリングです。


---

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