> 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/bian-cheng-mo-shi/mutable-data.md).

# 可变数据

了解如何处理组件中的只读和可变数据

当引用来自某个 [组件](/creator/content-creator-zh/chang-jing-sdk7/jia-gou/entities-components.md)时，你可以获取可变版本或只读（不可变）版本。

在可能的情况下，你应该始终处理数据的只读版本。与始终处理同一数据的可变版本相比，这种做法可以显著提升场景性能。

该 `.get()` 组件中的函数会返回该组件的只读（不可变）版本。你只能读取其值，不能更改其任何属性。

该 `.getMutable()` 函数会返回一个允许你更改其值的组件版本。只有当你计划修改组件时才使用可变版本，否则始终使用 `get()`.

```ts
// 获取只读（不可变）版本
const immutableTransform = Transform.get(myEntity)

// 以下代码不会生效：
// immutableTransform.position.y = 2

const mutableTransform = Transform.getMutable(myEntity)

// 以下这行确实会更改实体的位置
mutableTransform.position.y = 2
```

一个好的做法是遍历只读组件以检查其值，然后仅在需要更改时获取单个组件的可变版本。

```ts
// 硬编码的最大高度
const MAX_HEIGHT = 10

// 定义系统
function HeightLimitSystem(dt: number) {
	// 遍历所有具有 Transform 组件的实体
	for (const [entity] of engine.getEntitiesWith(Transform)) {
		// 获取只读值
		const currentHeight = Transform.get(entity).position.y

		// 比较数值
		if (currentHeight > MAX_HEIGHT) {
			// 获取可变版本以进行更改
			const mutableTransform = Transform.getMutable(entity)

			// 更改变换
			mutableTransform.position.y = MAX_HEIGHT
		}
	}
}

// 将系统添加到引擎
engine.addSystem(HeightLimitSystem)
```

在上面的示例中，系统会检查实体的 `Transform` 组件的只读值。每一帧它都会检查位置的 *y* 是否高于硬编码的最大高度。如果变换中的高度确实高于此限制，那么并且只有在那时我们才获取 Transform 的可变版本。对于场景来说，这看起来可能像是额外的工作，但在一个我们在游戏循环的每一帧都检查数值、只在偶尔进行更改的场景中，它会带来巨大的性能提升。

这种做法遵循了 [数据导向编程](/creator/content-creator-zh/chang-jing-sdk7/jia-gou/data-oriented-programming.md)的原则。由于它带来的巨大改进，它也正逐渐被游戏行业采纳为一种行业标准实践。

{% hint style="warning" %}
**📔 注意**：在 SDK 的旧版本（6.x 或更早）中，组件始终被视为可变的。那种模式可能更容易学习一些，但运行效率要低得多。
{% endhint %}


---

# 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-zh/chang-jing-sdk7/bian-cheng-mo-shi/mutable-data.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.
