> 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-zh/chang-jing-sdk7/jia-gou/querying-components.md).

# 查询组件

了解如何获取具有共同组件的实体列表，以便更轻松地检查或更新它们。

你可以 [查询组件](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jia-gou/querying-components.md) ，使用方法 `engine.getEntitiesWith(...components)` 来跟踪场景中具有特定组件的所有实体。

[系统](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jia-gou/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-zh/chang-jing-sdk7/jia-gou/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`.

## 处理实体和组件

&#x20;`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-zh/chang-jing-sdk7/jia-gou/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-zh/chang-jing-sdk7/jia-gou/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.
