> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/entities-components.md).

# 엔티티 및 컴포넌트

Decentraland 씬의 엔티티와 컴포넌트에 대한 필수 사항을 알아봅니다

Decentraland 씬은 다음을 중심으로 구성됩니다 [*엔티티*, *컴포넌트* 및 *시스템*](https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system). 이는 여러 게임 엔진의 아키텍처에서 사용되는 일반적인 패턴으로, 손쉽게 구성하고 확장할 수 있게 해줍니다.

![](https://3980763956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-47dcd6cd9846e4d62d320e0ba061c9261d2e4f2a%2Fecs-big-picture.webp?alt=media)

## 개요

*엔티티* 는 Decentraland 씬의 모든 요소를 만드는 기본 단위입니다. 씬에서 보이거나 보이지 않는 모든 3D 객체와 오디오 플레이어는 각각 엔티티가 됩니다. 엔티티는 컴포넌트에서 참조할 수 있는 ID일 뿐입니다. 엔티티 자체에는 고유한 속성이나 메서드가 없으며, 여러 컴포넌트를 함께 묶는 역할만 합니다.

*컴포넌트* 는 엔티티의 특성을 정의합니다. 예를 들어, `Transform` 컴포넌트는 엔터티의 좌표, 회전 및 크기 조정 값을 저장합니다.  `MeshRenderer` 컴포넌트는 씬에서 렌더링될 때 엔터티에 큐브나 구와 같은 눈에 보이는 형태를 부여합니다.  `Material` 컴포넌트는 엔터티에 색상이나 텍스처를 부여합니다. 또한 씬에 필요한 데이터를 담는 사용자 지정 컴포넌트를 만들 수도 있습니다. 예를 들어 사용자 지정  `health` 컴포넌트는 엔터티의 남은 체력을 저장할 수 있으며, 게임에서 플레이어가 아닌 적을 나타내는 엔터티에 추가할 수 있습니다.

웹 개발에 익숙하다면 엔터티를 다음과 같은 것이라고 생각하면 됩니다: *요소* 안의 *DOM* 트리, 구성 요소를 *속성으로* 해당 요소의.

에서 [Creator Hub의 씬 편집기](https://docs.decentraland.org/creator/content-creator-ko/scene-editor/get-started/about-editor.md), 엔티티를 선택하면 해당 엔티티에 속한 구성 요소를 확인할 수 있습니다.

![](https://3980763956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-b7c82899df98d40a8760f7e0668ce2aaed751945%2Fcomponents-example.webp?alt=media)

{% hint style="warning" %}
**📔 참고**: 이전 버전의 SDK에서는 엔티티가 *인스턴스화되는 객체* 로, 함수를 추가해 확장할 수 있었습니다. SDK 버전 7.0부터 엔티티는 ID만 나타냅니다. 이 구조는 다음 원칙에 더 잘 부합하며 [데이터 지향 프로그래밍](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/data-oriented-programming.md) 씬의 성능 향상에도 도움이 될 수 있습니다.
{% endhint %}

![](https://3980763956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-a7a82338639fce594aa9a8459ae54b9579f3e0c1%2Fecs-components-new.webp?alt=media)

다음과 같은 컴포넌트 `Transform`, `Material` 또는 다음과 같은 *형태* 컴포넌트는 씬 렌더링과 밀접하게 관련되어 있습니다. 이러한 컴포넌트의 값이 변경되면, 그것만으로도 엔진은 다음 프레임에서 씬을 렌더링하는 방식을 변경할 수 있습니다.

엔진은 씬의 중앙에 위치해 다른 모든 부분을 관리합니다. 어떤 엔티티를 렌더링할지, 플레이어가 엔티티와 어떻게 상호작용할지를 결정합니다. 또한 어떤 함수가 [시스템](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/systems.md) 에서 언제 실행되는지도 조정합니다.

컴포넌트는 자신이 참조하는 엔티티에 관한 데이터를 저장합니다. 컴포넌트는 이 데이터만 저장할 수 있으며, 직접 수정할 수는 없습니다. 컴포넌트의 값을 변경하는 작업은 모두 [시스템](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/systems.md)에서 수행합니다. 시스템은 컴포넌트 및 엔티티 자체와 완전히 분리되어 있습니다. 엔티티와 컴포넌트는 어떤 *시스템* 이 자신에게 작용하는지 알지 못합니다.

## 엔티티와 컴포넌트의 구문

아래 예제에서는 기본 엔티티와 컴포넌트를 선언하고 구성하는 몇 가지 기본 작업을 보여 줍니다.

```ts
export function main() {
	// 엔티티 생성
	const door = engine.addEntity()

	// Transform 컴포넌트로 엔티티에 위치 지정
	Transform.create(door, {
		position: Vector3.create(5, 1, 5),
	})

	// GltfContainer 컴포넌트로 엔티티에 보이는 모양 지정
	GltfContainer.create(door, {
		src: 'assets/models/door.glb',
	})
}
```

{% hint style="warning" %}
**📔 참고**: 이전 버전의 SDK에서는 렌더링을 시작하려면 엔티티를 엔진에 수동으로 추가해야 했습니다. SDK 버전 7부터는 엔티티에 컴포넌트가 할당되는 즉시 엔진에 암시적으로 추가됩니다.
{% endhint %}

컴포넌트를 생성하면 항상 상위 엔티티에 할당됩니다. 그러면 컴포넌트의 값이 해당 엔티티에 영향을 줍니다.

{% hint style="info" %}
**💡 팁**: 엔티티를 하나씩 생성하는 대신, 다음에서 엔티티와 컴포넌트의 전체 트리를 한 번에 생성할 수 있습니다. [컴포지트](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/composites.md) 파일.
{% endhint %}

## 엔티티 제거

엔진에서 엔티티를 제거하려면 `engine.removeEntity()`를 사용합니다. 이 함수는 `boolean`: `true` 엔티티가 제거된 경우, `false` 제거가 거부된 경우를 반환합니다.

```ts
export function main() {
	// 엔티티 생성
	const door = engine.addEntity()

	// GltfContainer 컴포넌트로 엔티티에 보이는 모양 지정
	GltfContainer.create(door, {
		src: 'assets/models/door.glb',
	})

	// 엔티티 제거
	const removed = engine.removeEntity(door)
	console.log('Entity removed:', removed) // true
}
```

제거된 엔티티에 자식 엔티티가 있으면 해당 자식 엔티티의 부모는 기본값인 `engine.RootEntity` 엔티티로 다시 변경됩니다. 이 엔티티는 씬의 기준 위치에 배치되며, 스케일은 *1*.

### 렌더러 예약 엔티티

일부 엔티티 ID는 원격 플레이어 아바타용으로 렌더러에 예약되어 있습니다. 이러한 엔티티는 제거할 수 없습니다. 렌더러 예약 엔티티에 `engine.removeEntity()` 를 호출하면 `false` 가 반환되며 모든 컴포넌트가 그대로 유지됩니다.

이름이 지정된 [예약 엔티티](#reserved-entities) (`engine.RootEntity`, `engine.PlayerEntity`, `engine.CameraEntity`)는 예외입니다: `engine.removeEntity()` 는 여전히 이 엔티티들에 대해 `false` 를 반환하지만(ID가 해제되지 않음), 해당 컴포넌트는 **실제로** 삭제됩니다. 따라서 해당 엔티티에서 자체 컴포넌트를 제거할 수 있습니다(예: 다음을 제거). `InputModifier` 에서 `engine.PlayerEntity`), 엔티티 ID 자체는 해제되지 않습니다.

```ts
// 예약된 엔티티 제거 시도
const result = engine.removeEntity(engine.PlayerEntity)
console.log(result) // false — 엔티티 ID가 해제되지 않음
// 하지만 PlayerEntity에 연결된 씬 자체의 컴포넌트는 제거됩니다
```

엔티티를 제거할 때 반환값을 확인하면 예외 상황을 처리할 수 있습니다.

```ts
if (!engine.removeEntity(someEntity)) {
	console.log('엔티티를 제거할 수 없습니다(예약됨)')
}
```

### 자식 엔티티와 함께 엔티티 제거하기

엔티티와 모든 자식 엔티티(그리고 자식의 자식까지 재귀적으로)를 함께 제거하려면 `removeEntityWithChildren()` 헬퍼를 사용합니다.

```ts
export function main() {
	// 부모 엔티티 생성
	const door = engine.addEntity()

	// 자식 엔티티 생성
	const doorKnob = engine.addEntity()

	// 엔티티에 보이는 모양 지정
	GltfContainer.create(door, {
		src: 'models/door.glb',
	})
	GltfContainer.create(doorKnob, {
		src: 'models/doorKnob.glb',
	})

	// 부모
	Transform.create(doorKnob, {
		parent: door,
	})

	// 부모와 자식 모두 제거
	removeEntityWithChildren(engine, door)
}
```

{% hint style="warning" %}
**참고:** 렌더러가 예약한 엔티티가 트리 내 어디에든 있으면, `removeEntityWithChildren` 는 예약된 엔티티를 그대로 두고 다른 모든 하위 항목을 제거합니다. 예약된 엔티티의 `Transform.parent` 는 제거된 엔티티를 가리키게 됩니다. 씬에서 예약된 엔티티를 씬 엔티티의 자식으로 설정한 경우에만 이런 일이 발생하며, 이는 흔하지 않습니다.
{% endhint %}

{% hint style="info" %}
**💡 팁**: 엔진에서 엔티티를 제거하는 대신, 지연 없이 다시 불러올 수 있도록 일부 경우에는 엔티티를 보이지 않게 만드는 편이 더 나을 수 있습니다. 자세한 내용은 [보이지 않게 만들기](https://docs.decentraland.org/creator/content-creator-ko/sdk7/3d/shape-components.md#make-invisible)
{% endhint %}

### 백그라운드에서 엔티티 제거하기

엔티티는 컴포넌트가 참조하는 ID일 뿐입니다. 따라서 엔티티를 제거한다는 것은 실제로 해당 엔티티를 참조하는 각 컴포넌트를 제거하는 것입니다. 엔티티의 모든 컴포넌트를 직접 제거하면 플레이어에게는 `engine.removeEntity()`를 실행한 것과 똑같이 보입니다. 그러나 `engine.removeEntity()` 이 함수는 엔티티 ID를 더 이상 사용하지 않는 것으로 표시하는 등 내부적으로 추가 작업도 수행하므로, 엔티티 제거에는 항상 이 방법을 사용하는 것이 좋습니다.

## 중첩 엔티티

엔티티는 다른 엔티티를 자식으로 가질 수 있습니다. 이를 활용하면 웹페이지의 HTML처럼 엔티티를 트리 구조로 배치할 수 있습니다.

![](https://3980763956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-05ea75b83fc4f7f0d533368199638177ef38915b%2Fecs-nested-entities-new.webp?alt=media)

한 엔티티를 다른 엔티티의 부모로 설정하려면 자식 엔티티에 `Transform` 컴포넌트가 있어야 합니다. 그런 다음 `parent` 필드에 부모 엔티티의 참조를 설정하면 됩니다.

```ts
export function main() {
	// 엔티티 생성
	const parentEntity = engine.addEntity()

	const childEntity = engine.addEntity()

	// 부모 설정
	Transform.create(childEntity, {
		parent: parentEntity,
	})
}
```

부모를 지정한 뒤에는 자식 엔티티의 `parent` 필드에서 부모를 확인할 수 있습니다. `Transform` 컴포넌트입니다.

```ts
// 엔티티에서 부모 가져오기
const parent = Transform.get(childEntity).parent
```

부모 엔티티에 위치, 크기 또는 회전에 영향을 주는 `Transform` 컴포넌트가 있으면 자식 엔티티에도 영향을 줍니다. 위치와 회전 값은 더해지고, 크기 값은 곱해집니다.

부모 또는 자식 엔티티에 `Transform` 컴포넌트가 없으면 다음 기본값이 사용됩니다.

* 의 경우 **위치**, 부모의 중심은 *0, 0, 0*
* 의 경우 **회전** 부모의 회전은 다음 쿼터니언입니다 *0, 0, 0, 1* (오일러 각으로는 *0, 0, 0*)
* 의 경우 **크기**, 부모의 크기는 다음과 같은 것으로 간주됩니다 *1* 부모의 크기를 조정하면 크기와 위치가 그에 비례해 변경됩니다.

도형 컴포넌트가 없는 엔티티는 장면에서 보이지 않습니다. 이러한 엔티티는 여러 엔티티를 그룹으로 처리하고 배치하는 래퍼로 사용할 수 있습니다.

자식 엔티티를 부모에서 분리하려면 엔티티의 부모를 `engine.RootEntity`.

```ts
const mutableChildTransform = Transform.getMutable(childEntity)
mutableChildTransform.parent = engine.RootEntity
```

{% hint style="warning" %}
**📔 참고**: 다른 플레이어와 동기화되는 중첩 엔티티를 다룰 때는 `parentEntity()`  대신 함수를 사용하세요 `parent` Transform의 엔터티입니다. 자세한 내용은 [부모가 있는 엔터티](https://docs.decentraland.org/creator/content-creator-ko/sdk7/networking/serverless-multiplayer.md#parented-entities)
{% endhint %}

Scene Editor의 왼쪽 패널에서 씬에 중첩된 엔터티의 전체 계층 구조를 확인할 수 있습니다.

![](https://3980763956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-55a529bfbd47e68e693cebfc82a98cb927678757%2Fentity-tree-example.webp?alt=media)

## ID로 엔터티 가져오기

씬의 모든 엔터티에는 고유한 숫자 *id*가 있습니다. 이 ID를 기준으로 엔진에서 특정 엔터티를 참조하는 컴포넌트를 가져올 수 있습니다.

```typescript
// fetch a Transform component
Transform.get(1000 as Entity)
```

{% hint style="warning" %}
**📔 참고**: 다음 사이의 엔터티 ID는 *0* 과 *511* 플레이어 아바타, 기본 씬 등 고정된 엔터티에 사용하도록 엔진에서 예약되어 있습니다.
{% endhint %}

예를 들어 플레이어의 클릭이나 [레이캐스트](https://docs.decentraland.org/creator/content-creator-ko/sdk7/interactivity/raycasting.md) 가 엔터티에 적중하면 적중한 엔터티의 ID가 반환됩니다. 위 명령을 사용해 해당 ID와 일치하는 엔터티의 Transform 컴포넌트를 가져올 수 있습니다. 같은 방식으로 해당 엔터티의 다른 컴포넌트도 가져올 수 있습니다.

## 이름으로 엔터티 가져오기

Scene Editor에서 드래그 앤 드롭으로 엔터티를 추가하면 각 엔터티에 고유한 이름이 지정됩니다. 다음을 사용하세요. `engine.getEntityOrNullByName()` 코드에서 이러한 엔티티 중 하나를 참조하는 함수입니다. 왼쪽 트리 뷰의 Scene Editor UI에 표시된 이름을 문자열로 전달하세요.

```ts
function main() {
	const door = engine.getEntityOrNullByName('door3')
}
```

{% hint style="warning" %}
**📔 참고**: 다음을 사용할 때는 `engine.getEntityOrNullByName()` 를 반드시 `main()` 함수 내부나 `main()`, 또는 시스템에서만 사용하세요. 이러한 맥락 밖에서 사용하면 Scene Editor UI에서 생성한 엔티티가 아직 인스턴스화되지 않았을 수 있습니다.
{% endhint %}

이 메서드로 가져온 엔티티에는 구성 요소 추가 또는 제거, 기존 구성 요소의 값 수정, 엔진에서 엔티티 제거 등 원하는 작업을 자유롭게 수행할 수 있습니다.

```ts
function main() {
	// 엔티티 가져오기
	const door = engine.getEntityOrNullByName('door-3')
	// 엔티티가 있는지 확인
	if (door) {
		// 포인터 이벤트 콜백 추가
		pointerEventsSystem.onPointerDown(
			{
				entity: door,
				opts: { button: InputAction.IA_PRIMARY, hoverText: '열기' },
			},
			function () {
				// 문 열기
			}
		)
	}
}
```

Scene Editor UI를 통해 추가된 모든 엔티티에는 `Name` 컴포넌트가 있습니다. 다음과 같이 모든 엔티티를 반복할 수 있습니다:

```ts
function main() {
	for (const [entity, name] of engine.getEntitiesWith(Name)) {
		console.log({ entity, name })
	}
}
```

## 컴포넌트 추가 또는 교체

각 엔티티에는 같은 종류의 컴포넌트를 하나만 추가할 수 있습니다. 예를 들어, 이미 Transform 컴포넌트가 있는 엔티티에 Transform을 할당하려고 하면 오류가 발생합니다.

이 오류를 방지하려면 `.createOrReplace` 대신 `.create`. 이 명령은 같은 종류의 기존 구성 요소가 있으면 덮어쓰고, 없으면 다음과 같이 새 구성 요소를 만듭니다. `.create`.

```ts
Transform.createOrReplace(door, {
	position: Vector3.create(5, 1, 5),
})
```

{% hint style="warning" %}
**📔 참고**: 다음 이유로 `.createOrReplace` 는 구성 요소를 만들기 전에 추가 검사를 수행하므로, 항상 `.create`를 사용하는 편이 더 효율적입니다. 추가하려는 구성 요소와 같은 구성 요소가 엔터티에 이미 없다고 확신한다면 다음을 사용하세요. `.create`.
{% endhint %}

## 엔터티에서 구성 요소에 접근하기

엔터티의 구성 요소에는 엔터티의 `.get()` 또는 `getMutable()` 함수.

```ts
export function main() {
	// 엔티티 생성
	const box = engine.addEntity()

	// 해당 엔터티에 컴포넌트 생성 및 추가
	Transform.create(box)

	// 컴포넌트의 읽기 전용 버전 가져오기
	let transform = Transform.get(box)

	// 컴포넌트의 변경 가능한 버전 가져오기
	let transform = Transform.getMutable(box)
}
```

이 `get()`  함수는 컴포넌트에 대한 읽기 전용 참조를 가져옵니다. 이 컴포넌트 참조를 통해 값을 변경할 수 없습니다.

컴포넌트의 값을 변경하려면 대신  `getMutable()`  함수를 사용하세요. 컴포넌트의 변경 가능한 버전에서 값을 변경하면 해당 컴포넌트가 속한 엔티티에 직접 영향을 줍니다.

자세한 내용은  [변경 가능한 데이터](https://docs.decentraland.org/creator/content-creator-ko/sdk7/programming-patterns/mutable-data.md) 를 참조하세요.

{% hint style="warning" %}
**📔 참고**: 다음 경우에만 `getMutable()` 을 사용하세요. 컴포넌트 값을 실제로 변경하려는 경우입니다. 그렇지 않다면 항상 `get()`을 사용하세요. 이 방법은 [데이터 지향 프로그래밍](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/data-oriented-programming.md)의 원칙을 따르며, 씬의 성능을 크게 향상할 수 있습니다.
{% endhint %}

```ts
// 컴포넌트의 변경 가능한 버전 가져오기
let transform = Transform.getMutable(box)

// 컴포넌트의 값 변경
transform.scale.x = 5
```

위의 예제는 Transform 컴포넌트의 *x* 축척 값을 직접 수정합니다.

가져오려는 컴포넌트가 엔티티에 있는지 확실하지 않다면 `getOrNull()` 또는 `getMutableOrNull()`.

{% hint style="warning" %}
**📔 참고**: 가능하면 `getOrNull()` 또는 `getMutableOrNull()` 은 사용하지 마세요. 이러한 함수는 추가 검사를 수행하므로 다음 함수보다 효율이 낮습니다. `.get()` 및 `getMutable()`.
{% endhint %}

```ts
// getOrNull
const transformOrNull = Transform.getOrNull(myEntity)

// getMutableOrNull
const mutableTransformOrNull = Transform.getMutableOrNull(myEntity)
```

가져오려는 컴포넌트가 엔티티에 없는 경우:

* `get()` 및 `getMutable()` 은 오류를 반환합니다.
* `getOrNull()` 및 `getMutableOrNull()` 은 `Null`.

## 엔티티에서 컴포넌트 제거하기

엔티티에서 컴포넌트를 제거하려면 컴포넌트 유형의 엔티티 `deleteFrom()` 메서드를 사용하세요.

```ts
Transform.deleteFrom(myEntity)
```

엔티티에 없는 컴포넌트를 제거하려고 해도 오류가 발생하지 않습니다.

{% hint style="warning" %}
**📔 참고**: 엔티티의 모든 컴포넌트를 한 번에 제거하는 방법은 [이 섹션](#remove-entities)
{% endhint %}

## 컴포넌트 확인하기

특정 컴포넌트의 인스턴스를 엔티티가 가지고 있는지 확인하려면 다음을 사용하세요. `has()`  함수입니다. 이 함수는 *true* 컴포넌트가 있으면 반환하고, *false* 없으면 반환합니다. 씬에서 조건부 로직을 사용할 때 매우 유용합니다.

```ts
const hasTransform = Transform.has(myEntity)
```

{% hint style="info" %}
**💡 팁**: 다음과 같이 [컴포넌트를 조회할 수도 있습니다](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/querying-components.md) 특정 컴포넌트 또는 특정 컴포넌트 집합을 보유한 전체 엔티티 목록을 가져오려면 다음을 사용하세요. 각 엔티티에 대해 `has()`,를 사용해 일일이 확인하려고 씬의 모든 엔티티를 직접 순회하지 마세요. 이 방식은 훨씬 비효율적입니다.
{% endhint %}

## 컴포넌트 변경 사항 확인

다음 `onChange` 함수를 사용하면 특정 엔티티의 컴포넌트 값이 변경될 때마다 콜백 함수를 실행할 수 있습니다. 모든 컴포넌트에서 사용할 수 있으며, 코드를 읽기 쉽게 유지하는 데 큰 도움이 됩니다.

콜백 함수에는 컴포넌트의 새 상태가 포함된 입력 매개변수를 지정할 수 있습니다.

```ts
Transform.onChange(cubeEntity, (newTransform) => {
	if (!newTransform) return
	console.log(
		'Cube position changed: ',
		newTransform.position,
		newTransform.rotation
	)
})

VisibilityComponent.onChange(cubeEntity, (newVisibilityComponent) => {
	if (!newVisibilityComponent) return
	console.log('Cube visibility changed: ', newVisibilityComponent.visible)
})
```

컴포넌트가 엔티티에서 제거되면 함수는 다음 입력값과 함께 호출됩니다. `undefined`.

{% hint style="info" %}
**💡 팁**: the `.onChange()` 함수는 SDK의 기본 컴포넌트와 [사용자 지정 컴포넌트](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/custom-components.md) 모두에서 작동합니다. 사용자 지정 컴포넌트는 제작자가 정의합니다.
{% endhint %}

## 자식 엔터티 가져오기

부모 엔터티의 직계 자식인 모든 엔터티에 액세스하려면 `getEntitiesWithParent`. 인수로는 `engine` 와 `parent` 엔티티를 인수로 받아 해당 엔티티를 부모로 둔 모든 엔티티의 목록을 반환합니다. 직계 자식만 반환하며, 자식의 자식은 반환하지 않습니다.

```ts
import { getEntitiesWithParent } from '@dcl/sdk/ecs'

const children = getEntitiesWithParent(engine, myEntity)
for (const child of children) {
   // 각 자식 엔티티 처리
}
```

중첩된 깊이에 관계없이 엔티티의 모든 하위 항목에 액세스하려면 대신 다음 함수를 사용하세요. `getComponentEntityTree()`. 계층 구조를 단계별로 직접 탐색하는 대신, 이 함수는 반복하기 쉬운 모든 하위 항목의 평탄화된 목록을 반환합니다. 또한 지정된 컴포넌트 또는 컴포넌트 목록을 가진 엔티티만 필터링합니다.

```ts
import { getComponentEntityTree } from '@dcl/sdk/ecs'

export function main() {
	// 중첩된 자식이 있는 부모 엔티티 만들기
	const parentEntity = engine.addEntity()
	Transform.create(parentEntity, {
		position: Vector3.create(8, 0, 8),
	})

	// ... 부모에게 여러 자식과 손자가 있다고 가정합니다

	// 부모 엔티티의 모든 하위 엔티티를 순회합니다
	for (const descendantEntity of getComponentEntityTree(
		engine,
		parentEntity,
		Transform
	)) {
		// 각 하위 엔티티에 접근합니다
		const transform = Transform.get(descendantEntity)
		console.log('Descendant position:', transform.position)
	}
}
```

다음  `getComponentEntityTree`  함수는 세 개의 매개변수를 받습니다:

* `engine`: 엔티티를 실행하는 엔진 인스턴스
* `entity`: 시작점이 되는 루트 엔티티
* `component`: 필터링 기준이 되는 컴포넌트(공간 계층 구조에서는 일반적으로  `Transform`  사용)

이 함수는 트리 구조의 각 하위 엔티티를 차례로 반환하는 제너레이터를 반환합니다. 지정된 컴포넌트가 있는 엔티티만 결과에 포함됩니다.

이를 다른 컴포넌트 검사와 결합하여 계층 구조에서 특정 엔티티를 찾을 수 있습니다:

```ts
// 특정 이름을 가진 모든 하위 엔티티를 찾습니다
for (const descendantEntity of getComponentEntityTree(
	engine,
	parentEntity,
	Transform
)) {
	const name = Name.getOrNull(descendantEntity)
	if (name && name.value === 'targetEntity') {
		console.log('Found target entity:', descendantEntity)
	}
}
```

## 예약된 엔티티

일부 엔티티 ID는 모든 씬에 존재하는 특수 엔티티를 위해 예약되어 있습니다. 다음 별칭을 통해 접근할 수 있습니다:

* `engine.RootEntity`
* `engine.PlayerEntity`
* `engine.CameraEntity`

{% hint style="warning" %}
**📔 참고**: 이러한 엔티티가 초기화되기 전에 참조하지 마세요. 이 문제를 방지하려면  `main()`  함수나 시스템에서 해당 엔티티를 참조하세요.
{% endhint %}

## 루트 엔티티

씬의 모든 엔티티는 직간접적으로  `engine.RootEntity`의 자식입니다.

이 엔티티에는 Transform 컴포넌트가 없지만, 다음과 같은 전역 설정을 나타내는 여러 컴포넌트를 처리하는 데 사용됩니다:  [스카이박스 제어](https://docs.decentraland.org/creator/content-creator-ko/sdk7/interactivity/skybox-control.md), [커서 위치](https://docs.decentraland.org/creator/content-creator-ko/sdk7/interactivity/user-data.md#check-the-players-cursor-position) 또는  [화면 크기](https://docs.decentraland.org/creator/content-creator-ko/sdk7/2d-ui/ui-positioning.md#responsive-ui-size).

## 플레이어 엔티티

엔티티  `engine.PlayerEntity` 는 플레이어의 아바타를 나타냅니다.

플레이어의 현재 위치와 회전을 가져오려면 플레이어의  `Transform`  컴포넌트를 가져오세요. 자세한 내용은 다음을 참조하세요:  [사용자 데이터](https://docs.decentraland.org/creator/content-creator-ko/sdk7/interactivity/user-data.md). 플레이어의 Transform은 읽기 전용이므로 수정하려면 다음을 사용하세요:  `movePlayerTo()` 함수, [자세히 알아보기](https://docs.decentraland.org/creator/content-creator-ko/sdk7/interactivity/avatars/move-player.md).

객체를 이 엔티티의 자식으로 설정하여 플레이어에 연결할 수도 있지만, [플레이어에 연결](https://docs.decentraland.org/creator/content-creator-ko/sdk7/3d/entity-positioning.md#attach-an-entity-to-an-avatar) 을 사용하는 것이 더 나은 경우가 많습니다.

## 카메라 엔티티

&#x20;`engine.CameraEntity` 엔티티는 플레이어의 카메라를 나타냅니다.

카메라의 `Transform` 컴포넌트를 가져와 카메라의 위치와 회전을 확인하세요. 이 엔티티의 Transform도 읽기 전용입니다. 카메라 각도나 위치를 수정하려면 [가상 카메라](https://docs.decentraland.org/creator/content-creator-ko/sdk7/3d/camera.md#using-virtual-cameras).

카메라의 `CameraMode` 컴포넌트를 가져와 플레이어가 1인칭 또는 3인칭 카메라 모드를 사용 중인지 확인할 수도 있습니다. 자세한 내용은 [카메라 모드](https://docs.decentraland.org/creator/content-creator-ko/sdk7/interactivity/user-data.md#check-the-players-camera-mode).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/entities-components.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
