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

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

* **所有条件都应满足（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)

### 状态

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

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

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)

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)

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)

你可以为智能物体添加任意多个状态。只需点击 **添加新状态** 按钮即可向列表中添加另一个状态。

![](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)

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

{% 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-c32416d2c7479d26d2cb70ef7f9440d149911e0b%2Fon_counter_change.png?alt=media)

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

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

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