WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   SavedVariables not writing table (https://www.wowinterface.com/forums/showthread.php?t=58438)

chrisronline 12-06-20 12:14 PM

SavedVariables not writing table
 
Hey folks,

I'm fairly new to lua and WoW addon development in general so hopefully this is a simple issue.

I have the following code as the only lua file loaded by my addon.


Code:

local REGION_NAMES = {
        [1] = 'us',
        [2] = 'kr',
        [3] = 'eu',
        [4] = 'tw',
        [5] = 'cn'
}
local DELIMITER = '::'
local MINOR = 95

local function getWardrobe()
        local wardrobe = {}
        for colType = 1, Enum.TransmogCollectionTypeMeta.NumValues do
                local app = C_TransmogCollection.GetCategoryAppearances(colType)
                for k, o in pairs(app) do
                        if o.isCollected and not o.isHideVisual then
                                for i, source in ipairs(C_TransmogCollection.GetAppearanceSources(o.visualID)) do
                                        local _, _, _, _, _, link = C_TransmogCollection.GetAppearanceSourceInfo(source.sourceID)
                                        local itemID = GetItemInfoInstant(link)
                                        local str = itemID .. ':' .. colType
                                        tinsert(wardrobe, str)
                                end
                        end
                end
        end
        return wardrobe
end

local function saveWardrobe()
        local region = REGION_NAMES[GetCurrentRegion()]
        local realm = GetRealmName()
        local character = UnitName('player')
        local wardrobe = getWardrobe()
        local hash = region .. ':' .. realm .. ':'.. character

        ArmoryHubExportData = ArmoryHubExportData and wipe(ArmoryHubExportData)  or {}
        ArmoryHubExportData.version = MINOR
        ArmoryHubExportData[hash] = ArmoryHubExportData[hash] or {}
        ArmoryHubExportData[hash].region = region
        ArmoryHubExportData[hash].realm = realm
        ArmoryHubExportData[hash].character = character
        ArmoryHubExportData[hash].wardrobe = wardrobe

        print('ArmoryHub: Saved wardrobe with ' .. table.getn(ArmoryHubExportData[hash].wardrobe) .. ' items')
end

local frame = CreateFrame('FRAME');
frame:RegisterEvent('ADDON_LOADED');
frame:RegisterEvent('PLAYER_LOGOUT');

function frame:OnEvent(event, arg1)
        if event == 'PLAYER_LOGOUT' then
                saveWardrobe()
        end
end

frame:SetScript('OnEvent', frame.OnEvent);
SLASH_ARMORYHUB1 = '/ah';
SlashCmdList['ARMORYHUB'] = function(msg)
        saveWardrobe()
end

In-game, the output says something like "ArmoryHub: Saved wardrobe with 5670 items".

However, when I open the SavedVariables file from my local disk, I see:

Code:


ArmoryHubExportData = {
        ["version"] = 95,
        ["us:Area 52:Dyspeptic"] = {
                ["wardrobe"] = {
                },
                ["character"] = "Dyspeptic",
                ["realm"] = "Area 52",
                ["region"] = "us",
        },
}

I don't know why the "wardrobe" field is not written out. I tried creating a simple table with a few entries and it writes it without issue. Is it a size issue? Is it trying to write too much at once?

briskman3000 12-06-20 01:18 PM

I may be wrong here, but I don't see the variable referenced in the global namespace. I am under the assumption that you have to at least define it in the global namespace first, before making any updates to the variable with local functions, in order for it to be saved properly.

I may be completely wrong here, but its how I do it.

Also, just double checking you do have the variable set up to be saved in the .toc file with the

## SavedVariables: variablename

line correct?

chrisronline 12-06-20 01:25 PM

Thanks for the quick reply!
Quote:

but I don't see the variable referenced in the global namespace.
Which one? `ArmoryHubExportData ` is the SavedVariable

It's in my .toc file:
Code:

## SavedVariables: ArmoryHubExportData

briskman3000 12-06-20 01:38 PM

Like i said, that part may have been wrong, so just ignore it.

My next guess would be that you are running the saveWardrobe function on logout, which calles the getWardrobe function, and it may be updating the table with empty data due to the timing???

Try commenting out the on logout portion of the code, and only have it update when you run the slash command, logout and see if that fixes it.

chrisronline 12-06-20 02:44 PM

Oh wow, that was it! It seems to be working now.

I guess when the log out event is emitted, the transmog API is returning nothing? Is there a way around this? Maybe a "pre log out" event of some kind?

myrroddin 12-06-20 09:47 PM

There is almost never any reason to expressly register and use PLAYER_LOGOUT to write your saved variables, as the game automatically writes SVs to disc during that event, meaning you don't have to do it.

The only reason I can think where someone might do it is if they use local references to longer SV tables and want to re-map the SVs to the local references. Usually nobody does this as it makes debugging a lot more difficult.

You can also use PLAYER_LOGIN to create or verify your SV table instead of ADDON_LOADED, mostly because PL only ever fires once and you don't have to parse the results until your addon is found.

chrisronline 12-07-20 12:15 PM

Thanks for that information!

The only reason I introduced the slash command is because I cannot seem to load the transmog data from the start.

This code:
Code:

function frame:OnEvent(event, arg1)
        if event == 'ADDON_LOADED' and arg1 == 'ArmoryHub' then
                local wardrobe = getWardrobe()
                print('ArmoryHub: From init, we see ' .. table.getn(wardrobe) .. ' items')
                print('ArmoryHub: Type /ah to generate the wardrobe')
        end
end

Shows `0 items` instead of how many show when I type in `/ah`


Is there some event I can listen to that will fire once certain APIs (such as transmog) are ready?

Seerah 12-07-20 12:26 PM

Then the information may not be available yet when your addon loads. All information about your character (presumably including what transmogs you know) should be available by PLAYER_LOGIN.

chrisronline 12-07-20 01:03 PM

Awesome, this works.

Thanks all so much!


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

vBulletin © 2021, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI