Reply
 
Thread Tools Display Modes
Unread 10-08-12, 09:16 PM   #1
Spawnova
A Black Drake
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 87
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.
Spawnova is offline   Reply With Quote
Unread 10-08-12, 10:17 PM   #2
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,234
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
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!

Last edited by Phanx : 10-09-12 at 11:05 PM.
Phanx is offline   Reply With Quote
Unread 10-08-12, 10:56 PM   #3
Spawnova
A Black Drake
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 87
Perfect thank you! =D
Spawnova is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Lua/XML Help » slider variable saving and using

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off