---
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/ko/eip-7509/
lang: ko
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)은 게임 엔진에서 흔히 쓰는 아키텍처입니다. 깊은 클래스 계층 대신 프로그램을 세 가지로 나눕니다.

- **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)`는 인코딩된 데이터를 읽고 씁니다. 두 번째 `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을 감사하세요. 명세는 제3자의 기여는 검토될 때까지 비활성 상태로 등록하라고 제안합니다.
- 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
