Originally Posted by Eommus
Yes, I read that it is done like that to save data between sessions. Though, I am just a newbie to LUA, so, just trying to stitch together bits of info here or there.
|
That is called SavedVariables. They need to be declared in your .toc file in order to save them. Otherwise, what you have there is just a global table that will be discarded upon logout.
I have drycoded (as in, not tested) something that should log everything new. The first time you run this, it will assume everything in your bags is new. You are safe to immediately logout, open the saved variable file, and delete "MyLootLog" after this first run. Leave the other table, "MyCurrentBags", alone. The second table is important to ensure what you log is new.
First step, open your .toc file for this addon and add the following line to the header:
Code:
## SavedVariables: MyLootLog,MyCurrentBags
Secondly, replace all the code you have with the following:
lua Code:
local a,e=...
local LL=CreateFrame('frame',a)
LL:RegisterEvent('ADDON_LOADED')
LL:RegisterEvent('PLAYER_LOGIN')
local llog,bags={},{} -- local log and image tables
MyLootLog,MyCurrentBags={},{} -- initialize SavedVariables
function e.ADDON_LOADED(addon)
if addon==a then -- load saved data into local tables, data is automatically linked between tables
bags=MyCurrentBags
llog=MyLootLog
end
end
function e.PLAYER_LOGIN()
LL:RegisterEvent('BAG_UPDATE') -- delays BAG_UPDATE until full login in attempt to minimize processing impact
end
function e.BAG_UPDATE()
local move,new={} -- temp tables for moved and new items
for b=0,NUM_BAG_SLOTS do for s=1,GetContainerNumSlots(b) do -- loop through bags
local _,item=strsplit('[]',GetContainerItemLink(b,s) or '') -- split item link into three parts, second part being the name
if bags[b..s] and bags[b..s]~=item then -- if something was there and current doesn't match...
move[bags[b..s]]=1 -- ...assume item was moved...
new[bags[b..s]]=nil -- ...and remove from new if there
elseif item and item~=bags[b..s] then -- there's an item there that wasn't there...
if not move[item] then new[item]=1 end -- ...so add to new if not moved
end
bags[b..s]=item -- update bag image
end end
for k in next,new do tinsert(llog,k) end -- loop through new and add to log, should only be one item to add each event, except for first run
end
LL:SetScript('OnEvent',function(_,event)e[event]()end) -- calls registered events
Final note, you can use
/dump MyLootLog ingame to see the last 30 items you looted.