Thread Tools Display Modes
10-23-12, 11:23 AM   #1
Hallur
A Murloc Raider
Join Date: May 2012
Posts: 4
Need help with saved variables

MyAddOn.toc
Code:
## Interface: 50001
## Title: My AddOn
## SavedVariablesPerCharacter: XCoordOffSet, YCoordOffSet
MyAddOn.xml
MyAddOn.xml
Code:
<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">
	<Script file="MyAddOn.lua" />
	<Frame name="MAO_Main_Frame" parent="UIParent">
		<Size>
			<AbsDimension x="50" y="50" />
		</Size>
		<Anchors>
			<Anchor point="center" />
		</Anchors>
		<Backdrop bgFile="Interface\TutorialFrame\TutorialFrameBackground" />
		<Scripts>
			<OnLoad function="RegisterEvents" />
			<OnEvent function="EventHandler" />
		</Scripts>
	</Frame>
</Ui>
MyAddOn.lua
Code:
function RegisterEvents(self)
	self:RegisterEvent("ADDON_LOADED")
end

function EventHandler(self, event, arg1)
	if event == "ADDON_LOADED" and arg1 == "MyAddOn" then
		InitializeSavedVariables()

		MAO_Main_Frame:SetPoint("CENTER", XCoordOffset, YCoordOffSet)

		print(arg1 .. " loaded.")
	end
end

function InitializeSavedVariables()
	if XCoordOffSet == nil then
		XCoordOffSet = 0
	end

	if YCoordOffSet == nil then
		YCoordOffSet = 0
	end
end
I set the XCoordOffSet saved variable in-game with "/run XCoordOffSet = 50" and I confirmed that it changed by checking the saved variables file, but it still doesn't position my frame at x=50, y=0 like I thought it would. If I change line 9 in MyAddOn.lua to "MAO_Main_Frame:SetPoint("CENTER", 50, 0)" it works, but it doesn't work when I call my saved variables.

What am I doing wrong?
  Reply With Quote
10-23-12, 11:36 AM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Hallur View Post
MyAddOn.toc
Code:
## SavedVariablesPerCharacter: XCoordOffSet, YCoordOffSet
MyAddOn.lua
Code:
MAO_Main_Frame:SetPoint("CENTER", XCoordOffset, YCoordOffSet)
Variables are case-sensitive. When you call your frame:SetPoint(), you're using XCoordOffset instead of XCoordOffSet.
__________________
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 : 10-23-12 at 11:40 AM.
  Reply With Quote
10-23-12, 11:42 AM   #3
Hallur
A Murloc Raider
Join Date: May 2012
Posts: 4
Oh, wow... that's embarrassing. Well, thanks!
  Reply With Quote
10-23-12, 07:24 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
On a side note, you should avoid generic names like that for saved variables. Saved variables are global, so they should have names that (1) are unlikely to conflict with other addons or the default UI, and (2) identify them as belonging to your addon. For example, "MyAddOn_X" and "MyAddOn_Y" would be better choices.

However, if you're storing more than one or two variables, it would be more efficient (not to mention less code to initialize) to store them all in a single table:

TOC:
Code:
## SavedVariablesPerCharacter: MyAddonSettings
Lua:
Code:
function MyAddOn:ADDON_LOADED(addon) -- or however you are responding to events
    if addon ~- "MyAddOn" then return end -- not your addon

    -- your addon loaded
    local defaults = {
        x = 100,
        y = 200,
        alpha = 1,
        scale = 0.8,
    }

    local settings = MyAddOnSettings
    if not settings then
        -- this addon hasn't been loaded before with this saved variable defined
        settings = {}
        MyAddOnSettings = settings
    end

    -- go through the defaults and make sure your table has them:
    for k, v in pairs(defaults) do
        if settings[k] == nil then
            settings[k] = v
        end
    end

    -- now you can read and write your settings like this:
    MyAddOnFrame:SetScale(settings.scale)
    settings.alpha = 0.5
end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 10-23-12 at 07:27 PM.
  Reply With Quote
10-23-12, 07:41 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
I didn't notice this before and Phanx's code doesn't have this problem, but setting an initial value to a saved variable in response to the ADDON_LOADED event will overwrite the saved variable as the data is loaded directly before the event fires. It is customary to put the initial values in the main block of your code, outside of any function. That way, you have your defaults set before WoW loads the saved data.
__________________
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
10-25-12, 03:58 AM   #6
Hallur
A Murloc Raider
Join Date: May 2012
Posts: 4
Thanks a lot to both of you
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Need help with 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