Quantcast
Help with Loading and Using Saved Variables - WoWInterface
Thread Tools Display Modes
12-16-18, 09:42 AM   #1
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 305
Help with Loading and Using Saved Variables

So im trying to load and use the saved varables for my addon.
Here is the current lua code:
Code:
local addon, sActionbars = ...

SlashCmdList['RELOADUI'] = function()
	ReloadUI()
end
SLASH_RELOADUI1 = '/rl'

function sActionbars:RegisterDefaultSetting(key, value)
    if ( sActionbarsDB == nil ) then
        sActionbarsDB = {}
    end
    if ( sActionbarsDB[key] == nil ) then
        sActionbarsDB[key] = value
    end
end

function sActionbars:SetDefaultOptions()
    sActionbars:RegisterDefaultSetting("showHotKeys", false)
    sActionbars:RegisterDefaultSetting("showMacronames", false)	
end

function sActionbars_OnLoad(self)
    self:RegisterEvent("ADDON_LOADED")
end

function sActionbars_OnEvent(self, event, ...)
    if ( event == "ADDON_LOADED" ) then
        local name = ...
        if ( name == "sActionbars" ) then
            sActionbars:SetDefaultOptions()
            self:UnregisterEvent("ADDON_LOADED")
        end
    end
end
	
local hotkeyAlpha = sActionbarsDB.showHotKeys and 1 or 0
local macroAlpha = sActionbarsDB.showMacronames and 1 or 0

for i = 1, 12 do
	_G["ActionButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- main bar
	_G["MultiBarBottomRightButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- bottom right bar
	_G["MultiBarBottomLeftButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- bottom left bar
	_G["MultiBarRightButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- right bar
	_G["MultiBarLeftButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- left bar
end

for i = 1, 12 do
	_G["ActionButton"..i.."Name"]:SetAlpha(macroAlpha) -- main bar
	_G["MultiBarBottomRightButton"..i.."Name"]:SetAlpha(macroAlpha) -- bottom right bar
	_G["MultiBarBottomLeftButton"..i.."Name"]:SetAlpha(macroAlpha) -- bottom left bar
	_G["MultiBarRightButton"..i.."Name"]:SetAlpha(macroAlpha) -- right bar
	_G["MultiBarLeftButton"..i.."Name"]:SetAlpha(macroAlpha) -- left bar
end

local function UpdateRange( self, elapsed )
	local rangeTimer = self.rangeTimer
	local icon = self.icon;

	if( rangeTimer == TOOLTIP_UPDATE_TIME ) then
		local inRange = IsActionInRange( self.action );
		if( inRange == false ) then
			-- Red Out Button
			icon:SetVertexColor( 1, 0, 0 );
		else
			local canUse, amountMana = IsUsableAction( self.action );
			if( canUse ) then
				icon:SetVertexColor( 1.0, 1.0, 1.0 );
			elseif( amountMana ) then
				icon:SetVertexColor( 0.5, 0.5, 1.0 );
			else
				icon:SetVertexColor( 0.4, 0.4, 0.4 );
			end
		end
	end
end

do
	hooksecurefunc( "ActionButton_OnUpdate", UpdateRange );
end
Here is the xml file that loads before the lua file:
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="sActionbars.lua"/>
	
    <Frame name="sActionbars">
        <Scripts>
            <OnLoad function="sActionbars_OnLoad" />
            <OnEvent function="sActionbars_OnEvent" />
        </Scripts>
    </Frame>
	
</Ui>
But when i log into WoW it thows me this error:
Code:
1x sActionbars\sActionbars-8.1.0.lua:36: attempt to index global 'sActionbarsDB' (a nil value)
sActionbars\sActionbars-8.1.0.lua:36: in main chunk

Locals:
addon = "sActionbars"
sActionbars = <table> {
 SetDefaultOptions = <function> defined @sActionbars\sActionbars.lua:17
 RegisterDefaultSetting = <function> defined @sActionbars\sActionbars.lua:8
}
(*temporary) = nil
(*temporary) = <function> defined @sActionbars\sActionbars.lua:3
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = "attempt to index global 'sActionbarsDB' (a nil value)"
What am I doing wrong here?

Thanks
Coke
  Reply With Quote
12-16-18, 10:14 AM   #2
Kanegasi
A Firelord
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 487
sActionbarsDB={} needs to be at the very top of the file, the addon load process will fill it in.

As for the actual saving, do you have it in your .toc file?
  Reply With Quote
12-16-18, 10:22 AM   #3
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 305
Originally Posted by Kanegasi View Post
As for the actual saving, do you have it in your .toc file?
Yes its there here is the toc:
Code:
## Interface: 80100
## Title:|cff00B4FFs|rActionbars
## Version: 8.1.0
## SavedVariablesPerCharacter: sActionbarsDB

sActionbars.xml
  Reply With Quote
12-16-18, 12:02 PM   #4
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 176
I am missing the lua files(s) in the toc.
__________________
Better to fail then never have tried at all.
  Reply With Quote
12-16-18, 12:34 PM   #5
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 305
Originally Posted by Yukyuk View Post
I am missing the lua files(s) in the toc.
the lua files load from the xml file

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="sActionbars.lua"/>
	
    <Frame name="sActionbars">
        <Scripts>
            <OnLoad function="sActionbars_OnLoad" />
            <OnEvent function="sActionbars_OnEvent" />
        </Scripts>
    </Frame>
	
</Ui>
  Reply With Quote
12-17-18, 12:28 AM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,236
You're executing
Code:
local hotkeyAlpha = sActionbarsDB.showHotKeys and 1 or 0
As the file loads (as well as all the for i = 1, 12 do setup code) and

Code:
    if ( sActionbarsDB == nil ) then
        sActionbarsDB = {}
    end
after the file has loaded, created the frame, registered for, and recieved the event.

You could create local hotkeyAlpha as a nil variable at the top of the file and then initialise it after testing for the SV in the event but then you woulld still have to move the setup code into the event as well.

If you move all the ADDON_LOADED code into PLAYER_LOGIN and do everything there then you only have one event to worry about (no name test or unregister required as it only fires once after all ADDON_LOADED events and before PLAYER_ENTERING_WORLD) and you know that, by then, the game has loaded your SavedVariables, if they exist yet.

Lua Code:
  1. function sActionbars_OnEvent(self, event, ...)
  2.     sActionbars:SetDefaultOptions()
  3.  
  4.     local hotkeyAlpha = sActionbarsDB.showHotKeys and 1 or 0
  5.     local macroAlpha = sActionbarsDB.showMacronames and 1 or 0
  6.  
  7.     for i = 1, 12 do
  8.             _G["ActionButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- main bar
  9.             _G["ActionButton"..i.."Name"]:SetAlpha(macroAlpha) -- main bar
  10.             _G["MultiBarBottomRightButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- bottom right bar
  11.             _G["MultiBarBottomRightButton"..i.."Name"]:SetAlpha(macroAlpha) -- bottom right bar
  12.             _G["MultiBarBottomLeftButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- bottom left bar
  13.             _G["MultiBarBottomLeftButton"..i.."Name"]:SetAlpha(macroAlpha) -- bottom left bar
  14.             _G["MultiBarRightButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- right bar
  15.             _G["MultiBarRightButton"..i.."Name"]:SetAlpha(macroAlpha) -- right bar
  16.             _G["MultiBarLeftButton"..i.."HotKey"]:SetAlpha(hotkeyAlpha) -- left bar
  17.             _G["MultiBarLeftButton"..i.."Name"]:SetAlpha(macroAlpha) -- left bar
  18.     end
  19. end
  20.  
  21. function sActionbars_OnLoad(self)
  22.     self:RegisterEvent("PLAYER_LOGIN")
  23. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 12-17-18 at 02:45 AM.
  Reply With Quote
12-18-18, 07:12 AM   #7
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 305
Thanks Fizzlemizz for the stucture help,

For now i went back to basics sence im not sharing the addon/addons with anyone and i dont change my settings i just hard coded them in without the need of option tables

Thanks again
Coke
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help with Loading and Using Saved 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