Thread Tools Display Modes
12-03-08, 11:47 AM   #1
SetiHunter
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 25
Saving variables

Hello. Thought I would be able to solve this by myself with all those questions asked before about this and the wiki tutorial but cant get it to work.
What I would like to have is a empty list {} at start that will be filled with unknown number of Strings.

What I have added:
MyAddon.toc:
Code:
## SavedVariables: listDB
MyAddon.lua:
Code:
local list;
function onLoad()
  listDB = setmetatable(listDB or {}, {__index = defaults})
  list = listDB
  ...
end
To be honest I dont understand "setmetatable" but most examples seems to use it so I accept it.
According to me I should load the listDB at startup but I never save the new uppdated "listDB" at LOG_OUT event. Had some problems finding any good place telling me how to use ADDON_LOADED, VARIABLES_LOADED, PLAYER_LEAVING_WORLD and so on.
Have tried to add things like:
Code:
this:RegisterEvent("ADDON_LOADED");
without any result.
  Reply With Quote
12-05-08, 02:38 AM   #2
SetiHunter
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 25
Would like to update my code to something I can read. But this still dose not work:
MyAddon.toc:
Code:
## SavedVariables: listDB
MyAddon.lua:
Code:
local list;
function onLoad()
  if getmetatable(listDB) == nil then
    listDB = {}
    listDB = setmetatable(listDB, {})
  end
  list = listDB
  listDB = list
end
Is this close to a solution? As I can read it now it should create a metatable when it dosent exist and when it exists I save the table and then makes it updated all the time with "listDB=list"("hooking" if I understood it correct).
Any idea what I am missing?

Last edited by SetiHunter : 12-05-08 at 02:44 AM.
  Reply With Quote
12-05-08, 04:04 AM   #3
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
I'm not certain but I don't think variables are loaded at onload, also metatables are not copied to the WTF file so "getmetatable(listDB) == nil" will always evaluate false.

setmetatable(myVar or {}, {__index = default}) is used to apply defaults, so when you try to index a key with a nil value the default will be used instead.

i.e.

Code:
local default = {foo = "bar"}
local t = setmetatable({}, {__index = default})

print(t.foo)
would print "bar", because the t table is empty, so it falls back to the index entry of default.

Just doing
this:RegisterEvent("ADDON_LOADED");
would not do anything as you don't have an event handler.

So if you want a run of the mill Saved Variable handler (this is done using purely lua, as I don't like XML).

you could do:

Code:
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")

local list

local function OnEvent(self, event, addon)
	if addon ~= "myaddon" then return end
	if not MyVar then
		MyVar = {} --If MyVar is empty hand it a table
	end
	list = MyVar
	self:UnregisterEvent("ADDON_LOADED") --stop ADDON_LOADED from firing for this frame
	OnEvent = nil -- Clean up OnEvent for Garbage Collection
end

f:SetScript("OnEvent", OnEvent)
Hope this helps.
  Reply With Quote
12-05-08, 04:51 AM   #4
SetiHunter
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 25
Thanks a lot Slakah. Worked on the first try and I like that way of doing it.
But I´m having a bit hard to understand how the "RegisterEvent" commands are able to know when they should be called but I guess I get used to it. Only programmed Java before =)
  Reply With Quote
12-05-08, 02:05 PM   #5
Cralor
Mmm... cookies!!!
 
Cralor's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2007
Posts: 772
There is events in the WoW API. These events get "called" when certain things happen. For example, "ADDON_LOADED" gets fired when an addon loads (no brainer). Also, another event like "UNIT_RUNIC_POWER" gets fired when a player's (Death Knight's) Runic Power changes.

All you have to do to use them in your addon is to register them through the RegisterEvent.

Most of the time you will have to do
Code:
if (event == "EVENT_NAME_HERE") then
in your OnEvent function

Code:
function AddonName_OnEvent(self, event, addon)
And also: self = your frame (?)
event = any registered event you have (see above)
addon = your addon

"addon" is useful when you do a "ADDON_LOADED" event and you need another arg for just your addon so it isn't fired by other addons.

Hope this helps!!!
__________________
Never be satisfied with satisfactory.

Last edited by Cralor : 12-05-08 at 02:19 PM.
  Reply With Quote
12-05-08, 05:27 PM   #6
SetiHunter
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 25
Thanks for clarifying m8! =)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Saving variables


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