Skip to content

Examples

Sebastian Lindfors edited this page Feb 20, 2024 · 4 revisions

Saved variable table

local Lib = LibStub('LibSettings');
local myObject;

Lib:LoadAddOnCategory('MyAddOn', function()
    -- Initialize the saved variables, or load from already existing
    MyAddOn_SavedVars = MyAddOn_SavedVars or {
        checkBox = false;
        slider   = 190;
        dropDown = 2;
    };

    -- Return the props tree using the table as storage
    return {
        name  = 'My AddOn';
        id    = 'myAddOn';
        table = MyAddOn_SavedVars;
        {
            {
                name    = 'My Check Box';
                id      = 'checkBox';
                tooltip = 'A simple checkbox to control a boolean value.';
                default = true;
            };
            {
                name    = 'My Slider';
                id      = 'slider';
                tooltip = 'A slider to adjust a numeric value.';
                default = 180;
                min     = 90;
                max     = 360;
                step    = 10;
            };
            {
                name    = 'My Drop Down';
                id      = 'dropDown';
                tooltip = 'A dropdown menu to select an option.';
                default = 1;
                options = {
                    { 1, 'Option 1' };
                    { 2, 'Option 2' };
                    { 3, 'Option 3', 'This option has some clarification.' };
                };
            };
        };
    };
end, function(result)
    -- Store the result somewhere
    myObject = result;
    -- Get the value of a setting
    print("My slider's current value is:", myObject.slider.getValue()); -- 190
    -- Set a new value using the settings object
    myObject.slider.setValue(200);
    -- Read the new value from the table storage
    print("My slider's current value is:", MyAddOn_SavedVars.slider); -- 200 
end)

Nested variable table with subcategory

local Lib = LibStub('LibSettings');
local myObject;

Lib:LoadAddOnCategory('MyAddOn', function()
    -- Initialize the saved variables, or load from already existing
    MyAddOn_SavedVars = MyAddOn_SavedVars or {
        checkBox = false;
        subCategory = {
            slider   = 190;
            dropDown = 2;
        };
    };

    -- Return the props tree using the table as storage
    return {
        name  = 'My AddOn';
        id    = 'myAddOn';
        table = MyAddOn_SavedVars;
        {
            {
                name    = 'My Check Box';
                id      = 'checkBox';
                tooltip = 'A simple checkbox to control a boolean value.';
                default = true;
            };
            {
                name    = 'My Subcategory';
                id      = 'subCategory';
                table   = MyAddOn_SavedVars.subCategory;
                {
                    {
                        name    = 'My Slider';
                        id      = 'slider';
                        tooltip = 'A slider to adjust a numeric value.';
                        default = 180;
                        min     = 90;
                        max     = 360;
                        step    = 10;
                    };
                    {
                        name    = 'My Drop Down';
                        id      = 'dropDown';
                        tooltip = 'A dropdown menu to select an option.';
                        default = 1;
                        options = {
                            { 1, 'Option 1' };
                            { 2, 'Option 2' };
                            { 3, 'Option 3', 'This option has some clarification.' };
                        };
                    };
                };
            };
        };
    };
end, function(result)
    -- Store the result somewhere
    myObject = result;
    -- Get the value of a setting
    print("My slider's current value is:", myObject.subCategory.slider.getValue()); -- 190
    -- Set a new value using the settings object
    myObject.subCategory.slider.setValue(200);
    -- Read the new value from the table storage
    print("My slider's current value is:", MyAddOn_SavedVars.subCategory.slider); -- 200 
end)

Category with subcategory

Lib({
    name  = 'My AddOn';
    id    = 'myAddOn';
    table = MyAddOn_SavedVars;
    {
        {
            name    = 'My Check Box';
            id      = 'checkBox';
            tooltip = 'A simple checkbox to control a boolean value.';
            default = true;
        };
        {
            name    = 'My Subcategory';
            id      = 'mySubCat';
            {
                {
                    name    = 'Meow';
                    id      = 'subliminalCatCheckBox';
                    tooltip = 'A simple checkbox to control a boolean value.';
                    default = true;
                };
            };
        };
    };
})

Clone this wiki locally