View Single Post
05-19-17, 11:00 PM   #13
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Originally Posted by Eommus View Post
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:
  1. local a,e=...
  2. local LL=CreateFrame('frame',a)
  3. LL:RegisterEvent('ADDON_LOADED')
  4. LL:RegisterEvent('PLAYER_LOGIN')
  5. local llog,bags={},{} -- local log and image tables
  6. MyLootLog,MyCurrentBags={},{} -- initialize SavedVariables
  7.  
  8. function e.ADDON_LOADED(addon)
  9.     if addon==a then -- load saved data into local tables, data is automatically linked between tables
  10.         bags=MyCurrentBags
  11.         llog=MyLootLog
  12.     end
  13. end
  14.  
  15. function e.PLAYER_LOGIN()
  16.     LL:RegisterEvent('BAG_UPDATE') -- delays BAG_UPDATE until full login in attempt to minimize processing impact
  17. end
  18.  
  19. function e.BAG_UPDATE()
  20.     local move,new={} -- temp tables for moved and new items
  21.     for b=0,NUM_BAG_SLOTS do for s=1,GetContainerNumSlots(b) do -- loop through bags
  22.         local _,item=strsplit('[]',GetContainerItemLink(b,s) or '') -- split item link into three parts, second part being the name
  23.         if bags[b..s] and bags[b..s]~=item then -- if something was there and current doesn't match...
  24.             move[bags[b..s]]=1 -- ...assume item was moved...
  25.             new[bags[b..s]]=nil -- ...and remove from new if there
  26.         elseif item and item~=bags[b..s] then -- there's an item there that wasn't there...
  27.             if not move[item] then new[item]=1 end -- ...so add to new if not moved
  28.         end
  29.         bags[b..s]=item -- update bag image
  30.     end end
  31.     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
  32. end
  33.  
  34. 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.

Last edited by Kanegasi : 05-19-17 at 11:28 PM.
  Reply With Quote