# Variables

As a miscellaneous tool, this plugin supports internal variables. These variables can be set inside actions or through a command by admins and then used to check conditions inside tooltips.

Variables have a target, persistence and are defined through key-value pairs. For example, you can have a variable named "`meow`" equal to "`true`" or "`amount`" equal to `5`.

## Usage

Below is a reference table, which shows where and how variables can be used;

*If you want to modify variables, you can do it:*

* Inside tooltips' [actions](https://tooltips.gitbook.io/tooltips/configuration/preset/actions), using [variable commands](https://tooltips.gitbook.io/tooltips/configuration/preset/actions/commands)\
  For example:

```yaml
conditions:
  conditions:
  # When looking at a bedrock block
  - lookingatblock{type=bedrock} true
  actions:
    right-click:
    # And right clicking that bedrock block, set players variable "cool" to "true"
    - setvar %player% cool true
```

* Externally (outside of actions), using the [vars command](https://tooltips.gitbook.io/tooltips/plugin/commands#variable-commands)\
  For example:\
  `/tt vars setvar Septicuss cool true`

*If you want to use variables in conditions (to check something)*

* Use the [compare condition ](https://tooltips.gitbook.io/tooltips/configuration/preset/condition-list#compare)with [variable functions](https://tooltips.gitbook.io/tooltips/configuration/preset/functions#usdvar-global-key-usdpvar-global-key)\
  For example:

```yaml
conditions:
  conditions:
  # Check if player has variable "cool" set to true
  - compare{ first=$var(cool); second=true }
```

*If you want to use variables in tooltips text*

* Use the [variable functions](https://tooltips.gitbook.io/tooltips/configuration/preset/functions#usdvar-global-key-usdpvar-global-key)\
  For example:

```yaml
content:
  text:
  - 'Is the player viewing'
  - 'this tooltip cool?'
  - '$var(cool)'
```

*If you want to display variables outside of Tooltips in other plugins*

* Use [PlaceholderAPI variable placeholders](#placeholderapi)\
  For example:\
  `/papi parse Septicuss %tooltips_v_cool%`

## Terminology

### Persistence

There are persistent and non-persistent variables. Persistence means that variables are saved into files and kept around. Non-persistent variables are reset on server restart, persistent variables can only be cleared through the proper commands.&#x20;

### Scope

Variable commands define a scope, which defines who this variable will be set for.

| Scope           | Explanation                                                                                                                                   |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| Global          | A global variable applied to all players.                                                                                                     |
| Player          | The current player. For example, if this scope is used in actions, the action command will be executed for the player with an active tooltip. |
| \<Player\_Name> | An online players name. Using this will set it only for a specific player.                                                                    |

## Evaluation

Setting the variable supports mathematical expressions, which can utilize PAPI placeholders and local placeholders. Examples:

```
setvar player myvariable (1+1)
setvar global goofballs $var(global, goofballs) + 1
setvar global goofballs ($var(global, goofballs) / $var(myvariable)) * 30 + 25
/tt setpersistentvar Septicuss personal_number 31*5
```

## PlaceholderAPI

Using PlaceholderAPI placeholders has been made as simple as possible. Placeholders are dynamic, and you have to build them using keys:

<table><thead><tr><th width="180">Key</th><th>Description</th></tr></thead><tbody><tr><td>v</td><td>variable (always required)</td></tr><tr><td>g</td><td>global</td></tr><tr><td>p</td><td>persistent</td></tr></tbody></table>

#### Structure of a placeholder

`%tooltips_[KEY]_[ID]%`

\[KEY] -> multiple keys combined in any order

\[ID] -> ID of the variable being requested

#### Examples

*Get the player variable "cool"*

`%tooltips_v_cool%`

*Get the global variable "cool"*

`%tooltips_gv_cool%`

*Get the **persistent** player variable "tutorial\_passed"*

`%tooltips_pv_tutorial_passed%`

*Get the **persistent** global variable "views"*

`%tooltips_pgv_views%`
