> 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/composites.md).

# 복합체

복합 파일에서 런타임에 엔티티와 컴포넌트의 트리를 생성합니다

다음은 *컴포지트* 의 트리를 설명하는 파일입니다. [엔티티와 컴포넌트](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/entities-components.md). 각 엔티티를 하나씩 만드는 코드를 작성하는 대신, 컴포지트 파일에 구조를 저장한 다음 한 번의 호출로 전체를 생성할 수 있습니다.

이미 컴포지트를 사용하고 있으면서도 눈치채지 못했을 수 있습니다.

* 모든 씬에는 `main.composite` 파일이 있습니다. 이 파일에는 [씬 에디터](https://docs.decentraland.org/creator/content-creator-ko/scene-editor/get-started/about-editor.md)에서 시각적으로 추가하고 구성한 모든 항목이 들어 있습니다. 씬이 시작되면 이 파일이 자동으로 로드됩니다.
* [사용자 지정 아이템](https://docs.decentraland.org/creator/content-creator-ko/scene-editor/interactivity/custom-items.md) 은 `composite.json` 파일로 저장됩니다. 각 사용자 지정 아이템은 여러 씬에서 재사용할 수 있는 컴포지트입니다.

이 페이지에서는 런타임에 코드에서 컴포지트를 생성하는 방법을 다룹니다. 예를 들어 씬 로직의 일부로 사용자 지정 아이템을 동적으로 생성할 수 있습니다.

## 컴포지트 생성하기

컴포지트를 생성하려면 두 단계가 필요합니다. **로드** 한 다음 **생성** 합니다. 로드는 비동기식이고 생성은 동기식이므로, 항상 먼저 로드한 다음 생성해야 합니다.

```ts
import { engine, Composite, getCompositeProvider } from "@dcl/sdk/ecs";

export async function spawnBarrel() {
  const src = "barrel.composite";
  const provider = getCompositeProvider();
  if (!provider || !provider.loadComposite) return;

  // 1. 파일에서 컴포지트 로드
  const resource = await provider.loadComposite(src);

  // 2. 생성: 컴포지트의 모든 엔티티와 컴포넌트를 만듭니다
  const barrel = Composite.instance(engine, resource, provider);
  return barrel;
}
```

`provider.loadComposite(src)` 는 컴포지트 파일을 읽어 메모리에 로드합니다. 일반적인 씬은 `@dcl/sdk`로 빌드되며, 컴포지트 제공자가 이미 설정되어 있습니다. 다음을 사용해 가져올 수 있습니다. `getCompositeProvider()` 함수는 `@dcl/sdk/ecs`.

`Composite.instance()` 는 컴포지트에 설명된 모든 엔티티와 컴포넌트를 생성한 다음, 생성된 트리의 **루트 엔티티** 를 반환합니다. 이 엔티티를 사용하면 나중에 컴포넌트를 읽거나 변경할 수 있습니다. 예를 들어 생성된 아이템의 위치를 바꾸거나 제거할 수 있습니다.

{% hint style="info" %}
**💡 팁**: 코드를 작성하지 않고 같은 작업을 하려면 씬 에디터에서 **엔티티 생성** 액션을 사용하세요. 자세한 내용은 [엔티티 생성에 관하여](https://docs.decentraland.org/creator/content-creator-ko/scene-editor/interactivity/smart-items-advanced.md#about-spawning-entities).
{% endhint %}

{% hint style="warning" %}
**📔 참고**: 컴포지트를 생성하기 전에 먼저 로드해야 합니다. `Composite.instance()` 은 동기식이며 메모리에 이미 있는 컴포지트만 생성할 수 있습니다.  `main.composite` 만 씬과 함께 번들로 제공되어 처음부터 사용할 수 있습니다. 다른 컴포지트 파일은 먼저  `loadComposite()` 를 사용해 로드해야 합니다. 컴포지트를 이미 사용할 수 있는지 동기식으로 확인하려면  `provider.getCompositeOrNull(src)`.

`loadComposite()` 는 멱등적입니다. 각 컴포지트를 해당  `src` 문자열을 기준으로 식별하므로, 같은 경로로 다시 호출해도 파일을 다시 로드하지 않고 이미 로드된 컴포지트를 반환합니다. 같은 파일이 두 번 로드될 걱정 없이 스폰하기 전에 매번 안전하게 호출할 수 있습니다.
{% endhint %}

### 기존 엔티티에 스폰하기

기본적으로  `Composite.instance()` 는 스폰된 트리를 담을 새 엔티티를 생성합니다. 이미 보유한 엔티티에 스폰하려면 옵션에  `rootEntity` 를 전달합니다.

```ts
// 래퍼 엔티티 없이 씬 루트에 직접 컴포지트를 스폰합니다
const barrel = Composite.instance(engine, resource, provider, {
  rootEntity: engine.RootEntity,
})
```

## 스폰된 컴포지트 위치 지정하기

스폰된 컴포지트를 특정 위치, 회전 또는 크기에 배치하려면  `Transform` 컴포넌트를  `Composite.instance()`.

```ts
import { engine, Composite, getCompositeProvider, Transform } from "@dcl/sdk/ecs";
import { Vector3 } from "@dcl/sdk/math";

export async function spawnBarrel() {
  const src = "barrel.composite";
  const provider = getCompositeProvider();
  if (!provider || !provider.loadComposite) return;

  const resource = await provider.loadComposite(src);

  // 특정 위치에 컴포지트를 스폰합니다
  const barrel = Composite.instance(engine, resource, provider);
  Transform.createOrReplace(barrel, {
    position: Vector3.create(8, 0, 8),
  });
}
```

{% hint style="warning" %}
**📔 참고**: `Transform.createOrReplace()` 는 루트 엔티티의 기존 Transform 컴포넌트를 대체합니다. 루트 엔티티에만 영향을 주며, 자식 엔티티는 루트를 기준으로 한 위치를 유지합니다.
{% endhint %}

## 현재 제한 사항: 중첩 컴포지트

독립형 컴포지트와 Custom Items는 스폰할 수 있습니다. 컴포지트의 엔티티에서 다른 컴포지트를 참조할 수 있지만,  `loadComposite()` 는 이러한 중첩 참조를  **재귀적으로** 로드하지 않습니다. 전달된 파일만 로드합니다.

스폰할 때  `Composite.instance()` 는 메모리에 이미 있는 중첩 참조를 재귀적으로 처리합니다. 중첩 컴포지트가 로드되지 않은 경우(씬과 함께 번들로 제공되는 유일한 컴포지트인  `main.composite`가 아닌 경우), 해당 분기에 경고가 기록되고 건너뜁니다. 중첩 참조가 있는 컴포지트를 스폰하려면 먼저 각 참조 파일에 대해  `loadComposite()` 를 호출하세요. 더 간단한 방법은 스폰하는 컴포지트를 독립형으로 유지하는 것입니다.

## 관련 페이지

* [엔티티 및 컴포넌트](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/entities-components.md) — 컴포지트가 설명하는 기본 구성 요소입니다.
* [Custom Items](https://docs.decentraland.org/creator/content-creator-ko/scene-editor/interactivity/custom-items.md) — 컴포지트로 저장되는 재사용 가능한 항목입니다.
* [스마트 항목 - 고급](https://docs.decentraland.org/creator/content-creator-ko/scene-editor/interactivity/smart-items-advanced.md#about-spawning-entities) — 코드 없이 엔터티 생성 작업을 사용해 컴포지트를 생성합니다.
* [씬 파일](https://docs.decentraland.org/creator/content-creator-ko/sdk7/kinds-of-projects/scene-files.md) — 위치 `main.composite` 는 씬 프로젝트에 있습니다.


---

# 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/composites.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.
