> For the complete documentation index, see [llms.txt](https://pahutyakv.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pahutyakv.gitbook.io/docs/delayed/condition-based-actions.md).

# Condition-Based Actions

The `Delayed.Condition` module performs actions when a given condition (or conditions) becomes true, or repeatedly while the condition (or conditions) remains true.&#x20;It is ideal for responding to dynamic game states, events, or logical conditions.

***

### <mark style="color:$success;">Methods</mark>

<details>

<summary><strong>When(Action callback, Func condition</strong>, int repeats = 0<strong>)</strong></summary>

Executes the `callback` **once** when the specified `condition` becomes `true`.

{% code overflow="wrap" %}

```csharp
Delayed.Condition.When(() => Debug.Log("Low health!"), () => playerHealth < 50);
```

{% endcode %}

</details>

<details>

<summary><strong>When(Action callback</strong>, int repeats = 0<strong>, params Func[] conditions)</strong></summary>

Executes the `callback` **once** when all of the specified `conditions` are `true`.

{% code overflow="wrap" %}

```csharp
Delayed.Condition.When(
    () => Debug.Log("All enemies dead!"),
    () => enemy1.IsDead,
    () => enemy2.IsDead
);
```

{% endcode %}

</details>

<details>

<summary><strong>WhenAny(Action callback</strong>, int repeats = 0<strong>, params Func[] conditions)</strong></summary>

Executes the `callback` **once** when **any** of the specified `conditions` is `true`.

```csharp
Delayed.Condition.WhenAny(
    () => Debug.Log("Any enemy dead!"),
    () => enemy1.IsDead,
    () => enemy2.IsDead
);
```

</details>

<details>

<summary><strong>While(Action callback, Func condition, cancelIfFalse = true)</strong></summary>

Repeatedly executes the `callback` **every frame** while the specified `condition` remains `true`.

```csharp
Delayed.Condition.While(() => Debug.Log("Running!"), () => playerIsRunning);
```

</details>

<details>

<summary><strong>While(Action callback, cancelIfFalse = true, params Func[] conditions)</strong></summary>

Repeatedly executes the `callback` **every frame** while all of the specified `conditions` remain `true`.

```csharp
Delayed.Condition.While(
    () => Debug.Log("All enemies still alive!"),
    () => !enemy1.IsDead,
    () => !enemy2.IsDead
);
```

</details>

<details>

<summary><strong>WhileAny(Action callback, cancelIfFalse = true, params Func[] conditions)</strong></summary>

Repeatedly executes the `callback` **every frame** while **any** of the specified `conditions` remain `true`.

```csharp
Delayed.Condition.WhileAny(
    () => Debug.Log("At least one enemy alive!"),
    () => !enemy1.IsDead,
    () => !enemy2.IsDead
);
```

</details>

***

### <mark style="color:$warning;">Important</mark>

For `While/WhileAny`, by default `cancelIfFalse = false`.\
When the condition (or conditions) become false, the action is **not canceled** and is **not removed** — instead, it simply waits until the condition becomes true again.

You can enable cancellation behavior by setting `cancelIfFalse = true`.\
In this case, when the condition is no longer met, the action will be canceled (removed) and the `OnCancel` event will be invoked.

It is also not recommended to use `Loop` with `While/WhileAny`, as it is unnecessary and may lead to redundant execution.

Use a `Loop(repeats)` to loop `When/WhenAny` for a certain number of times.
