Hello,
I have a checkbutton that I need to save as a variable. I tried a few different things but it didn’t work. I want the checkbutton state to be saved on logout and loaded at login.
Thank you,
Inthenstus
Hello,
I have a checkbutton that I need to save as a variable. I tried a few different things but it didn’t work. I want the checkbutton state to be saved on logout and loaded at login.
Thank you,
Inthenstus
The only mechanism to save information between sessions is via SavedVariables:
https://wow.gamepedia.com/Saving_variables_between_game_sessions
I am aware of that. I am using that in my addon without the user interface. However, when I set the variable to be saved I get an error and the addon doesn’t load. I tried to create a global variable isChecked = CheckButton:GetChecked() like so. Is saving the state of a checkbutton different then a boolean saved variable?
What error?
When you say “without a user interface”, you will need a frame to check that your SavedVariable table has been loaded and if not , create it.
Saved Variables don’t get loaded until after your addon code is loaded.
local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(self, event, ...)
if event == "PLAYER_LOGIN" then
if not MY_SAVED_VARIABLES_TABLE then
MY_SAVED_VARIABLES_TABLE = {}
end
end
end)
f:RegisterEvent("PLAYER_LOGIN") -- only fires once and after SVs have loaded
After this you can:
X_CheckButton = CreateFrame(...)
X_CheckButton:SetChecked(MY_SAVED_VARIABLES_TABLE.ThisCheckButton)
X_CheckButton:SetScript("OnClick", function(self)
MY_SAVED_VARIABLES_TABLE.ThisCheckButton = self:GetChecked()
end)
CORE
Hyperspawn_Core_Event = CreateFrame(“Frame”);
CORE
function Hyperspawn_Core_OnEvent(self, event, …)
if event == “PLAYER_LOGIN” then
if not Hyperspawn_Options then
Hyperspawn_Options = {}
end
end
end
CORE
Hyperspawn_Core_Event:RegisterEvent(“PLAYER_LOGIN”);
CORE
Hyperspawn_Core_Event:SetScript(“OnEvent”, Hyperspawn_Core_OnEvent)
UI
V_AN_CB = CreateFrame(“CheckButton”, nil, Hyperspawn_UI.childpanel_Announcements, “UICheckButtonTemplate”)
V_AN_CB:SetChecked(Hyperspawn_Options.V_AN_CB)
V_AN_CB:SetPoint(“TOPLEFT”, 10, -160)
V_AN_CB.text:SetText(“Vendor”)
V_AN_CB:SetScript(“OnClick”, function(self)
Hyperspawn_Options.V_AN_CB = V_AN_CB:GetChecked()
end)
Hello, thank you for your help. I was able to implement the code but the setting isn’t be saved. Any idea what could be going on here? I am separating the addon out into multiple .LUA files. The first block of code is in the CORE file and the second block is in the UI file.
EDIT: I am doing the prerequisites in this order. 1. Create Frame, 2. Function, 3. Register Events, and 4. Set Scripts in the CORE file.
EDIT: The error message that I am getting is null. The function on the core page was originally local, but I removed that thinking that could of been the issue. In the .TOC file I am loading the CORE file before the UI folder.
What do you mean by “isn’t being saved”
After checking the box have you logged out and had a look in your SavedVariables file?
If you are creating the checkbox at the time your addon loads, you will need to initialise if after your saved variables have loaded.
Probably the easiest is to move the frame creations code (or UI element creation code) into a function that is called after PLAYER_LOGIN. Either that or use a function that initialises the UI widgets after the event.
I was able to figure it out. I had to use two OnEvents (one in CORE and the other in UI). Below is the code. Is there any problems with using multiple OnEvents in different .LUA files?
UI
– Create Frame
Hyperspawn_UI_Event = CreateFrame(“Frame”);
– Functions
function Hyperspawn_UI_OnEvent(self, event, …)
if event == “PLAYER_LOGIN” then
V_AN_CB:SetChecked(Hyperspawn_Options.V_AN_CB)
V_AN_CB:SetScript(“OnClick”, function(self)
Hyperspawn_Options.V_AN_CB = V_AN_CB:GetChecked()
end)
end
end– Register Events
Hyperspawn_UI_Event:RegisterEvent(“PLAYER_LOGIN”);
– Set Scripts
Hyperspawn_UI_Event:SetScript(“OnEvent”, Hyperspawn_UI_OnEvent)
CORE
– Create Frames
Hyperspawn_Core_Event = CreateFrame(“Frame”);
– Functions
function Hyperspawn_Core_OnEvent(self, event, …)
if event == “PLAYER_LOGIN” then
if not Hyperspawn_Options then
Hyperspawn_Options = {}
end
end
end– Register Events
Hyperspawn_Core_Event:RegisterEvent(“PLAYER_LOGIN”);
– Set Scripts
Hyperspawn_Core_Event:SetScript(“OnEvent”, Hyperspawn_Core_OnEvent)
I can’t see your code so I have no idea how it is organised.
local function Hyperspawn_Core_OnEvent(self, event, ...)
if event == "PLAYER_LOGIN" then
if not Hyperspawn_Options then
Hyperspawn_Options = {}
end
InitialiseMyWidgets()
-- or CreateMyWidgetFrame()
-- or ...
end
end
Thank you for all of your help 
You shouldn’t need to create two event frames in different lua modules just in order to use two functions to do “stuff”.
You can create functions (and data) in different lua modules within the same addon that can be shared without the use of globals.
Every .lua file is “passed” a table that is exclusive to that addon as well as a string containing the addon name
At the beginning of your file you can code something like:
addonName, addonTable = ...
addonName and addonTable can be any names you like and can be different in each lua file (although why you would I don’t know). Commonly people call the table an addon’s NameSpace.
file1.lua
local addonName, NS = ...
function NS.SayHello()
print("Hello!")
end
File2.lua
local addonName, NS = ...
NS:SayHello()
-- my comment
https://www.wowinterface.com/forums/forumdisplay.php?f=16