> For the complete documentation index, see [llms.txt](https://docs.anvil.team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.anvil.team/user-settings/make-use-of-our-settings-api.md).

# Make use of our settings API

The settings API allows you to link your LUA variables to our settings system, providing an auto-generated user interface to modify any of your variables in real-time while your script is running.

Users can open the LUA Settings Hub to see all of the variables your script exposed and change them.\
They can save their own configuration and load it or delete it to go back to your provided default values. You can optionally call a function to load the user settings on each script start, more on that below.

## Quick Overview

To integrate your script with our settings API, you will have to define a global `settings` table variable. Here is an example of a settings table defining some values, for clarity the full script is provided:

{% hint style="info" %}
It may be obvious to some but the names here are freely choosable.

Of course "someInt" could be named anything you like and the same applies to any other entry in the settings table. Do not assume that you have to use the same naming as in these examples. The only thing it has to adhere to is Lua's naming convention (e.g. no spaces etc)
{% endhint %}

```lua
settings = {
    someInt = 50,
    someFloat = 5.5,
    someOtherFeature = true,
    name = "Bob",
    textColor = Vec4:new(1, 0, 0, 1)
}

function tick()
    draw.text(
        "someInt: " .. tostring(settings.someInt), 
        Vec2:new(200, 200), 
        Vec4:new(255, 0, 0, 255), 
        60, 
        "Verdana"
    )

    draw.text(
        "someFloat: " .. tostring(settings.someFloat), 
        Vec2:new(200, 260), 
        Vec4:new(255, 0, 0, 255), 
        60, 
        "Verdana"
    )

    draw.text(
        settings.name, 
          Vec2:new(200, 320), 
         settings.textColor, 
        60, 
        "Verdana"
     )    
end

engine.addEventListener("render", tick);
```

If you run the above script yourself you should see something similar to:

<figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FgMiza4N2vOVsM7rL4es7%2Fsettings_1.jpg?alt=media&amp;token=56afeccf-8c3e-4faa-9915-421121eaf71e" alt=""><figcaption></figcaption></figure>

## Opening the Settings Hub

In order to open the Lua Settings Hub, you'll have to create bind for it. You'll find all binds in the last tab under the Binds subtab, like so:

<figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FYaZ7DXix49bEimjeQhP8%2Fsettings_2.png?alt=media&amp;token=f1676de4-1571-4951-a725-436fe8883dfc" alt=""><figcaption></figcaption></figure>

If you open the settings hub you'll see all the exported settings of our small example script, easily changable. Your script has to be running for it to show up in the Settings Hub.

<div data-full-width="false"><figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2F0c8jO7TKSAUszORUhFPf%2Fsettings_3.gif?alt=media&amp;token=a0a95b80-3d80-40b6-8b42-93484f1c0b5d" alt=""><figcaption><p>Please click to enlarge for more detail</p></figcaption></figure></div>

## Saving & Loading

Users can change your variables in the hub and save them by clicking on the save symbol.

<figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2Fj1JakbPUc01B7troKX2C%2Fsettings_4.jpg?alt=media&amp;token=b705a0f2-fd7a-4500-ad7f-aa5033bf16d3" alt=""><figcaption></figcaption></figure>

You can load the settings of a user directly upon the start of your script by simply calling `loadSettings()` after you defined the `settings` table, like so:

```lua
settings = {
    someInt = 50,
    someFloat = 5.5,
    someOtherFeature = true,
    name = "Bob",
    textColor = Vec4:new(1, 0, 0, 1)
}

loadSettings()
```

{% hint style="info" %}
Make sure to call loadSettings() after you defined the settings table. If you were to call it before then Anvil would load the user settings and then your definition of the settings table would override the loaded settings again.
{% endhint %}

If the user has any settings saved it will load them into your settings table, otherwise use the default values you provided e.g. someInt would be 50 if no settings were found.

## Advanced Usage

The settings API supports the following types:

* <mark style="color:purple;">bool</mark>&#x20;
* <mark style="color:purple;">int</mark>
* <mark style="color:purple;">float</mark>
* <mark style="color:purple;">string</mark>
* <mark style="color:purple;">Vec4 (color usage)</mark>
* <mark style="color:purple;">callback function</mark>

### Bool

A simple bool will be displayed as a checkbox in the Settings Hub

```lua
settings = {
    someOtherFeature = true
}
```

<div align="left"><figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FjODvUF0p2kxoRBLjBp2A%2Fcheckbox.jpg?alt=media&amp;token=94dd12be-82b4-4954-9e98-667804c2b1a5" alt=""><figcaption></figcaption></figure></div>

The only supported metadata for bools is the `onChange` handler. If you wish to run some code when the user clicks the checkbox, you can specify an `onChange` handler like in the below example:

```lua
function doSomeWork(newBoolState)
    print("State is now: " .. tostring(newBoolState))
end

settings = {
    someOtherFeature = {
        value = true,
        onChange = doSomeWork
    }
}

-- using the bool in your code: settings.someOtherFeature.value
```

<figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FeiTBu14dB94HQ4dnmELk%2Fbool_onchange.gif?alt=media&amp;token=8ae7e72e-47b2-40d6-a078-3b7b2d886530" alt=""><figcaption><p>Please click to enlarge for more detail</p></figcaption></figure>

### Int & Float

Ints and floats will be displayed as a number input respectively.

```lua
settings = {
    someInt = 50,
    someFloat = 5.5
}
```

<div align="left"><figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FyEDrneTW13qC4Ag9b8M6%2Fsettings_int_float_unclamped.jpg?alt=media&amp;token=92fd37b3-13a9-45f1-9e3d-e7abdd342982" alt=""><figcaption></figcaption></figure></div>

The supported metadata for ints and floats are `min` and `max` and the `onChange` handler as seen above in the Bool section. To constrain the user to only inputting numbers in a certain range make use of the `min` and `max` metadata, like so:

```lua
settings = {
    someInt = {
        value = 50,
        min = 0,
        max = 100
    },
    someFloat = {
        value = 5.5,
        min = -180,
        max = 180
    }
}
```

Instead of a direct number input, the numbers will now be linked to sliders with your specified min and max values clamping the input to your desired range.

<figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FEuz6aPYWoVSrLe1BdjGE%2Fint_float_clamped.gif?alt=media&amp;token=fd82b165-2ca1-4a61-9c80-82f8141e552d" alt=""><figcaption><p>Please click to enlarge for more detail</p></figcaption></figure>

### String

Strings will be displayed as a simple text input.&#x20;

```lua
settings = {
    name = "Bob"
}
```

<div align="left"><figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FpvmsKkepRIB630NHATFd%2Fstring_textinput.jpg?alt=media&amp;token=438d9eb2-a847-4394-a12d-5a0b186baa93" alt=""><figcaption></figcaption></figure></div>

The supported metadata for strings is the `onChange` handler as seen in the Bool section. If you want to run code when the user typed in a new character (e.g. changed the string at all) then do it like so and run the handling code in the onChange callback function:

```lua
function handleNameChange(newName)
    print("New name: " .. newName)
    -- do some work etc..
end

settings = {
    name = {
        value = "Bob",
        onChange = handleNameChange
    }
}
```

<figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2Fe2abew5Myr3QV6jXZMh5%2Fstring_onchange.gif?alt=media&amp;token=f7dfd67d-3d3a-4791-8e59-9fd8889b2c04" alt=""><figcaption><p>Please click to enlarge for more detail</p></figcaption></figure>

### Vec4 (color)

Vec4's will be displayed as a color input and are intended to be used for colors.

```
settings = {
    textColor = Vec4:new(1, 0, 0, 1)
}
```

<div align="left"><figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FTr6RPOY20J3r4oxR7hBH%2Fcolor.jpg?alt=media&amp;token=dd6e9b70-4a72-46a3-b1e1-5ddb74268038" alt=""><figcaption></figcaption></figure></div>

If you click on the color square you can fully configure the color:

<div align="left"><figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FxVRHxdHuSMmgPGgZViEu%2Fcolor2.jpg?alt=media&amp;token=53067376-3574-44bd-b8c4-b0b03f154a55" alt=""><figcaption></figcaption></figure></div>

The supported metadata for is the `onChange` handler. Refer to the Bool or Int\&Float section for details. The callback function will get passed the new color value (Vec4 type).

### Function

A function will be displayed as a button. When the user clicks the button in the Settings Hub, your function will be executed.

```lua
function doSomething()
    print("doing some work...")
    -- do some work etc..
end

settings = {
    Activate = doSomething
}
```

<div align="left"><figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2F4PGu6cc9nJ37J68IKMJU%2Fbutton.jpg?alt=media&amp;token=bcb49be9-2783-45a6-914d-1f408be8c6be" alt=""><figcaption></figcaption></figure></div>

<figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FnroNoqSWbOfvX5DE2Q2R%2Fcallback.gif?alt=media&amp;token=73620603-8b8a-40b8-8e22-bb8470c2da1f" alt=""><figcaption><p>Please click to enlarge for more detail</p></figcaption></figure>

### Extra

You can provide a "description" key in the settings table to have a description of what your script is doing. This is a good place to tell the user how your script should be used / what it does.

<figure><img src="https://3505030569-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEyvfcrHLzYzsbCSX7ZDV%2Fuploads%2FPGpOUA4G2H4H63xoGt7u%2Fdesc.png?alt=media&amp;token=69693c3c-965d-497b-bc26-d65ae716a286" alt=""><figcaption></figcaption></figure>
