vide/docs/tut/crash-course/10-actions.md
Aaron Smith 68ff117be9
2023-09-12 17:55:54 +01:00

973 B

Actions

Actions in Vide are special callbacks that you can pass along with properties, which will be called when those properties are being processed with the instance being assigned to, allowing you to run custom code.

local action = vide.action
create "TextLabel" {
    Text = "test",

    action(function(instance)
        print(instance.Text)
    end)
}

-- will print "test"

Actions can be wrapped with functions to re-use specific behaviors. Below is an example of an action used to listen for property changes:

local function changed(property: string, callback: (new) -> ())
    return action(function(instance)
        instance:GetPropertyChangedSignal(property):Connect(function()
            callback(instance[property])
        end)
    end)
end

local output = source ""

create "TextBox" {
    changed("Text", output)
}

The source output will be updated with the new property value any time it is changed externally.