Thread Tools Display Modes
08-16-10, 04:55 PM   #1
Benholeo
A Murloc Raider
Join Date: Aug 2010
Posts: 4
how can i get addon to save a variable?

new to coding addons and have spent hours trying to figure out how to get variables to save. Now I feel like I have been swimming in snot and its time to actually ask for help. I am wondering whats wrong with the logic in this save, I think its myMod_Save and how it is used.

myMod.toc file
## Interface: 30300
## Title: myMod
## SavedVariablesPerCharacter: myMod_Save
## Dependencies:
myMod.lua
myMod.xml

myMod.lua file
myMod_Save = {
variable = 0
}

function myModFunction()
variable = variable + 1
end

myMod.xml file
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
..\..\FrameXML\UI.xsd">
<Frame name="myModFrame">
<Scripts>
<OnLoad>
myModFunction();
</OnLoad>
</Scripts>
</Frame>
</Ui>
  Reply With Quote
08-16-10, 07:15 PM   #2
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
You're overwriting your SavedVariables every time your AddOn loads. You are also attempting arithmetic on an uninitialized global variable named "variable".

This is what you need to do:


Code:
local defaults = {
        variable = 0
}

myMod_Save = myMod_Save or defaults

function myModFunction() 
        myMod_Save.variable = myMod_Save.variable + 1
end
myMod_Save is declared in the global environment via your ToC file. The first time your AddOn is loaded, myMod_Save will be nil; you have to define it as a table (Generally, this is what you want. It could also simply be a string, number, etc). Every time after that, you want to use what you've already defined so what the code above does is say "Make the value of myMod_Save by the previous value, or make it defaults if this is the first time we've been run."


The table member "variable" needs to be accessed from that table - Lua isn't psychic, so it doesn't know unless you tell it.

I highly suggest that you read Programming in Lua so that you have some basic understanding of what you are dealing with.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
08-16-10, 07:59 PM   #3
Benholeo
A Murloc Raider
Join Date: Aug 2010
Posts: 4
Thank you

also I am not surprised that there were errors in other places since the mod was slapped together to illustrate how i was trying to save in my actual beta.

Edit: its now making a nil saved variable gonna have to keep working at it.
Thanks again, slowly im actually getting something done!

Last edited by Benholeo : 08-16-10 at 09:08 PM.
  Reply With Quote
08-17-10, 04:40 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Torhal View Post
You're overwriting your SavedVariables every time your AddOn loads.
He's actually not, since saved variables aren't loaded yet when the main chunk executes.

Basically, all of your addon's files are read in the order they are listed in your TOC, and then your addon's saved variable(s) are read, and then the ADDON_LOADED event fires to say that your addon is done loading.

Code:
MySavedVar = {
    foo = bar,
    x = 2,
}

local MyAddonFrame = CreateFrame("Frame")
MyAddonFrame:RegisterEvent("ADDON_LOADED")
MyAddonFrame:SetScript("OnEvent", function(self, event, addon)
    if addon == "MyAddon" then
        print(MySavedVar.foo)
    end
end)
The first time this addon loaded, it would print "bar" to your chat frame. If you then typed:

Code:
/run MySavedVar.foo = "kittens!"
...then the next time the addon loaded, it would print "kittens!" instead.
  Reply With Quote
08-18-10, 03:16 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
I don't think VARIABLES_LOADED fires before ADDON_LOADED. ADDON_LOADED fires when all of the addon's core lua and xml files have been read and parsed. It has nothing to do with saved variables. Only after ALL of the initial addons have been loaded, it then loads the variable files located in your account/character settings folder. After all those, it finally fires VARIABLES_LOADED.

The point of this statement, when dealing with previously saved variables, you'd want to hook the VARIABLES_LOADED event and never use ADDON_LOADED for that kind of thing. It's a completely unrelated event and you'll run into glitches using it.

As an added note, VARIABLES_LOADED fires multiple times; every time a CVar changes or a load-on-demand addon has its variables loaded. You should take steps to make sure it only runs on the first time your addon sees the event fire.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
08-18-10, 07:13 PM   #6
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by SDPhantom View Post
I don't think VARIABLES_LOADED fires before ADDON_LOADED. ADDON_LOADED fires when all of the addon's core lua and xml files have been read and parsed. It has nothing to do with saved variables. Only after ALL of the initial addons have been loaded, it then loads the variable files located in your account/character settings folder. After all those, it finally fires VARIABLES_LOADED.

The point of this statement, when dealing with previously saved variables, you'd want to hook the VARIABLES_LOADED event and never use ADDON_LOADED for that kind of thing. It's a completely unrelated event and you'll run into glitches using it.
This is completely wrong.

What Phanx posted is correct.
  Reply With Quote
08-18-10, 09:57 PM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
http://www.wowwiki.com/AddOn_loading_process
The VARIABLES_LOADED event was specifically made for this, but it looks like Blizzard screwed things up once again.

Writing an addon using the old behavior that isn't LoD should still work, the event still fires during initial load.
I haven't done any tests on variable availability since 3.0 when this change apparently happened.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 08-18-10 at 10:11 PM.
  Reply With Quote
08-18-10, 10:08 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
If you have these addons:
  • Bartender
  • GatherMate
  • Mapster
Then the UI loading process looks like this:
  1. Blizzard's FrameXML code loads
  2. Bartender's TOC file is read
  3. Bartender's XML and Lua files are read in the order they are listed in its TOC file
  4. Bartender's SavedVariables file(s) are read
  5. ADDON_LOADED fires with "Bartender" as arg1
  6. GatherMate's TOC file is read
  7. GatherMate's XML and Lua files are read in the order they are listed in its TOC file
  8. GatherMate's SavedVariables file(s) are read
  9. Mapster's TOC file is read
  10. Mapster's XML and Lua files are read in the order they are listed in its TOC file
  11. Mapster's SavedVariables file(s) are read
  12. ADDON_LOADED fires with "GatherMate" as arg1
  13. VARIABLES_LOADED may fire one or more times, with no arguments
  14. PLAYER_LOGIN fires, with no arguments
  15. PLAYER_ENTERING_WORLD fires, with no arguments
  16. VARIABLES_LOADED may fire one or more times, with no arguments
  17. PLAYER_ALIVE fires, with no arguments

Prior to WotLK, VARIABLES_LOADED fired once to signal that all non-LoD addons had been loaded. However, this is no longer the case, and addons wishing to know when all of their files and saved variables have been loaded should listen for the ADDON_LOADED event; addons wishing to know when all non-LoD addons and their saved variables have been loaded should listen for the PLAYER_LOGIN event. Addons wishing to know when an LoD addon and its saved variables have been loaded should listen for ADDON_LOADED. For all practical purposes, VARIABLES_LOADED is useless to addons.

Last edited by Phanx : 08-18-10 at 10:11 PM.
  Reply With Quote
08-19-10, 01:50 PM   #9
Benholeo
A Murloc Raider
Join Date: Aug 2010
Posts: 4
Figured I would end this thread

The original problem was that saved variable where not being created in the WTF folder to begin with.

-The solution to that was not even declaring MyMod_Save in the lua.
-In otherwords Torhal's example worked

My second post stated that I wasn't acessing the data when I reloaded the addon.

-My solution was to use <OnUpdate> and check weither or not MyMod_Save was nil or a variable called default. if it was nil or default = true then MyMod_Save got set to default and if not MyMod_Save = MyMod_Save.
the default variable is so function can be reused elsewhere as a toDefault.

-Phanx had another solution where he listened specificly for Addon_Loaded event. And then I could have called my function. Which would lead to the same end result.

That's 2 solutions hope that helps someone after me.
  Reply With Quote
08-19-10, 08:33 PM   #10
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Benholeo View Post
-My solution was to use <OnUpdate> and check weither or not MyMod_Save was nil or a variable called default. if it was nil or default = true then MyMod_Save got set to default and if not MyMod_Save = MyMod_Save.
the default variable is so function can be reused elsewhere as a toDefault.
This is, to be blunt, a horrible solution. You should only use polling (eg. an OnUpdate script) when it's absolutely necessary. You should definitely never use it when there is an event that fires exactly when the information you want becomes available.
  Reply With Quote
08-20-10, 04:00 AM   #11
Mischback
A Cobalt Mageweaver
 
Mischback's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 221
Originally Posted by Phanx View Post
This is, to be blunt, a horrible solution.
You can't stress this point enough!

In programmer's words, 'OnUpdate' checking for things is 'expensive', which means that you're doing something on EVERY frame update (or in given interval, depending on how you implement it), but chances are high, that it is most of the time not necessary.

As Phanx said: If there's an event, which provide you the information you want, just use that!
__________________
  Reply With Quote
08-20-10, 08:58 AM   #12
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
To illustrate how often your OnUpdate code would be run... As mentioned above, it runs for every frame update/draw/refresh.

If you have 60fps (frames per second), then your code would be run 60 times every second.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
08-25-10, 01:51 PM   #13
Benholeo
A Murloc Raider
Join Date: Aug 2010
Posts: 4
Officially Pwnd

I come back and my invented solution gets owned, I figured it fired every time the addon has an update and not the game. Switching to addonloaded event. Please dont kill me.
__________________
One day I will be able to look back at these post and ask why? The answer was so trivial.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » how can i get addon to save a variable?


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