Thread Tools Display Modes
01-15-09, 10:34 AM   #1
detheavn
A Kobold Labourer
Join Date: Jun 2008
Posts: 1
Kind of lost when adding saved variables

This is my first addon, so bear with me. It probably has been done numerous times, but since I'm trying to get the hang of this doing something relatively easy I'd appreciate any help

Most info I've scraped together from various sites like WoWWiki and addon fora, also looked through the code from addons in hope that I'd easily understand the basic syntax.

I've made a little addon for raid icon marking and it was functional in some shape way and form, but I botched up somewhere.

The code looks as follows:
Code:
MarkMyWorld = {}

--[[------------------------------------------------------------------------
	Settings
		MarkMyWorld will use these settings when there is no SavedVariables.
--]]

MarkMyWorld.scale	= 1
MarkMyWorld.x		= 0
MarkMyWorld.y		= 0

--[[------------------------------------------------------------------------
	MarkMyWorld:CheckSavedVars()
		Checks for Saved varables to use, if none present will create them.
--]]
function MarkMyWorld:CheckSavedVars()
	-- Setup Saved Variable defaults, if none exist.
	if (not(MarkMyWorldDB)) then DEFAULT_CHAT_FRAME:AddMessage("Setting up Saved Variables for MarkMyWorld") end
	if (not(MarkMyWorldDB)) then MarkMyWorldDB = {} end
	if (not(MarkMyWorldDB.scale)) then MarkMyWorldDB.scale = self.scale end
	if (not(MarkMyWorldDB.x)) then MarkMyWorldDB.x = self.x end
	if (not(MarkMyWorldDB.y)) then MarkMyWorldDB.y = self.y end
	self:SettingsChanged()
end

--[[-------------------------------------------------------------------------
	MarkMyWorld:SettingsChanged()
		Watches if settings change and sets them to current value.
--]]
function MarkMyWorld:SettingsChanged()
	if not(self.Frame.isMoving) then
		self.Frame:SetScale(MarkMyWorldDB.scale)
		self.Frame:SetPoint("CENTER", "UIParent", "CENTER", MarkMyWorldDB.x, MarkMyWorldDB.y)
	end
end

--[[------------------------------------------------------------------------
	MarkMyWorld:VARIABLES_LOADED()
		Calls when the SavedVariables loaded and MMW is able to access.
--]]
function MarkMyWorld:VARIABLES_LOADED()
	MarkMyWorld:CheckSavedVars()
end

--[[------------------------------------------------------------------------
	Run Once
		Creates the MMW frame.
--]]
do
	-- Create the Main Addon Frame
	MarkMyWorld.Frame = CreateFrame("Frame", "MarkMyWorldFrame", UIParent)
	MarkMyWorld.Frame:SetWidth((8*64)+10)
	MarkMyWorld.Frame:SetHeight(64+10)
	-- Set "Starting Point" (ToDo: Remember Location)
	MarkMyWorld.Frame:SetPoint("CENTER", "UIParent", "CENTER", MarkMyWorldDB.x, MarkMyWorldDB.y)
	MarkMyWorld.Frame:SetBackdrop({
		bgFile = [[Interface\Tooltips\UI-Tooltip-Background]]
		})
	MarkMyWorld.Frame:SetBackdropColor(.01, .01, .01, .6)
	-- Easy Scaling (ToDo: Slider to set scale manually)
	MarkMyWorld.Frame:SetScale(MarkMyWorldDB.scale)

	-- Make Movable
	MarkMyWorld.Frame:SetMovable(true)
	MarkMyWorld.Frame:EnableMouse(true)
	MarkMyWorld.Frame:SetScript("OnMouseDown", function()
		if IsShiftKeyDown() then
		this:StartMoving()
		end
	end)
	MarkMyWorld.Frame:SetScript("OnMouseUp", function()
		this:StopMovingOrSizing()
	end)

	-- Buttons
	-- Skull (1)
	MMWButtonSkull = CreateFrame("Button", "ButtonSkull", MarkMyWorld.Frame)
	MMWButtonSkull:SetWidth(64)
	MMWButtonSkull:SetHeight(64)
	MMWButtonSkull:SetPoint("TOPLEFT", MarkMyWorld.Frame, "TOPLEFT", 5, -5)
	MMWButtonSkull:SetNormalTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Skull]])
	MMWButtonSkull:SetHighlightTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Skull]])
	MMWButtonSkull:SetText("Skull")
	MMWButtonSkull:Enable()
	-- Marking Script
	MMWButtonSkull:SetScript("OnMouseDown", function()
		if UnitExists("target") then -- Check if Player Has Target
			SetRaidTargetIcon("target", 8) -- If Yes, Mark Target
			else
		end -- If Not, Stop Action
	end)

	-- Cross (2)
	MMWButtonCross = CreateFrame("Button", "ButtonCross", MarkMyWorld.Frame)
	MMWButtonCross:SetWidth(64)
	MMWButtonCross:SetHeight(64)
	MMWButtonCross:SetPoint("TOPLEFT", MarkMyWorld.Frame, "TOPLEFT", (5+(64*1)) , -5)
	MMWButtonCross:SetNormalTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Cross]])
	MMWButtonCross:SetHighlightTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Cross]])
	MMWButtonCross:Enable()
	-- Marking Script
	MMWButtonCross:SetScript("OnMouseDown", function()
		if UnitExists("target") then
			SetRaidTargetIcon("target", 7)
			else
		end
	end)

	-- Square (3)
	MMWButtonSquare = CreateFrame("Button", "ButtonSquare", MarkMyWorld.Frame)
	MMWButtonSquare:SetWidth(64)
	MMWButtonSquare:SetHeight(64)
	MMWButtonSquare:SetPoint("TOPLEFT", MarkMyWorld.Frame, "TOPLEFT", (5+(64*2)), -5)
	MMWButtonSquare:SetNormalTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Square]])
	MMWButtonSquare:SetHighlightTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Square]])
	MMWButtonSquare:Enable()
	-- Marking Script
	MMWButtonSquare:SetScript("OnMouseDown", function()
		if UnitExists("target") then
			SetRaidTargetIcon("target", 6)
			else
		end
	end)

	-- Moon (4)
	MMWButtonMoon = CreateFrame("Button", "ButtonMoon", MarkMyWorld.Frame)
	MMWButtonMoon:SetWidth(64)
	MMWButtonMoon:SetHeight(64)
	MMWButtonMoon:SetPoint("TOPLEFT", MarkMyWorld.Frame, "TOPLEFT", (5+(64*3)), -5)
	MMWButtonMoon:SetNormalTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Moon]])
	MMWButtonMoon:SetHighlightTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Moon]])
	MMWButtonMoon:Enable()
	-- Marking Script
	MMWButtonMoon:SetScript("OnMouseDown", function()
		if UnitExists("target") then
			SetRaidTargetIcon("target", 5)
			else
		end
	end)

	-- Triangle (5)
	MMWButtonTriangle = CreateFrame("Button", "ButtonTriangle", MarkMyWorld.Frame)
	MMWButtonTriangle:SetWidth(64)
	MMWButtonTriangle:SetHeight(64)
	MMWButtonTriangle:SetPoint("TOPLEFT", MarkMyWorld.Frame, "TOPLEFT", (5+(64*4)), -5)
	MMWButtonTriangle:SetNormalTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Triangle]])
	MMWButtonTriangle:SetHighlightTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Triangle]])
	MMWButtonTriangle:Enable()
	-- Marking Script
	MMWButtonTriangle:SetScript("OnMouseDown", function()
		if UnitExists("target") then
			SetRaidTargetIcon("target", 4)
			else
		end
	end)

	-- Diamond (6)
	MMWButtonDiamond = CreateFrame("Button", "ButtonDiamond", MarkMyWorld.Frame)
	MMWButtonDiamond:SetWidth(64)
	MMWButtonDiamond:SetHeight(64)
	MMWButtonDiamond:SetPoint("TOPLEFT", MarkMyWorld.Frame, "TOPLEFT", (5+(64*5)), -5)
	MMWButtonDiamond:SetNormalTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Diamond]])
	MMWButtonDiamond:SetHighlightTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Diamond]])
	MMWButtonDiamond:Enable()
	-- Marking Script
	MMWButtonDiamond:SetScript("OnMouseDown", function()
		if UnitExists("target") then
			SetRaidTargetIcon("target", 3)
			else
		end
	end)

	-- Circle (7)
	MMWButtonCircle = CreateFrame("Button", "ButtonCircle", MarkMyWorld.Frame)
	MMWButtonCircle:SetWidth(64)
	MMWButtonCircle:SetHeight(64)
	MMWButtonCircle:SetPoint("TOPLEFT", MarkMyWorld.Frame, "TOPLEFT", (5+(64*6)), -5)
	MMWButtonCircle:SetNormalTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Circle]])
	MMWButtonCircle:SetHighlightTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Circle]])
	MMWButtonCircle:Enable()
	-- Marking Script
	MMWButtonCircle:SetScript("OnMouseDown", function()
		if UnitExists("target") then
			SetRaidTargetIcon("target", 2)
			else
		end
	end)

	-- Star (8)
	MMWButtonStar = CreateFrame("Button", "ButtonStar", MarkMyWorld.Frame)
	MMWButtonStar:SetWidth(64)
	MMWButtonStar:SetHeight(64)
	MMWButtonStar:SetPoint("TOPLEFT", MarkMyWorld.Frame, "TOPLEFT", (5+(64*7)), -5)
	MMWButtonStar:SetNormalTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Star]])
	MMWButtonStar:SetHighlightTexture([[Interface\Addons\MarkMyWorld\images\RI_Button_Star]])
	MMWButtonStar:Enable()
	-- Marking Script
	MMWButtonStar:SetScript("OnMouseDown", function()
		if UnitExists("target") then
			SetRaidTargetIcon("target", 1)
			else
		end
	end)
	
	-- Prints to chat frame that addon loaded.
	DEFAULT_CHAT_FRAME:AddMessage("MarkMyWorld loaded.")
end
I'm probably overlooking something simple, or I've not really thought up a good enough structure here, but any help will be greatly appreciated.

Thx
  Reply With Quote
01-15-09, 12:42 PM   #2
Mikord
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 61
Here is a link to a post where I provided a basic framework to somebody else having issues with saved variables.

http://www.wowinterface.com/forums/s...0&postcount=15

Hopefully you will find it useful.
  Reply With Quote
01-15-09, 01:06 PM   #3
Mera
Retired of WoW, In ESO :)
 
Mera's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 331
mikord links clearly resume how it works, a savedvar is nothing much than a global var that is saved by the client each time you reloadu the ui or disconnect.
You have to define the global var in the toc file for this as in mik sample he did

## SavedVariables: TestSV

then anything you define in the var TestSV.* will be saved

else if you want to add AceDB-2.0 support you can do it easly so you can get a look on my ace2 mod easydnd for that
__________________
If you need to reach me I'm in ESO, @class101 or "Fathis Ules i"
addons: SpamBayes, BrokerCPU
projects: ThunderBayes
Mera[xeh]? - La CroisadeEcarlate (wow)

Last edited by Mera : 01-15-09 at 01:10 PM.
  Reply With Quote
02-22-09, 09:54 PM   #4
eliljey
A Deviate Faerie Dragon
Join Date: Feb 2009
Posts: 14
This doesn't seem to be working for me. I'm sure I'm just being incredibly oblivious here but for some reason the whole saved vars thing still isn't clicking for me.

Putting the variables in the .toc adds them to the "gotta save these" list and I've verified that's working by looking at the actual saved file. If I am understanding things correctly, these variables are loaded automatically when the addons are loaded. (This begs the question as to why you need to pull them into the addon to begin with. If they're loaded then why can' you just use them? But I don't need to understand the underlying mechanics of how WoW works. It just makes me wonder.)

In the above link the function is relatively straightforward but I'm not clear about the frame stuff at the end:

Code:
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:SetScript("OnEvent", OnEvent)
If I understand correctly, eventFrame is a dummy frame that's required to tie events to but nothing more. Then we tie the ADDON_LOADED event to it and also tell it that OnEvent is the script (function) to run when the event occurs.

Is all that correct? Does that mean that the name of the frame is irrelevant and I just need to stick this anywhere in my code and it'll work or does this somehow need to be triggered by some other piece of code?

I have the stuff from the link in my code and the first thing it does is print a dummy message to show that I've entered the function and I'm not even getting that so for some reason the ADDON_LOADED event isn't even kicking things off let alone causing the saved vars stuff to work correctly.

Last edited by eliljey : 02-22-09 at 09:56 PM.
  Reply With Quote
02-22-09, 11:42 PM   #5
Tuhljin
A Flamescale Wyrmkin
 
Tuhljin's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2008
Posts: 106
Originally Posted by eliljey View Post
Putting the variables in the .toc adds them to the "gotta save these" list and I've verified that's working by looking at the actual saved file. If I am understanding things correctly, these variables are loaded automatically when the addons are loaded. (This begs the question as to why you need to pull them into the addon to begin with. If they're loaded then why can' you just use them? But I don't need to understand the underlying mechanics of how WoW works. It just makes me wonder.)
The global variable that you've placed in the "'gotta save these' list," as you put it, is automatically set to the value saved in relevant file in the WTF folder, but not before the addon begins loading, which means that the global will generally be nil when your addon's code first starts executing. It's not until the ADDON_LOADED event (with arg1 set as your addon's name) that the saved values, if any, have been restored.

You might find this WoWWiki "How To" on events helpful: Handling events.
  Reply With Quote
02-23-09, 02:14 AM   #6
eliljey
A Deviate Faerie Dragon
Join Date: Feb 2009
Posts: 14
So is the saved vars handler just a method for letting the addon know when the variables are ready for use?

Is my assessment of how the frame is used correct? Because I can't even detect the "ADDON_LOADED" event. Is there some way that needs to get called?

Edit:
I figured it out I think. I needed to tie all that stuff to my main frame in my "OnLoad" function. Once I did that it all fell into place.

Last edited by eliljey : 02-23-09 at 03:10 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Kind of lost when adding 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