> 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).

# 컴포넌트 쿼리

공통 컴포넌트를 가진 엔티티 목록을 가져와 확인이나 업데이트를 더 쉽게 하는 방법을 알아보세요.

할 수 있습니다 [컴포넌트를 조회](/creator/content-creator-ko/sdk7/architecture/querying-components.md) 메서드를 사용하여 `engine.getEntitiesWith(...components)` 특정 컴포넌트를 가진 씬의 모든 엔티티를 추적할 수 있습니다.

[시스템](/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" %}
**💡 팁**: 쿼리가 처리할 필요가 없는 엔티티를 반환한다면, 사용자 정의 컴포넌트를 만들어 다음 역할을 하도록 고려해 보세요 [플래그](/creator/content-creator-ko/sdk7/architecture/entities-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` 컴포넌트가 있다면, 이들은 처리되고 위치가 매 틱마다 업데이트됩니다.
* 씬에 다른 엔티티도 있다면, 예를 들어 *골대* 그리고 *scoreBoard* 오직 하나의 `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](/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
