WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   slider variable saving and using (https://www.wowinterface.com/forums/showthread.php?t=44691)

Spawnova 10-08-12 09:16 PM

slider variable saving and using
 
Lua Code:
  1. MyAddon = {};
  2.  MyAddon.panel = CreateFrame( "Frame", "MyAddonPanel", UIParent );
  3.  MyAddon.panel.name = "Smp";
  4.  InterfaceOptions_AddCategory(MyAddon.panel);
  5.  
  6.     --SLIDER
  7.     SlashCmdList["SMP"] = function() InterfaceOptionsFrame_OpenToCategory(MyAddon.panel) end
  8.     SLASH_SMP1 = "/smp"
  9.     local slider = CreateFrame("Slider","MyAddonSlider",MyAddon.panel,'OptionsSliderTemplate')
  10.     slider:SetPoint("CENTER",0,0)
  11.     getglobal(slider:GetName() .. 'Low'):SetText('25%');
  12.     getglobal(slider:GetName() .. 'High'):SetText('95%');
  13.     getglobal(slider:GetName() .. 'Text'):SetText('Health');
  14.     slider:SetMinMaxValues(25, 95)
  15.     slider:SetValue(70)
  16.     slider:SetValueStep(5)

how would i save the slider value after changing it (so its that value everytime i run wow)
and how would i find the value of the slider such as "slider.Value" etc.

Phanx 10-08-12 10:17 PM

1. The getglobal function was deprecated years ago and should never be used by anyone for any reason. Use _G[value] instead of getglobal(value).

2. You use the GetValue method on your slider to get the current value:
Code:

local value = slider:GetValue()
3. To save anything between sessions, you first need to define a saved variable for your addon. Add something like this to your TOC file:
Code:

## SavedVariables: MyAddonSetting
4. Then, you need to initialize the variable with a reasonable default setting:
Code:

MyAddon.panel:RegisterEvent("PLAYER_LOGIN")
MyAddon.panel:SetScript("OnEvent", function(self)
        MyAddonSetting = MyAddonSetting or 65 -- Default to 65
end)

5. If you are only saving one setting, you can just use the variable directly:
Code:

## SavedVariables: MyAddonSliderValue
Code:

slider:SetScript("OnValueChanged", function(self)
        local value = self:GetValue()
        -- Save the value to your saved variable:
        MyAddonSetting = value
        -- Do whatever you want with the value here.
end)

6. If you want to save more than one setting, you should use a table:
Code:

## SavedVariables: MyAddonSettings
Code:

MyAddon.panel:RegisterEvent("PLAYER_LOGIN")
MyAddon.panel:SetScript("OnEvent", function(self)
        MyAddonSettings = MyAddonSetting or {
                slidervalue = 65,
                othervalue = 12,
                somevalue = "some string",
        }
end)

slider:SetScript("OnValueChanged", function(self)
        -- Get the slider's current value:
        local value = self:GetValue()

        -- Save the value to your saved variable:
        MyAddonSettings.slidervalue = value

        -- Do whatever you want with the value here.
end)

7. You should also add a refresh function to your options panel to set the slider's value based on the saved setting:
Code:

MyAddon.panel.refresh = function()
        slider:SetValue(MyAddonSettings.slidervalue)
end


Spawnova 10-08-12 10:56 PM

Perfect thank you! =D


All times are GMT -6. The time now is 03:31 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI