# Math

## Use Cases

* Currency systems
* Resource management
* Health and life counter systems
* Damage systems
* Upgrade systems
* Leveling up and scaling

{% hint style="warning" %}
Set **Broadcast Type** to *All* or *Rules* to send messages from objects to the Rules System.
{% endhint %}

{% hint style="success" %}
Reduce a numerical variable with a negative value.
{% endhint %}

## Math

<figure><img src="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FtfF3MdieuJ4RkYY93E95%2Frules%20math%20and%20broadcast3%20resize.gif?alt=media&#x26;token=ac68f5d6-ec2e-4159-90dc-96108d5c9989" alt=""><figcaption><p>Powerful Math rule works with integer, decimal, and time variables</p></figcaption></figure>

{% tabs %}
{% tab title="About" %}
Perform basic math operations with time and number variables using one versatile rule.

{% hint style="warning" %}
A timer must be paused in order for the Math rule to work correctly with a time variable.
{% endhint %}

<figure><img src="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FEWbU2EUnBm7E2XoSXGgt%2Fimage.png?alt=media&#x26;token=b4fa372e-95fc-4600-a620-39cd2d7ec232" alt="" width="321"><figcaption></figcaption></figure>
{% endtab %}

{% tab title="Details" %}
{% hint style="info" %}
The Math rule may use values and variables for integers, decimals, and time.
{% endhint %}

#### Required Message

Select the incoming message to trigger the rule.

#### Operand 1

Select one of the following:

* **Variable** will allow you to select an existing numerical variable or create a new one.
* **Fixed value** will allow you to enter a constant numerical value.
* **Message Argument** will allow you to use an argument sent within the Required Message that triggered the rule. The message has to be created with an argument of the number type for it to be selectable here. A dropdown menu will appear to select the message argument.

#### Operator

Select the mathematical operator for the calculation (e.g., add, multiply, etc).

#### Operand 2

Select variable, fixed value, or message argument (see Operand 1).

#### Result

Select a number or decimal variable to staore the result of the calculation.

#### Message to Send

Select the message that will be sent after the calculation.
{% endtab %}
{% endtabs %}

## Add/Subtract Number Continuously

{% tabs %}
{% tab title="About" %}
Add or subtract from a numerical variable repeatedly for a set time period. Optional: Set a maximum and minimum value for the variable used.

{% hint style="warning" %}
A timer must be paused in order for the Add/Subtract Number Continuously rule to work correctly with a time variable.
{% endhint %}

<figure><img src="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2F0uhLFopXOmQ4MpL9rH2O%2Fimage.png?alt=media&#x26;token=38c7214b-2d05-48bb-884f-0c51880287b3" alt="" width="325"><figcaption></figcaption></figure>
{% endtab %}

{% tab title="Details" %}

### Add/Subtract number continuously

**Required message**&#x20;

The message to activate this tree. Every time this message is broadcast the tree will be triggered.

**Time variable**&#x20;

This defines the time variable, that is used to measure time in this tree.

**Duration**&#x20;

There are three modes to define the time between each increase/decrease:

* **Variable** will allow you to define an existing variable to modify the timer. This variable can be modified elsewhere using the **Roll a Random Number, Add/Subtract number** or **Set number value** Rules. A dropdown menu will appear to select a variable.
* **Fix value** will allow you to set up a constant value. An input field will appear to enter the number value.
* **Message Argument** will allow you to use an argument sent within the message. The message has to be created with an argument of the number type for it to be selectable here. A dropdown menu will appear to select the message argument.

**Number variable**&#x20;

This defines the number variable that is modified by this rule. This could be a players score or stat like hunger.

**Value change**&#x20;

There are three modes to define how much time will be added to the number:

* **Variable** will allow you to define an existing variable to modify the number. This variable can be modified elsewhere using the **Roll a Random Number, Add/Subtract number** or **Set number value** Rules. A dropdown menu will appear to select a variable.
* **Fix value** will allow you to set up a constant value. An input field will appear to enter the number value.
* **Message Argument** will allow you to use an argument sent within the message. The message has to be created with an argument of the number type for it to be selectable here. A dropdown menu will appear to select the message argument.

**Minimum Value**&#x20;

There are three modes to define the minimal value. The number variable won’t be lower than this value.

* **Variable** will allow you to define an existing variable to modify the number. This variable can be modified elsewhere using the **Roll a Random Number, Add/Subtract number** or **Set number value** Rules. A dropdown menu will appear to select a variable.
* **Fix value** will allow you to set up a constant value. An input field will appear to enter the number value.
* **Message Argument** will allow you to use an argument sent within the message. The message has to be created with an argument of the number type for it to be selectable here. A dropdown menu will appear to select the message argument.

**Maximum Value**&#x20;

There are three modes to define the maximal value. The number variable won’t be higher than this value.

* **Variable** will allow you to define an existing variable to modify the number. This variable can be modified elsewhere using the **Roll a Random Number, Add/Subtract number** or **Set number value** Rules. A dropdown menu will appear to select a variable.
* **Fix value** will allow you to set up a constant value. An input field will appear to enter the number value.
* **Message Argument** will allow you to use an argument sent within the message. The message has to be created with an argument of the number type for it to be selectable here. A dropdown menu will appear to select the message argument.

**Message to stop the rule**&#x20;

This message is stop the rule from running.
{% endtab %}
{% endtabs %}


---

# 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.sandbox.game/en/creator/game-maker/docs/game-rules/math.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.
