Okay, scratch the last reply.
It only works that way if I log in with an EMPTY!!!! Saved Variables table. Which totally defeats the point.
If I log in with an empty table that games session will correctly traverse the table ( maybe without the need for adding the history header table but I am not spending another half hour changing it back now ) but the moment I tell it not to clear the table out the old traverse problem returns.
By the way, what the heck is that version value for ? Should I set that to another value ?
Can some bright spark see what the heck I am doing wrong here or is it a bug in the SavedVariables table?
edit: As you can see this is annoying me to no end as I have wasted my beta weekend writing this addon and haven't been able to finish it because of this problem.
Here is my code in its entirety barring the Update routine being called from the XML as per the norm.
Lua Code:
-- Values used often so created variables for them
local addonName = "XrysGatherer"
local addonVersion = "0.0.1"
local addonSV = addonName.."_SavedVariables"
-- References to System and Manager objects
local ChatMsg = CHAT_SYSTEM
local EventMgr = EVENT_MANAGER
-- References to the Saved Variables Data
local SVDefault = {}
local SVData = {}
local SVDataIndex = 0
-- Whether the player is currently interacting with something
local interacting = false
-- Whether the player wants to view his history
local showHistoryAtLogin = true
-- Initialise the Saved Variables File
-- Found in EsoHead addon ( thanks )
local function InitSavedVariables(...)
SVData = {
["History"] = ZO_SavedVars:NewAccountWide("XrysGatherer_SavedVariables", 1, "History", SVDefault)
}
SVDataIndex = 1
end
-- Check if the item already exists in the database in that zone and location ( or very close to it )
-- Variation of the test found in Esohead addon ( thanks )
local function SVDataExists(xPos,yPos,zone,name)
local found = false
local zoneData = SVData["History"][zone]
if not zoneData then return false end
local nameData = zoneData[name]
if not nameData then return false end
for i,v in pairs(nameData) do
local x,y = v["X"],v["Y"]
if math.abs(xPos - x) < 0.005 and math.abs(yPos - y) < 0.005 then
found = true
break
end
end
if found then return true end
return false
end
-- Update Routine to track harvest interactions which do not have an event at present
function XrysGatherer_Update(...)
-- Is the player interacting with something ?
local playerInteracting = IsPlayerInteractingWithObject()
if not playerInteracting then interacting = false return end
-- Check if the Interaction Type is Harvest
local interactMode = GetInteractionType()
if interactMode ~= INTERACTION_HARVEST then return end
-- Check if we are still processing the last interaction update cycle
if interacting then return end
interacting = true
-- Get some important information into variables for use later
local unitID = "player"
local zone = GetUnitZone(unitID)
local xPos,yPos,zPos = GetMapPlayerPosition(unitID)
local action, name, blocked, extra, context = GetGameCameraInteractableActionInfo()
local dateValue = GetDate()
local timeValue = GetTimeString()
-- Just in case it thinks crafting is a harvestable object
if action == "Use" then
ChatMsg:AddMessage(string.format("Action: %s Name: %s Mode: %d Extra: %d",action,name,interactMode,extra))
return
end
-- Does the node exist in the database ?
local nodeExists = SVDataExists(xPos,yPos,zone,name)
local NewOrOld = "New"
if nodeExists then NewOrOld = "Old" end
-- Display information on the screen regardless if it is an old or new node
ChatMsg:AddMessage(string.format("[%s] %s: %s at ( x:%0.3f, y:%0.3f ) in %s",NewOrOld,action,name,xPos,yPos,zone))
-- Only update the saved variable file if the node doesn't exist in there yet
if not nodeExists then
ChatMsg:AddMessage("Adding Harvest Node to Database")
SVData = SVData or {}
SVData["History"] = SVData["History"] or {}
SVData["History"] = SVData["History"] or {}
SVData["History"][zone] = SVData["History"][zone] or {}
SVData["History"][zone][name] = SVData["History"][zone][name] or {}
SVDataIndex = #SVData["History"][zone][name] + 1
SVData["History"][zone][name][SVDataIndex] = SVData["History"][zone][name][SVDataIndex] or {}
SVData["History"][zone][name][SVDataIndex]["Action"] = action
SVData["History"][zone][name][SVDataIndex]["X"] = xPos
SVData["History"][zone][name][SVDataIndex]["Y"] = yPos
SVData["History"][zone][name][SVDataIndex]["Date"] = dateValue
SVData["History"][zone][name][SVDataIndex]["Time"] = timeValue
end
ChatMsg:AddMessage("Traversing Harvest History")
for history,sv in pairs(SVData) do
if history == "History" then
for zone, t1 in pairs(SVData[history]) do
for item, t2 in pairs(SVData[history][zone]) do
for index, t3 in pairs(SVData[history][zone][item]) do
local data = SVData[history][zone][item][index]
ChatMsg:AddMessage(string.format("%d: %s %s in %s at %0.3f,%0.3f",index,item,tostring(data["Action"]),zone,data["X"],data["Y"]))
end
end
end
end
end
end
-- Our Addon is loaded so we can start work
local function AddOnLoaded(eventID,addon)
if addon ~= addonName then return end
EventMgr:UnregisterForEvent(addonName,eventID)
ChatMsg:AddMessage(string.format("%s %s Loaded",addonName,addonVersion))
InitSavedVariables()
-- Have to have this here at this point to get the table traversing properly that session.
-- If I remove this line the function error appears again and it won't traverse.
SVData["History"] = {}
end
-- Track addons loading
EventMgr:RegisterForEvent( addonName ,EVENT_ADD_ON_LOADED , AddOnLoaded )
Here is a screenshot of what is happening when I try and traverse through the table.
If I change the traversing to the following in the hopes of bypassing the other stuff I get the same problem:
Lua Code:
for zone, t1 in pairs(SVData[history]) do
for item, t2 in pairs(SVData[history][zone]) do
for index, t3 in pairs(SVData[history][zone][item]) do
local data = SVData[history][zone][item][index]
ChatMsg:AddMessage(string.format("%d: %s %s in %s at %0.3f,%0.3f",index,item,tostring(data["Action"]),zone,data["X"],data["Y"]))
end
end
end
Again, only if the data exists when logging in.