# Functions

## Usage

### Format

All functions follow the following format. Arguments can also be wrapped in quotes.&#x20;

```c
$function(arg1, "arg2")
```

Most functions can also be nested inside eachother, creating more complex behaviour. This is the main advantage of functions over placeholders.

```c
$uppercase($var("pet_name")) // Cat -> CAT
```

New functions can also be added with addons using [plugin API](/tooltips/extra/api.md).

## Default Functions

#### ⭐ `$data(key)`

> Get data from the current preset file. See [how to define data in presets](/tooltips/configuration/preset.md#data).

```yaml
# EXAMPLE DATA
data:
    key: 10
    prices:
        sword: 100
        chair: 50
    lines:
    - 'Hello!'
    - 'How are you?'
```

```c
// Returns the value from data.key
$data(key) // 10

// Returns the value from data.prices.sword
$data(prices.sword) // 100

// Returns the price from data.prices.<furniture_id>
$data(prices.$context(furniture.id)) // 50 if chair

// Returns the 0 indexed element in list "lines"
$data(lines[0]) // Hello

// Returns the length (size) of a list
$data(lines[length] // 2
```

***

#### `$hasdata(key)`

> Same as data, but returns *true* if given path exists and *false* if not.

```c
$hasdata(key) // true

$hasdata(fakekey) // false
```

***

#### ⭐ `$context(key)`

> Get a given key from condition context. In order for context to exist, the preset has to have this condition in its `conditions`. `$context(key)` can use similar keys to the data function. [See which conditions return context](/tooltips/configuration/preset/condition-list.md).\
> \
> (By default, all conditions also return a `condition.<condition>` context, which has the result of the condition (true/false) )

```c
// true if "sneaking" condition is true 
$context(condition.sneaking)

// true if "op" condition is true
$context(condition.op)

// world name if "world" condition is true
$context(world)

// get first element from a context
$context(list[0])
```

***

#### `$hascontext(key)`

> Same as $context(key), but returns *true* if context is set and *false* if not.

```c
// returns true if world context has been set
$hascontext(world)
```

***

⭐ `$if(condition, trueText, falseText)`

> Conditional function, which takes an evaluated statement and depending on the output displays either trueText or falseText.

```c
// returns the text "True!"
$if(true, "True!", "False...")

// returns "You're opped!" if player is opped
$if($context(condition.op), "You're opped!", "You're not opped")
```

***

#### ⭐ `$var(global, key)`   `$pvar(global, key)`

> Variable functions allow to retrieve values from [variables](/tooltips/extra/variables.md) using a key. `$var` is used for local variables and `$pvar` is used for persistent (saved) variables.&#x20;

```c
// returns the value of the "busy" variable for current player, for ex. true
$var("busy")
// same as above, but persistent
$pvar("busy")

// returns the global and persistent "purchases" variable, for ex. 10
$pvar("global", "purchases")
```

***

#### `$parse(player, text)`

> Parses the given text for another player, instead of the player viewing the tooltip.

```c
// returns "Septicuss"
$parse("Septicuss", "%player_name%")

// returns the Vault balance of the target player
$parse($context("lookingatplayer.name", %vault_eco_balance%)
```

***

#### String functions

```c
$lowercase("HEY") // -> "hey"
$uppercase("hey") // -> "HEY"
$capitalize("hey") // -> "Hey"
```

#### ⭐ `$static(text)`

> An important optimization function, which processes and caches the text within. This will stop the given text from being updated and will stay static.

```c
// will display the value of the "busy" variable
$static($var("busy"))
```

***

#### `$strip(text)`

> Strips the given text from all animation tags. Useful when text has to be displayed in another tooltip after an animation was done.

```c
// returns "Hello!"
$strip("<tw t=Hello!>")
```

***

`$preprocess(text)`

> Needed when importing text from elsewhere (for example using a placeholder or a function), which has animations.

```c
// processes the animations of the first line defined in data
$preprocess($data(lines[0]))
```

***

Can think of some other useful function? Suggest it in the Discord!


---

# 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://tooltips.gitbook.io/tooltips/configuration/preset/functions.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.
