---
description: Section 4.C.2
---

# Advanced: Variable Utilities

This subsection will introduce the midbattle variable and how it can be utilized within a midbattle script. Examples are provided to give a clearer picture in how this can be applied.

***

<mark style="background-color:orange;">**Manipulating the Variable's Value**</mark>

By default, this variable is set to zero at the start of every battle. However, you can manipulate this variable's default value by manually setting it to the specific value you desire. To do this, we would use the <mark style="background-color:blue;">"setVariable"</mark> Command Key.

```
"RoundEnd_foe_every_4" => {
  "setVariable" => 0
}
```

In this example, the value of the variable is reset to zero at the end of every 4th turn. Something like this can be utilized if you want to have some sort of timer in battle that resets every four turns. This is a handy want of have some way to hard reset this variable once it has reached a value threshold that you needed it to.

If you want this variable to start counting in response to something, you may do so by utilizing the <mark style="background-color:blue;">"addVariable"</mark> key. For example, let's say you want the variable to be increased by 1 at the start of each round in a battle.

```
"RoundStartCommand_foe_repeat" => {
  "addVariable" => 1
}
```

You could use something like this to accomplish that. Now each turn, the variable's value will be increased by 1. If you want to _reduce_ the variable's value instead, you would still use the <mark style="background-color:blue;">"addVariable"</mark> key, except you would set this to a negative number instead. For example, if set to -1, then the variable would be reduced by 1 each turn.&#x20;

If you want to randomize how much this variable should be changed by, you can instead set it to an array with as many different values as you want, and one of these values will randomly be selected to add to the variable's total value. For example:

```
"RoundStartCommand_foe_repeat" => {
  "addVariable" => [1, 4, -2, 0]
}
```

This would add to the value at the start of each turn, but the amount would be randomly selected amongst the values in the array.

Finally, if you want to increase the variable's value by a certain factor rather than directly adding an amount, you can accomplish this with the <mark style="background-color:blue;">"multVariable"</mark> Command Key.

```
"RoundStartCommand_foe_repeat" => {
  "multVariable" => 2
}
```

In this example, the value of the variable will now double at the start of each round. If you would like to reduce the variable by a certain factor, you would still use the <mark style="background-color:blue;">"multVariable"</mark> key, except you would set this to a float number instead. For example, if set to 0.5, then the variable would be reduced by half each turn.

{% hint style="info" %}
Note: The value of the midbattle variable may never be less than zero. If you use any of the above methods to set or reduce the variable's value to a number less than zero, the variable will just default to zero. Keep this in mind when designing scenarios where the variable's value may decrease for some reason.
{% endhint %}

By utilizing <mark style="background-color:blue;">"setVariable"</mark>, <mark style="background-color:blue;">"addVariable"</mark> and <mark style="background-color:blue;">"multVariable"</mark>, you have full control in manipulating the value of the midbattle variable. Now let's move on to how we can make something actually _happen_ in response to this variable's value.

***

<mark style="background-color:orange;">**Responding to the Variable's Value**</mark>

Any time you utilize the <mark style="background-color:blue;">"addVariable"</mark> or <mark style="background-color:blue;">"multVariable"</mark> Command Keys described in the above section of this page, a series of Trigger Keys are checked for. These are the keys you want to use in order to make something happen in response to the value of the variable changing. Note that <mark style="background-color:blue;">"setVariable"</mark> does not trigger these keys.

***

<mark style="background-color:yellow;">**When the Variable Reaches a Specific Value**</mark>

Whenever the variable is changed, the <mark style="background-color:purple;">"Variable\_#"</mark> Trigger Key is checked for, where <mark style="background-color:purple;">#</mark> corresponds to the specific value of the variable. For example, if you set the Trigger Key <mark style="background-color:purple;">"Variable\_2"</mark>, this will trigger whenever the value of the variable equals exactly 2. Here's an example:

<figure><img src="https://2153694798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvzqYxbzoSOVtGkOgfRUQ%2Fuploads%2FpL3A79ovfnl2SeGmj3jo%2Fdemo28.gif?alt=media&#x26;token=67c7b7e9-7327-4463-a0ca-474170745ddd" alt="" width="381"><figcaption></figcaption></figure>

```
"TargetDodgedMove_foe_repeat" => {
  "addVariable" => 1
},
"Variable_2" => {
  "speech" => "Man, are you battling blind-folded?"
}
```

In this example, each time the player's Pokemon misses the opponent with a move, the variable's value is increased by 1 thanks to the <mark style="background-color:blue;">"addVariable"</mark> Command Key. Once this value reaches 2, this will trigger a speech event thanks to the <mark style="background-color:purple;">"Variable\_2"</mark> Trigger Key where the opponent mocks you about your inaccuracy.

You can include as many <mark style="background-color:purple;">"Variable\_#"</mark> keys as you want in order to check for as many different variable values as you want. For example, we can expand upon the previous example so that the opponent will now say something each time the player misses, up to 3 times.

<figure><img src="https://2153694798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvzqYxbzoSOVtGkOgfRUQ%2Fuploads%2FemDLugsJfSR5wsPFqc7Y%2Fdemo29.gif?alt=media&#x26;token=71b0c0d4-cf3a-4dec-b590-f9fc224d9bae" alt="" width="381"><figcaption></figcaption></figure>

```
"TargetDodgedMove_foe_repeat" => {
  "addVariable" => 1
},
"Variable_1" => {
  "speech" => "That's strike one."
},
"Variable_2" => {
  "speech" => "That's strike two!"
},
"Variable_3" => {
  "speech" => "Three strikes! You're outta here!"
}
```

***

<mark style="background-color:yellow;">**When the Variable Changes Value**</mark>

If you want to respond to the variable changing value, but don't want to specify a particular value to check for, you may do so with the <mark style="background-color:purple;">"VariableUp"</mark> and/or <mark style="background-color:purple;">"VariableDown"</mark> Command Keys. Unlike the normal <mark style="background-color:purple;">"Variable\_#"</mark> key, these don't require any specific number value entered. Instead, these keys simply check the value of the variable to see if it has been changed by any amount since the last time it was changed. If so, the respective key will trigger depending on whether this amount has increased or decreased.

<figure><img src="https://2153694798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvzqYxbzoSOVtGkOgfRUQ%2Fuploads%2FqzpCUA595cQHdO0HQn9t%2Fdemo31.gif?alt=media&#x26;token=072c458e-6413-4fc6-93cc-be5d5ef5b9da" alt="" width="381"><figcaption></figcaption></figure>

```
"TurnEnd_player_repeat" => {
  "speech"      => [:Opposing, "Let's flip a coin!",
                    "Heads, you win!\nTails, you lose!"],
  "text"        => [:Opposing, "{2} tossed a coin in the air!"],
  "setVariable" => 1,
  "addVariable" => [-1, 1]
},
"VariableUp_repeat" => {
  "speech"       => [:Opposing, "Heads! Lucky you!"],
  "battlerStats" => [:Random, 1]
},
"VariableDown_repeat" => {
  "speech"       => [:Opposing, "Tails! Too bad!"],
  "battlerStats" => [:Random, -1]
}
```

In this example, the opponent plays a game with the player at the end of each of the player's Pokemon's turns. During each instance, the opponent will "flip a coin." If the player wins the coin flip, their Pokemon has a random stat raised by one stage. If the player loses the coin flip, then their Pokemon has a random stat lowered by one stage. This is accomplished by using <mark style="background-color:blue;">"setVariable"</mark> to reset the variable to 1 each turn. Then, <mark style="background-color:blue;">"addVariable"</mark> is used to randomly add 1 or subtract 1 from this value, simulating a coin flip. Finally, the <mark style="background-color:purple;">"VariableUp"</mark> or <mark style="background-color:purple;">"VariableDown"</mark> Trigger Key will trigger depending on this outcome.

If you want something to happen when the variable's value is changed, but only if its new value is above or below a certain value, then you may do so by using the <mark style="background-color:purple;">"VariableOver\_#"</mark> and <mark style="background-color:purple;">"VariableUnder\_#"</mark> Trigger Keys, respectively. The <mark style="background-color:purple;">#</mark> in these keys represent the number which the variable's value should be compared to.

<figure><img src="https://2153694798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvzqYxbzoSOVtGkOgfRUQ%2Fuploads%2FUnhXLNXOC3XL5peS65Ns%2Fdemo30.gif?alt=media&#x26;token=d55f4265-c0d7-45ac-8fc6-45dc92c0e90c" alt="" width="381"><figcaption></figcaption></figure>

```
"AfterItemUse_player_repeat" => {
  "addVariable" => 1
},
"VariableOver_2" => {
  "speech" => [1, "Ugh...stop spamming items!"]
}
```

In this example, the variable's value is increased by 1 each time the player uses an item from their bag. Once this variable reaches a value which is greater than 2, a speech event will trigger in which the opponent will comment on the player's repeated item use.

The same idea works in reverse when using <mark style="background-color:purple;">"VariableUnder\_#"</mark>, which will trigger only when the variable's value has been reduced, but only if this value is less than the entered number.
