WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Help with Loading and Using Saved Variables (https://www.wowinterface.com/forums/showthread.php?t=56902)

cokedrivers 12-16-18 09:42 AM

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

Kanegasi 12-16-18 10:14 AM

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?

cokedrivers 12-16-18 10:22 AM

Quote:

Originally Posted by Kanegasi (Post 331051)
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


Yukyuk 12-16-18 12:02 PM

I am missing the lua files(s) in the toc.

cokedrivers 12-16-18 12:34 PM

Quote:

Originally Posted by Yukyuk (Post 331053)
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>


Fizzlemizz 12-17-18 12:28 AM

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

cokedrivers 12-18-18 07:12 AM

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


All times are GMT -6. The time now is 06:46 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI