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

# 컴포넌트 조회

공통 컴포넌트를 가진 엔티티 목록을 가져오는 방법을 알아보고, 이를 더 쉽게 확인하거나 업데이트하는 방법을 배웁니다.

다음과 같이 [컴포넌트를 쿼리할](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/querying-components.md) 수 있습니다. 메서드 `engine.getEntitiesWith(...components)` 를 사용하면 특정 컴포넌트를 가진 씬의 모든 엔티티를 추적할 수 있습니다.

[시스템은](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/systems.md) 일반적으로 이러한 쿼리의 엔티티를 순회하며 각 엔티티에 동일한 작업을 수행합니다. 유효한 엔티티 그룹을 미리 정의해 두면 리소스를 절약할 수 있습니다. 특히 게임 루프의 매 틱마다 실행되는 함수에서 유용합니다. 시스템이 매 틱마다 씬의 모든 엔티티를 순회해 필요한 엔티티를 찾아야 한다면 매우 비효율적입니다.

다음과 같이 쿼리의 엔티티에 액세스할 수 있습니다.

```ts
for (const [entity] of engine.getEntitiesWith(Transform)) {
	//...
}
```

## 필수 컴포넌트

쿼리를 만들 때 그룹에 추가될 모든 엔티티에 있어야 하는 컴포넌트를 지정합니다. 원하는 만큼 컴포넌트를 나열할 수 있으며, 쿼리는 나열된 컴포넌트를 **모두** 가진 엔티티만 반환합니다.

```ts
for (const [entity] of engine.getEntitiesWith(
	Transform,
	Physics,
	NextPosition
)) {
	//...
}
```

{% hint style="info" %}
**💡 팁**: 쿼리에서 처리할 필요가 없는 엔티티가 반환된다면, 다음과 같은 역할을 하는 사용자 지정 컴포넌트를 만드는 것을 고려해 보세요. [플래그](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/custom-components.md#components-as-flags). 이 컴포넌트에는 속성이 없어도 되지만, 다르게 처리하려는 특정 엔티티 하위 그룹을 표시하는 데 사용할 수 있습니다.
{% endhint %}

## 시스템에서 쿼리 사용하기

```ts
// 시스템 정의
function PhysicsSystem(dt: number) {
	// Transform 컴포넌트와 Physics 컴포넌트를 모두 포함하는 엔티티를 쿼리합니다
	for (const [entity] of engine.getEntitiesWith(Transform, Physics)) {
		const transform = Transform.getMutable(entity)
		const vel = Physics.get(entity).velocity
		transform.position.x += vel.x
		transform.position.y += vel.y
		transform.position.z += vel.z
	}
}

// 엔진에 시스템 추가
engine.addSystem(PhysicsSystem)
```

위 예제에서 `PhysicsSystem` 함수는 쿼리에 포함된 엔티티를 순회하며, 이 작업은 게임 루프의 매 틱마다 실행됩니다.

* 씬에 여러 *공* 엔티티가 있고 각 엔티티에 `Transform` 및 `Physics` 컴포넌트가 있다면, 해당 엔티티들이 처리되고 매 틱마다 위치가 업데이트됩니다.
* 씬에 다른 엔티티도 있는 경우를 생각해 보겠습니다. 예를 들어 *골대* 와 *점수판* 이 있고, 이 엔티티들에는 `Transform` 만 있으며 `Physics` 컴포넌트는 다음의 영향을 받지 않습니다. `PhysicsSystem`.

## 엔티티와 컴포넌트 다루기

다음 `getEntitiesWith` 함수는 엔티티 집합에 대한 참조를 포함하는 컬렉션을 반환하며, 선택적으로 나열된 컴포넌트에 대한 참조도 포함할 수 있습니다.

가장 간단한 구문을 사용하면 해당 엔티티에 대한 참조 목록만 가져옵니다.

```ts
for (const [entity] of engine.getEntitiesWith(myComponent, myOtherComponent)) {
	//...
}
```

이 엔티티 목록을 순회하는 동안, 다음을 사용해 각 엔티티의 컴포넌트를 읽기 전용 또는 변경 가능한 형태로 가져올 수 있습니다. `.get` 또는 `getMutable`.

```ts
for (const [entity] of engine.getEntitiesWith(Transform)) {
	// 읽기 전용 버전 가져오기
	const transformReadOnly = Transform.get(entity)

	// 변경 가능한 버전 가져오기
	const transformMutable = Transform.getMutable(entity)
}
```

선택적으로 쿼리에서 반환되는 컬렉션의 일부로 나열된 각 컴포넌트에 대한 참조를 직접 가져올 수도 있습니다. 이렇게 하려면 가져오려는 각 컴포넌트마다 하나씩 여러 참조를 함께 선언하면 됩니다. 참조 추가는 선택 사항이며, 쿼리에 있는 *모든* 컴포넌트에 대한 참조를 선언할 필요도 없습니다.

```ts
// 엔터티와 첫 번째로 나열된 컴포넌트에 대한 참조를 반환
for (const [entity, component1] of engine.getEntitiesWith(
	MyCustomComponent1,
	MyCustomComponent2
)) {
	// 엔티티 목록 순회
}

// 엔티티와 처음 나열된 두 컴포넌트에 대한 참조를 반환
for (const [entity, component1, component2] of engine.getEntitiesWith(
	MyCustomComponent1,
	MyCustomComponent2
)) {
	// 엔티티 목록 순회
}
```

{% hint style="warning" %}
**📔 참고**: 이러한 참조는 읽기 전용입니다. 해당 컴포넌트의 변경 가능한 버전을 가져오려면 엔티티를 참조하는 `.getMutable` 함수를 사용해야 합니다.
{% endhint %}

그런 다음 결과 컬렉션을 반복하면서 이러한 참조를 사용할 수 있습니다. 각 항목에서 엔터티와 해당 컴포넌트 참조에 접근할 수 있습니다.

```ts
for (const [entity, transformReadOnly] of engine.getEntitiesWith(Transform)) {
	console.log('entity id: ', entity)
	console.log('has position : ', transformReadOnly.position)
}
```

## 변경 사항 구독

일반적인 사용 사례는 특정 컴포넌트의 데이터가 변경될 때만 함수를 실행하는 것입니다. 다음을 사용하면 [OnChange](https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/subscribe-to-changes.md) 시스템을 정의하거나 이전 값과 새 값을 명시적으로 비교할 필요 없이 함수를 실행할 수 있습니다.


---

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