---
description: Learn more about methods of a pui element
---

# Methods

## `:depend(...)`

Sometimes you want to hide certain elements to not mess up the menu. It may look simple but actually it's rather difficult and complicated. But _**pui**_ can make this as easy as pie.

`:depend(...)` is a method of a pui element, which creates a dependence on elements passed as arguments.

Its arguments can be either elements themselves (generally switches), or tables, which contain `ref, condition, invert`.

In most of the cases, it looks like this: `:depend( {ref, condition}, {ref2, condition2}, ... )`

Also, if you want to "disable" elements (lock them), you can do it with `:depend(true, ...)`

It's worth to note that `pui::group` supports `:depend()` as well.

#### Study these examples:

{% tabs %}
{% tab title="General" %}
This is a common example of the use of `:depend(...)`. As you can see, there are two labels showing statuses of a switch.&#x20;

<pre class="language-lua"><code class="lang-lua">local switch = group:switch("Master switch")
local status1 = group:label("...is enabled!")
local status2 = group:label("...is disabled!")

<strong>status1:depend( {switch, true} )  -- or simply :depend(switch)
</strong><strong>status2:depend( {switch, false} )
</strong></code></pre>
{% endtab %}

{% tab title="Lists" %}
If you want to show your elements when a certain value is selected in a combobox, this can be done as well.

<pre class="language-lua" data-overflow="wrap"><code class="lang-lua">local combo = group:combo("Author of Pride and Prejudice:", {"Leo Tolstoy", "Charles Dickens", "Jane Austen"})

local correct = group:label("Yes! Jane Austen is")
local wrong = group:label("No! Try again")

<strong>correct:depend( {combo, "Jane Austen"} )
</strong><strong>wrong:depend( {combo, "Jane Austen", true} )
</strong></code></pre>

`true` in the third argument invert the condition. Basically, it will look like this:

`not "Jane Austen"`
{% endtab %}

{% tab title="Sliders" %}
You can define ranges for sliders like this:

```lua
local jitter = group:slider("Jitter degree", 0, 100)
local good = group:depend("Good choice")

good:depend( {jitter, 40, 60} ) -- range from 40 to 60
```
{% endtab %}

{% tab title="Functions" %}
Sometimes the condition may be too complicated. In this case, you can use a function:

```lua
local slider = group:slider("Body temperature", 16, 42, 36)

local normal = group:label("You are healthy")
local cold = group:label("You are rather cold!")
local hot = group:label("You are quite hot!")

normal:depend( {slider, function()
    return slider.value >= 35 and slider.value <= 37
end} )
cold:depend( {slider, function()
    return slider.value < 34
end} )
hot:depend( {slider, function()
    return slider.value > 37
end} )
```
{% endtab %}

{% tab title="Multiple conditions" %}
Yes, you can add more conditions calling `:depend(..)` multiple times. But you don't have to.

Study this example of a password puzzle:

```lua
local a = group:combo("A", {"1", "2", "3", "4"} )
local b = group:combo("B", {"1", "2", "3", "4"} )
local c = group:combo("C", {"1", "2", "3", "4"} )

local answer = group:label("yes! 241 is correct")

answer:depend( {a, "2"}, {b, "4"}, {c, "1"} )
```
{% endtab %}
{% endtabs %}

There are a lot of ways to use `:depend()` that I can't even imagine.



## `:set_event(event, func, condition*)`

This methods is used to call functions in events when a certain condition is met. You can unset this behavior via `:unset_event(event, func)`. You can't unset _anonymous_ functions.

{% tabs %}
{% tab title="Example" %}
```lua
local enabled = pui.switch("Main", "Enable", "Name")

local antiaim = function (cmd)
    print(cmd.choked_commands)
end

enabled:set_event("createmove", antiaim)
```
{% endtab %}

{% tab title="Condition as a value" %}
```lua
local selector = pui.combo("Main", "Event", {"off", "render", "createmove"})

local render = function (cmd)
    print "render"
end
local createmove = function (cmd)
    print "createmove"
end

selector:set_event("render", paint_ui, "paint_ui")
selector:set_event("createmove", setup_command, "createmove")
```
{% endtab %}

{% tab title="Condition as a function" %}
{% code fullWidth="false" %}
```lua
local multiselect = pui.selectable("Main", "Events", {"render", "createmove"})

local render = function ()
    print "render"
end
local createmove = function (cmd)
    print "createmove"
end

multiselect:set_event("render", render, function (this)
    return this:get("render")
end)
multiselect:set_event("createmove", createmove, function (this)
    return this:get("createmove")
end)
```
{% endcode %}
{% endtab %}

{% tab title="Unsetting" %}
```lua
local enabled = pui.switch("Main", "Enabled")

local func = function (cmd)
    print(cmd.choked_commands)
end

enabled:set_event("setup_command", func)

enabled:set_callback(function (this)
    if not this.value then
        -- will unset the event after you disable the checkbox for the second time
        enabled:unset_event("setup_command", func)
    end
end)
```

```lua
local enabled = pui.checkbox("CONFIG", "Presets", "Enabled")

enabled:set_event("setup_command", function (cmd)
    print(cmd.choked_commands)
end)
-- this won't work because the function is anonymous
enabled:unset_event("setup_command", function (cmd)
    print(cmd.choked_commands)
end)
```
{% endtab %}
{% endtabs %}
