> 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-7bb63e09d7e61b57709e9fdc983047a25e33dc5a%2Fcondition.png?alt=media\&token=0307c6b8-b072-4bcf-805b-3b85f6793d09)

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

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

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-588927ea5768c6e3a1490d5335c5d89047a1fe2c%2Fmultiple-conditions.png?alt=media\&token=889132f5-cb4f-432f-ba48-82b042e7ef65)

### States

该 **States** 组件包含在多个智能物体上。它列出了该智能物体可能处于的状态。在任何给定时刻，智能物体都处于这些状态之一。例如，一扇门可以是 *打开* 或 *关闭*。打开操作将状态设置为 *打开*，关闭操作将状态设置为 *关闭*.

你可以对状态执行以下操作：

1. 在触发器上使用条件来检查实体的状态。这样只有在某个特定状态处于激活时才会执行该操作。

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-7bb63e09d7e61b57709e9fdc983047a25e33dc5a%2Fcondition.png?alt=media\&token=0307c6b8-b072-4bcf-805b-3b85f6793d09)

2. 通过……更改状态 **设置状态** 操作显示的图片时。

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-a1d45062ce10c08e424bc3948bf2d7eaff1b15be%2Fset-state.png?alt=media\&token=b02a5064-a480-4be6-865a-707a7e97c725)

3. 通过……对状态变化作出响应 **状态更改时** 触发事件。

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

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-03ba26fa1e6024bd4811bac4289684be640d33c3%2Fdoor_conditions.png?alt=media\&token=e4cd0c18-b41c-4549-8e94-57c85c95e412)

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

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-56792d2ce50169a8f8c2707c74297df1a024aed1%2Fnew_state.png?alt=media\&token=8c933266-0c66-405a-a4ce-7374ae5e7f6b)

其中一个状态被选为默认状态，场景运行时该物体将始终以此状态开始。你可以通过点击另一个状态旁边的三个点并选择 **设为默认**.

{% hint style="info" %}
**💡 提示**：保持物体之间的交互简单。例如，避免像这样：用一个按钮通过触发三个操作来打开门：播放门的动画、播放门的声音以及更改门的状态。相反，让按钮更改门的状态。然后使用一个 **状态更改时** 触发器，这样当状态变化时，门本身会负责播放动画和声音。
{% endhint %}

### Counter

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

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

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

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

![](https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-c32416d2c7479d26d2cb70ef7f9440d149911e0b%2Fon_counter_change.png?alt=media\&token=708c6da5-7773-4899-b45e-64f9e47e8e28)

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

* 大于给定值
* 小于给定值
* 等于给定值

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

为了让某个操作在越过阈值时只发生一次 **状态** 组件结合使用。每当达到所需数值时，将状态设置为“完成”，并在触发事件上添加一个条件来检查此状态。
{% endhint %}

### 另请参阅

* [智能项目 - 基础](/creator/content-creator-zh/chang-jing-bian-ji-qi/jiao-hu-xing/smart-items.md)
* [智能物件 - 高级](/creator/content-creator-zh/chang-jing-bian-ji-qi/jiao-hu-xing/smart-items-advanced.md)
* [让任意项目变为智能项目](/creator/content-creator-zh/chang-jing-bian-ji-qi/jiao-hu-xing/make-any-item-smart.md)
* [与代码结合](/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.
