Quantcast
SavedVariables not writing table - WoWInterface
Thread Tools Display Modes
12-06-20, 12:14 PM   #1
chrisronline
A Murloc Raider
Join Date: Dec 2020
Posts: 5
Exclamation 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?
  Reply With Quote
12-06-20, 01:18 PM   #2
briskman3000
A Warpwood Thunder Caller
 
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 90
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?
__________________
My Addons: Convert Ratings Honor Track
  Reply With Quote
12-06-20, 01:25 PM   #3
chrisronline
A Murloc Raider
Join Date: Dec 2020
Posts: 5
Thanks for the quick reply!
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
  Reply With Quote
12-06-20, 01:38 PM   #4
briskman3000
A Warpwood Thunder Caller
 
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 90
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.
__________________
My Addons: Convert Ratings Honor Track
  Reply With Quote
12-06-20, 02:44 PM   #5
chrisronline
A Murloc Raider
Join Date: Dec 2020
Posts: 5
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?
  Reply With Quote
12-06-20, 09:47 PM   #6
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,152
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.
  Reply With Quote
12-07-20, 12:15 PM   #7
chrisronline
A Murloc Raider
Join Date: Dec 2020
Posts: 5
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?
  Reply With Quote
12-07-20, 12:26 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,820
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.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
12-07-20, 01:03 PM   #9
chrisronline
A Murloc Raider
Join Date: Dec 2020
Posts: 5
Awesome, this works.

Thanks all so much!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » SavedVariables not writing table

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