# States and conditions

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

## Conditional logic

Add conditions on a trigger, so that the action only occurs if those conditions are met. For example, clicking on a door only activates the "Open" action if it wasn't already open.

To add a condition, click the three dots icon next to **Trigger event** and select **Add Trigger Condition**.

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

A single trigger can include multiple conditions. Click the **+** icon to add more conditions. When more than one condition exist, you can select one of these options:

* **All Conditions should be met (AND)**: The trigger only happens if every one of the conditions is true.
* **Any Condition can be met (OR)** The trigger happens if at least one of the conditions is true.

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

### States

The **States** component is included on several smart items. It lists possible states that the smart item can be in. At any given time, the smart item is in one of these states. For example, a door can be *Open* or *Closed*. The Open action sets the state to *Open*, the Close action sets the state to *Closed*.

You can do the following things with states:

1. Use a condition on a trigger to check the state of an entity. In that way the action is only carried out if a specific state is active.

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

2. Change a state via the **Set State** action.

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

3. React to changes in state via the **On State Change** trigger event.

To toggle between two actions, define two triggers, each with a condition that checks a state. For example, doors have one trigger that activates the Open action, with a condition that first checks that the door's state is *Closed*, and another trigger that activates the Close action, with a condition that checks that the door's state is *Open*. Only one of the two is activated each time the player clicks on the door.

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

You can add as many states as you want to a smart item. Just click the **Add New State** button to add another one to the list.

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

One of the states is selected as the default, the item will always start in this state when the scene runs. You can assign a different state to be the default by clicking the three dots next to another one of the states and selecting **Set as Default**.

{% hint style="info" %}
**💡 Tip**: Keep interactions between items simple. For example, avoid scenarios like having a button that opens a door by triggering three actions: play the door's animation, play the door's sound and change the door's state. Instead, make the button change the door's state. Then use an **On State Change** trigger so that the door itself handles playing the animation and sound whenever the state changes.
{% endhint %}

### Counter

Use the **Counter** component to keep track of a number, which can change as the player performs actions in the scene. You can use the values of the counter in conditional logic.

When an entity has a Counter component, you can run the following actions on it:

* **Increment Counter**: Increment the value of the counter by 1.
* **Decrease Counter**: Decrease the value of the counter by 1.
* **Set Counter**: Set the value of the counter to a specific number, for example to set it back to 0.

Use the **On Counter Change** trigger to perform an action every time the counter's value changes. Add a condition to this trigger so that it only activates after passing a certain threshold.

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

On a condition, you can check if the value of the counter is

* Greater than a given value
* Lower than a given value
* Equal to a given value

{% hint style="info" %}
**💡 Tip**: To check for greater or equal, you can add two conditions to the trigger event, using the AND option.

To make an action occur only once when passing a threshold, and not repeat on every increment after that, combine the counter with a **State** component. Set the State to "Done" whenever you reach the desired value, and add a condition to check this state on the trigger event.
{% endhint %}

### See also

* [Smart items - Basics](https://docs.decentraland.org/creator/scene-editor/interactivity/smart-items)
* [Smart items - Advanced](https://docs.decentraland.org/creator/scene-editor/interactivity/smart-items-advanced)
* [Making any item smart](https://docs.decentraland.org/creator/scene-editor/interactivity/make-any-item-smart)
* [Combine with code](https://docs.decentraland.org/creator/scene-editor/extend-with-code/overview)


---

# Agent Instructions: 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:

```
GET https://docs.decentraland.org/creator/scene-editor/interactivity/states-and-conditions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
