Reply
Thread Tools Display Modes
Unread 12-10-13, 03:35 PM   #1
dave20010
A Defias Bandit
Join Date: Dec 2013
Posts: 2
Saving Variables

I'm working on an AddOn but I can't seem to get certain variables to save in between sessions. I've looked at the wowwiki article, many posts on this forum, and random sites found through google. (Note: Most of the variable names and such in this article are changed)

.lua
Code:
local DB_DEFAULTS = {
		x = 0,
		y = 0,
		width = 100,
		height = 200,
}

local addon = CreateFrame("Frame", "MyAddon")
addon:RegisterEvent("ADDON_LOADED")

addon:SetScript("OnEvent", function(self, event, arg1)
	if event == "ADDON_LOADED" and arg1 == "MyAddon" then
	
		if (DB == nil) then
			print("hello") --Never gets printed
			DB = CopyDefaults(DB_DEFAULTS, DB) 
                        --Copies defaults if there is no saved data
		end
		
		Main() --Finishes frames
		self:UnregisterEvent("ADDON_LOADED")
		
	end
	
end)
.toc
Code:
## Interface: 50400
## Title: MyAddon
## Author: Dave20010
## Version: v1.0
## Notes: Addon for things
##SavedVariablesPerCharacter: DB

wat.lua
When I look at the SavedVariables .lua file in the WTF\Account\Accountname\Realmname\Charactername\SavedVariables folder, its contents are

Code:
DB = {
	["y"] = 949.9998168945313,
	["x"] = 2726.000244140625,
	["height"] = 350.0001220703125,
	["width"] = 221.9995574951172,
}
Where are these numbers coming from? And why doesn't the "if(DB == nil)" evaluate to nil if I delete the contents in the Addons SavedVariables file?
dave20010 is offline   Reply With Quote
Unread 12-10-13, 04:17 PM   #2
bsmorgan
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Mar 2005
Posts: 66
Savedvariables are loaded when a character logs on and saved when a character logs off (/reloadui does a save and a load). Deleting the savedvariables file while a character is logged on is ignored. I am, of course, constantly forgetting to log off before I make external changes so I am constantly reminded about how it works.

If you change the .toc then you must exit WoW completely and restart for the changes to be picked up.

A global variable named DB might not be unique. MyAddon_DB is probably unique.

Does this help solve your problem?

Last edited by bsmorgan : 12-10-13 at 04:19 PM.
bsmorgan is offline   Reply With Quote
Unread 12-10-13, 04:50 PM   #3
dave20010
A Defias Bandit
Join Date: Dec 2013
Posts: 2
Originally Posted by bsmorgan View Post
Savedvariables are loaded when a character logs on and saved when a character logs off (/reloadui does a save and a load). Deleting the savedvariables file while a character is logged on is ignored. I am, of course, constantly forgetting to log off before I make external changes so I am constantly reminded about how it works.

If you change the .toc then you must exit WoW completely and restart for the changes to be picked up.

A global variable named DB might not be unique. MyAddon_DB is probably unique.

Does this help solve your problem?
Yes, I got it all working now

I couldn't get the GetHeight() or GetWidth() functions working, but I was able to get the data via GetRect(). I feel so stupid for not trying to restart the client after deleting my addons SavedVariables folder, I knew about he ToC, but had no idea about that. Haha
dave20010 is offline   Reply With Quote
Unread 12-10-13, 05:48 PM   #4
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,601
Originally Posted by dave20010 View Post
I couldn't get the GetHeight() or GetWidth() functions working, but I was able to get the data via GetRect().
None of those functions are mentioned anywhere in any of the (obviously fake) snippets of code you posted.

Originally Posted by dave20010 View Post
(Note: Most of the variable names and such in this article are changed)
Then don't ****ing bother to post at all. Show your real code or GTFO. Yeah, I'm sure people are going to cry about this post because I'm being such a jerk, but seriously -- how on earth do you think it is even remotely reasonable for you to post some fake code that may or may not be related to anything your real code (and certainly doesn't represent ALL of your real code), and then ask people to help you fix a problem in your real code? Nobody here (or anywhere) is psychic. We cannot see your screen over the forums, we cannot magically comminicate with your hard drive with the power of our minds, and we cannot read your mind to know anything about your real code unless you show it to us. What you are doing is basically like going to an auto repair shop, giving the mechanic a Hot Wheels toy, and asking him why your Ferrari is making funny noises when you drive up steep hills. It's completely ridiculous, and a total waste of everyone's time. Don't do it.
__________________
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!
Phanx is offline   Reply With Quote
Unread 12-10-13, 06:23 PM   #5
bsmorgan
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Mar 2005
Posts: 66
Phanx is mostly right, post everything or a proper subset. What I mean by a proper subset is a portion of your addon that loads and demonstrates the error you want help with (i.e. a complete mini-addon).

Frankly, creating a proper subset is often more work than just posting the whole thing. Posting the whole thing has a added bonus in that you often get more help than you need. Many of the people that provide answers also provide an education by suggesting changes that improve your code or your technique.
bsmorgan is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » General Authoring Discussion » Saving Variables

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