> 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-bian-ji-qi/jiao-hu-xing/states-and-conditions.md).

# 状态与条件

管理物品状态和条件逻辑

{% embed url="<https://www.youtube.com/watch?v=wm8ZD2kSyKA>" %}

## 条件逻辑

为触发器添加条件，使操作仅在满足这些条件时执行。例如，点击一扇门时，只有门尚未打开才会触发“打开”操作。

要添加条件，请点击 **触发事件** 旁边的三点图标，然后选择 **添加触发器条件**.

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-b2775405d3a1e158daf49e89b0b1e7af8f44ce40%2Fcondition.webp?alt=media)

一个触发器可以包含多个条件。点击 **+** 图标可添加更多条件。当存在多个条件时，可以选择以下选项之一：

* **必须满足所有条件（AND）**：只有所有条件都为真时，触发器才会触发。
* **满足任一条件即可（OR）** 只要至少有一个条件为真，触发器就会触发。

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-ea7940d5ed373be8d0845709468df96aa47e875c%2Fmultiple-conditions.webp?alt=media)

### 状态

一些智能物品包含 **状态** 组件，其中列出了智能物品可能处于的状态。智能物品在任意时刻都处于其中一种状态。例如，一扇门可以处于 *Open* 或 *Closed*状态。“打开”操作将状态设为 *Open*，“关闭”操作将状态设为 *Closed*.

你可以使用状态执行以下操作：

1. 使用触发器上的条件检查实体的状态，使操作仅在特定状态生效时执行。

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-b2775405d3a1e158daf49e89b0b1e7af8f44ce40%2Fcondition.webp?alt=media)

2. 通过 **设置状态** 操作更改状态。

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-c1a1bebf1536c6d3e8bf25cd9164aef651b9d9a9%2Fset-state.webp?alt=media)

3. 通过 **状态变更时** 触发事件响应状态变化。

要在两个操作之间切换，请定义两个触发器，每个触发器都带有一个检查状态的条件。例如，门有一个触发“打开”操作的触发器，其条件会先检查门的状态是否为 *Closed*；另一个触发器触发“关闭”操作，其条件会检查门的状态是否为 *Open*。玩家每次点击门时，这两个触发器中只有一个会被激活。

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-4cc97917b39c5abf24309ea1ff45021d652282db%2Fdoor_conditions.webp?alt=media)

你可以为智能物品添加任意数量的状态。只需点击 **添加新状态** 按钮，即可向列表中添加一个状态。

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-2eae40fd0b8eb62479d4a87fde4cc9f87e1c7631%2Fnew_state.webp?alt=media)

其中一个状态会被选为默认状态，场景运行时物品始终从该状态开始。要将其他状态设为默认状态，请点击该状态旁边的三点图标，然后选择 **设为默认**.

{% hint style="info" %}
**💡 提示**：保持物品之间的交互简单。例如，避免使用按钮通过触发三个操作来开门：播放门的动画、播放门的声音以及更改门的状态。应改为让按钮更改门的状态，然后使用 **状态变更时** 触发器，让门在状态发生变化时自行处理动画和声音的播放。
{% endhint %}

### 计数器

使用 **计数器** 组件记录一个数值，该数值可随着玩家在场景中执行操作而变化。你可以在条件逻辑中使用计数器的值。

当实体具有计数器组件时，你可以对其执行以下操作：

* **增加计数器**：将计数器的值增加一个可配置的 **数量**，默认值为 1。
* **减少计数器**：按可配置的 **数量**，默认值为 1。
* **设置计数器**：将计数器的值设置为特定数字，例如将其重置为 0。

使用 **计数器值变化时** 触发器，在每次数值变化时执行操作。为此触发器添加条件，使其仅在超过某个阈值后触发。

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-bd7370032dd98768db05fb39d072082cf6cd6682%2Fon_counter_change.webp?alt=media)

在条件中，可以检查计数器的值是否

* 大于指定值
* 小于指定值
* 等于指定值

{% hint style="info" %}
**💡 提示**：要检查是否大于或等于，可以使用 AND 选项，在触发事件中添加两个条件。

若要在超过阈值时仅执行一次操作，而不是此后每次递增时都重复执行，请将计数器与 **状态** 组件结合使用。达到所需值时，将状态设为“Done”，并在触发事件中添加条件来检查此状态。
{% endhint %}

### 另请参阅

* [智能项目 - 基础](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-bian-ji-qi/jiao-hu-xing/smart-items.md)
* [智能项目 - 进阶](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-bian-ji-qi/jiao-hu-xing/smart-items-advanced.md)
* [让任何项目变得智能](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-bian-ji-qi/jiao-hu-xing/make-any-item-smart.md)
* [与代码结合](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-bian-ji-qi/shi-yong-dai-ma-kuo-zhan/overview.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-bian-ji-qi/jiao-hu-xing/states-and-conditions.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.
