Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs: add entity hierarchy API (setParent/removeParent) to ECS and Systems docs

esengine50f47732f1a1
diff --git a/docs/astro/src/content/docs/core-concepts/ecs.mdx b/docs/astro/src/content/docs/core-concepts/ecs.mdx
index 954baa72e..add654fc0 100644
--- a/docs/astro/src/content/docs/core-concepts/ecs.mdx
+++ b/docs/astro/src/content/docs/core-concepts/ecs.mdx
@@ -154,6 +154,8 @@ The `World` stores all entities and components. In your scripts, you interact wi
| React to changed components | `Query(Changed(Component))` |
| React to removed components | `Removed(Component)` |
| Send events between systems | `EventWriter(Event)` / `EventReader(Event)` |
+| Set parent-child hierarchy | `GetWorld()` → `world.setParent(child, parent)` |
+| Remove parent | `GetWorld()` → `world.removeParent(entity)` |
See [Systems](/microes/core-concepts/systems/) and [Queries](/microes/core-concepts/queries/) for detailed usage.
diff --git a/docs/astro/src/content/docs/core-concepts/systems.mdx b/docs/astro/src/content/docs/core-concepts/systems.mdx
index 2c6ace78c..6861d4245 100644
--- a/docs/astro/src/content/docs/core-concepts/systems.mdx
+++ b/docs/astro/src/content/docs/core-concepts/systems.mdx
@@ -243,11 +243,38 @@ defineSystem([GetWorld()], (world) => {
| `world.has(entity, Component)` | boolean | Check if entity has component |
| `world.tryGet(entity, Component)` | data \| null | Safe read, returns null if missing |
| `world.valid(entity)` | boolean | Check if entity is alive |
+| `world.setParent(child, parent)` | void | Set entity's parent (child Transform becomes relative to parent) |
+| `world.removeParent(entity)` | void | Remove entity's parent, making it a root entity |
<Aside type="tip">
Prefer `Query` + `Mut()` when iterating multiple entities — it handles auto-writeback. Use `GetWorld()` when you need to access a **known entity by ID** without iteration, e.g. after `findEntityByName()`.
</Aside>
+#### Entity Hierarchy
+
+Use `setParent` / `removeParent` to build parent-child relationships. A child's `Transform` is relative to its parent.
+
+```typescript
+import { GetWorld, Commands, Transform, Sprite } from 'esengine';
+
+defineSystem([Commands(), GetWorld()], (cmds, world) => {
+ const parent = cmds.spawn()
+ .insert(Transform, { position: { x: 100, y: 100, z: 0 } })
+ .insert(Sprite, {})
+ .id();
+
+ const child = cmds.spawn()
+ .insert(Transform, { position: { x: 50, y: 0, z: 0 } })
+ .insert(Sprite, {})
+ .id();
+
+ world.setParent(child, parent);
+
+ // Later, to detach:
+ // world.removeParent(child);
+});
+```
+
### Combining Parameters
```typescript
diff --git a/docs/astro/src/content/docs/zh/core-concepts/ecs.mdx b/docs/astro/src/content/docs/zh/core-concepts/ecs.mdx
index f85531931..5943bc2b6 100644
--- a/docs/astro/src/content/docs/zh/core-concepts/ecs.mdx
+++ b/docs/astro/src/content/docs/zh/core-concepts/ecs.mdx
@@ -154,6 +154,8 @@ addSystem(defineSystem(
| 响应变更组件 | `Query(Changed(Component))` |
| 响应移除组件 | `Removed(Component)` |
| 系统间发送事件 | `EventWriter(Event)` / `EventReader(Event)` |
+| 设置父子层级 | `GetWorld()` → `world.setParent(child, parent)` |
+| 移除父节点 | `GetWorld()` → `world.removeParent(entity)` |
详见[系统](/microes/zh/core-concepts/systems/)和[查询](/microes/zh/core-concepts/queries/)。
diff --git a/docs/astro/src/content/docs/zh/core-concepts/systems.mdx b/docs/astro/src/content/docs/zh/core-concepts/systems.mdx
index 7c39ac494..c70858fb3 100644
--- a/docs/astro/src/content/docs/zh/core-concepts/systems.mdx
+++ b/docs/astro/src/content/docs/zh/core-concepts/systems.mdx
@@ -243,11 +243,38 @@ defineSystem([GetWorld()], (world) => {
| `world.has(entity, Component)` | boolean | 检查实体是否有该组件 |
| `world.tryGet(entity, Component)` | data \| null | 安全读取,不存在返回 null |
| `world.valid(entity)` | boolean | 检查实体是否存活 |
+| `world.setParent(child, parent)` | void | 设置父子关系(子实体 Transform 相对于父实体) |
+| `world.removeParent(entity)` | void | 移除父节点,使实体成为根实体 |
<Aside type="tip">
遍历多个实体时优先用 `Query` + `Mut()`——它会自动回写。当你需要**按实体 ID 直接访问已知实体**(如 `findEntityByName()` 之后)时用 `GetWorld()`。
</Aside>
+#### 实体层级
+
+使用 `setParent` / `removeParent` 建立父子关系。子实体的 `Transform` 相对于父实体计算。
+
+```typescript
+import { GetWorld, Commands, Transform, Sprite } from 'esengine';
+
+defineSystem([Commands(), GetWorld()], (cmds, world) => {
+ const parent = cmds.spawn()
+ .insert(Transform, { position: { x: 100, y: 100, z: 0 } })
+ .insert(Sprite, {})
+ .id();
+
+ const child = cmds.spawn()
+ .insert(Transform, { position: { x: 50, y: 0, z: 0 } })
+ .insert(Sprite, {})
+ .id();
+
+ world.setParent(child, parent);
+
+ // 之后可以解除父子关系:
+ // world.removeParent(child);
+});
+```
+
### 组合参数
```typescript