Thread Tools Display Modes
04-08-09, 10:02 AM   #1
Soeters
A Warpwood Thunder Caller
 
Soeters's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 97
New Saved variables

I'm making an addon to easily provide roles for raid icons. I've made a config window inspirated by tekKonfig-AboutPanel which needs to show the text from the saved variables.
I'm using p3lim's code to copy the defaults into the SavedVariables if the values are nil:
Code:
local function LoadDefaults()
	for k,v in next, defaults do
		if(type(AssignDB[k]) == 'nil') then
			AssignDB[k] = v
		end
	end 
end

function Assign:OnInitialize()
	LoadDefaults()
end
If this is the first time the addon is loaded I got an error the tells me that I'm trying to index global "AssignDB" in my LoadDefaults() function.
But if I just manually add a value in the SavedVariables like this
Code:
AssignDB = {
	["dummy"] = 1,
}
The function copies the default table well and I got no error.

I just want to know if someone can tell me why I have a such error if my SavedVariables table is nil.

My two lua files are here if it can help: Main Config
__________________

Last edited by Soeters : 04-08-09 at 10:14 AM. Reason: New test
  Reply With Quote
04-08-09, 10:13 AM   #2
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
It's happening because AssignDB hasn't been given a table, so do:

lua Code:
  1. AssignDB = AssignDB or defaults

So AssignDB will be handed the table defaults when AssignDB hasn't already been assigned a value (if that makes any sense what so ever :P).

To elaborate, a saved variable is something which is saved on logout and renewed on log in, so if I created a saved variable x and assigned it a value "foo" then when I reload x will still equal "foo", and if I assigned x a value of nil then it will continue to be nil on reload, and if it's the first time I load a mod, and I don't assign any value to x it will continue to equal nil on all reloads.

Last edited by Slakah : 04-08-09 at 10:22 AM.
  Reply With Quote
04-08-09, 12:08 PM   #3
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
using a metatable does most of it for you:
Code:
AssignDB = setmetatable(AssignDB or {}, {__index = defaults})
'defaults' is your defaults table of course
  Reply With Quote
04-08-09, 12:33 PM   #4
Soeters
A Warpwood Thunder Caller
 
Soeters's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 97
Originally Posted by Slakah View Post
lua Code:
  1. AssignDB = AssignDB or defaults
I haven't tried this so I'll see.

Originally Posted by p3lim View Post
Code:
AssignDB = setmetatable(AssignDB or {}, {__index = defaults})
I already tried with this but I got exactly the same error ( tried to index global 'AssignDB')
__________________
  Reply With Quote
04-08-09, 01:14 PM   #5
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Originally Posted by Soeters View Post
I haven't tried this so I'll see.



I already tried with this but I got exactly the same error ( tried to index global 'AssignDB')
Remember the toc metadata
  Reply With Quote
04-09-09, 09:16 AM   #6
Soeters
A Warpwood Thunder Caller
 
Soeters's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 97
I retried and it worked (and still works) well.

Thanks P3lim and Slakah.


But now I get another problem. I use this code to handle my savedvariables
Code:
AssignDB = AssignDB or {}
AssignDB.icons = setmetatable(AssignDB.icons or {}, {__index = defaults})
AssignDB.channel = setmetatable(AssignDB.channel or {}, {__index = channels})
Everytime I use my function to send assignments, it reads the messages contained in AssignDB.icons so I should have the values from AssignDB.icons if the value exist or from the defaults.
But instead of that it only reads the values from AssignDB.icons which is problematic : if I use the defaults values and I want to send assignments I must change every message to add them to the saved variables.

So I really want to know a way like setmetatable which works perfect for my configuration frame but for my assignments values. I already tried to make a local metatable in my function which reads from AssignDB.icons or defaults.
__________________

Last edited by Soeters : 04-09-09 at 10:29 AM.
  Reply With Quote
04-18-09, 09:24 AM   #7
Soeters
A Warpwood Thunder Caller
 
Soeters's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 97
Question

Nobody can help ?
__________________
  Reply With Quote
04-19-09, 05:06 AM   #8
Aezay
A Theradrim Guardian
 
Aezay's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 66
Originally Posted by Soeters View Post
Everytime I use my function to send assignments, it reads the messages contained in AssignDB.icons so I should have the values from AssignDB.icons if the value exist or from the defaults.
But instead of that it only reads the values from AssignDB.icons which is problematic : if I use the defaults values and I want to send assignments I must change every message to add them to the saved variables.

So I really want to know a way like setmetatable which works perfect for my configuration frame but for my assignments values. I already tried to make a local metatable in my function which reads from AssignDB.icons or defaults.
I am not quite sure what you mean by this, could you try and explain it in another way?
  Reply With Quote
04-19-09, 06:12 AM   #9
Soeters
A Warpwood Thunder Caller
 
Soeters's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 97
I have a function which send the messages contained in AssignDB.icons. With the metatable, each time I call the function I should have all values: the modified from AssignDB.icons and the ones not modified from my default table.
To access the table I use for
Code:
 k,v in pairs (AssignDB.icons) do
(...) 
end
But this only read the modified values and not the ones from the default table. But for the messages displayed in the Blizz addon frame, which uses the same table, I got the modified and defaults ones.
__________________
  Reply With Quote
04-19-09, 06:23 AM   #10
Aezay
A Theradrim Guardian
 
Aezay's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 66
Ahh, when using an iterator over a table, it wont invoke the metatable, only direct access will.

Maybe this would be a solution
Code:
for k,v in next, defaults do
	HandleIcon(rawget(AssignDB.icons,k) or v);
end

Last edited by Aezay : 04-19-09 at 06:27 AM.
  Reply With Quote
04-19-09, 08:35 AM   #11
Soeters
A Warpwood Thunder Caller
 
Soeters's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 97
But if I want to use it in a function what do I have to do because I don't really understand what rawget is and what it does.

Here's my function:

Code:
function Assign:SendAssignments(icon, channel, to)
 local icons = setmetatable(AssignDB.icons or {}, {__index = defaults})
	if icon == "all" or (not icon) then
		for icon, message in pairs(icons) do
			if (not channel) then
				SendChatMessage(("{"..icon.."} "..string.upper(string.sub(icon,1,1))..string.sub(icon,2)..": "..message),AssignDB.channel["Default channel Name"])
			elseif string.lower(channel) ~= "whisper" then
				SendChatMessage(("{"..icon.."} "..string.upper(string.sub(icon,1,1))..string.sub(icon,2)..": "..message),string.upper(channel))
			else
				SendChatMessage(("{"..icon.."} "..string.upper(string.sub(icon,1,1))..string.sub(icon,2)..": "..message),string.upper(channel), nil, to)
			end
		end
	end			

end
Thanks for helping Aezay
__________________
  Reply With Quote
04-19-09, 10:14 AM   #12
Aezay
A Theradrim Guardian
 
Aezay's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 66
rawget accesses a table without invoking any of its metatable methods.

I'm a little unsure how your function is suppose to work. I'm guessing you have some icons in "defaults" and you want to be able to override them in "AssignDB.icons"?
Since a table iterator doesn't return entries from the metatable, you will either have to iterate over "defaults" and just read from "AssignDB.icons". But in case your code allows to add extra entries into the "AssignDB.icons" table, that wont work, and you will pretty much have to scan both tables, or just initialise the "AssignDB.icons" table with defaults instead of using a metatable for it.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » New Saved 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