Logging the items my character acquired
Hi,
I am working on a simple addon, which is supposed to log (i.e. save in a text/lua file) the items that I acquire. This is for my own personal use and to keep track of which items I have acquired in the game so far. These items include anything my character can receive via loot, mail, trade, vendor, quest reward, crafting, containers etc. I have the following code so far: Lua Code:
It gets the item name correctly, it stores the item name correctly in the LootLog.lua file in savedvariables folder, but it stores it 183 times. When I acquire an item, it creates 183 entries with that item's name. When I acquire a second item, it creates another 183 entries with those two items' names. What is wrong with my code? I heard that to do it efficiently, I should register BAG_UPDATE after PLAYER_ENTERING_WORLD. How do I do that? Thank you very much. |
You are continually adding to an already existing table. At the very least, LootLog = {} should be performed on each parse ("BAG_UPDATE").
|
Quote:
Let's say, I acquired 100 items in a play session, I want them all to be logged in the LootLog.lua savedvariable file. In my next play session, say, I acquired 60 items, I want them to be added to the same file. A growing log of items in other words. |
Quote:
Also, I don't see how you are loading items from previous session. Instead of table.insert(LootLog, itemName) I would suggest using LootLog[itemName] = {}, assuming you don't want to track how many items you obtained. Edit. Also, there might be better events to watch, for example, "BAG_UPDATE_DELAYED" or events dealing with loot. Additionally, I think long time ago there was an addon that tracked what previously dropped from a mob. Due to AoE looting, it might be abandoned. |
The LootLog table initialization was fine where it was. The problem is that you're looking through your entire inventory every bag update, adding an item 183 times because you have 183 total slots. What I find weird is that it's adding the same item, but your code should add every item the way it's written.
In order to log a new item, you need to maintain a separate memory table that keeps track of what's in your bags, then log any difference. You can either log both additions and removals or just additions, like logging only if the bag/slot was empty and an item name was returned. I would write up a working example, but I'm on my phone. I'll see if I can help later today. |
Quote:
Quote:
Quote:
Quote:
With your tips and comments, I was finally able to do it, using the code below: Lua Code:
My last questions: Is my use of PLAYER_ENTERING_WORLD correct (I used it for efficiency, not sure). Is the code I have efficient? Thanks. |
You can use C_NewItems to query whether an item is new or not, then clear it manually.
Lua Code:
Note that in the case of stacks, your stack will not be flagged as new when you loot another item of that type. The default UI uses this class to highlight new items in your bags, and it also clears all new items whenever you close the container they're in. You'll have to do the lookup on BAG_UPDATE and clear them yourself before the container frame gets a chance to do it. This way you can track any new item that you acquire. |
You should try CHAT_MSG_LOOT. I don't know that it will fire for every conceivable situation, but I don't think BAG_UPDATE will work very well. First of all you'd need to keep track of what's actually changed between every update, and not just store everything in your bags. BAG_UPDATE also fires when items are removed, stacks are split, etc. Anything that requires the bag UI to update. What if you swap two items with each other, or put an equipped item in your bags?
|
Quote:
You probably need to work with multiple events and draw information from different sources. |
Quote:
Currently, my addon works just fine. My only concern is whether it is efficient or not. From a general programming perspective, using BAG_UPDATE is not efficient for identifying newly acquired items. I currently don't know how to use CHAT_MSG_LOOT, but I will check. If there was a simple ITEM_RECIEVED event, that would be perfect. Is there a way to check or compare two lua scripts in terms of efficiency? For example, using BAG_UPDATE vs using CHAT_MSG_LOOT. @MunkDev, the current code stores only the newly acquired items, so, I will note your suggestion about C_NewItems, but I will not use it for the moment. Thanks again. |
Link in gamepedia about CHAT_MSG_LOOT.
When comparing OnEvent scripts, both the frequency of the event firing, and the complexity of it (for example, getting info from inventory is slower than doing arithmetical operations with local variables). The frequency of different events can be estimated by assigning an integer to number of calls of particular event. The complexity of code can be evaluated by having it repeated CRAZY but the same number of times (say, 10000), and recording execution time. With BAG_UPDATE in this particular case it's looping through inventory many times per session despite (potentially) just rearranging items in bag/splitting a stack. With CHAT_MSG_LOOT the corresponding script of getting itemlink from the text, or saving the text as is. Moreover, it's supposed to run just when you receive an item. |
Quote:
It's unclear whether you want your addon to track items that you have acquired but no longer possess or just the items you currently have. Logging the items you acquire can mean both of these things and using C_NewItems is certainly not necessary in the latter case. If you, as an example, want to track how many of a particular item you've acquired over time then your current solution would not work. |
Quote:
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:
Final note, you can use /dump MyLootLog ingame to see the last 30 items you looted. |
Quote:
Quote:
Quote:
You see, as a newbie, I don't know how something should be done, or what's the best approach for something in WoW LUA. So, I am following tips and advice given here or there. Yesterday, I was thinking BAG_UPDATE is the way to go, today, it became obvious that I need to use CHAT_MSG_LOOT as it will be more efficient for what I am trying to accomplish, which is simply logging what items I acquire. Now, I need to figure out how to get the itemlink from the CHAT_MSG_LOOT event. |
Quote:
Test it, with, for example, this: Lua Code:
|
Quote:
Isn't there a way to directly trigger at CHAT_MSG_LOOT event, rather than checking all events and triggering IF the event is CHAT_MSG_LOOT? I mean your use of if event == my_event. I checked the eventtrace and noticed that hundreds of events are taking place in a minute, even when your character is just standing still. |
In this example check "if event == my_event" then can be omitted - the frame f is registered to listen to only "CHAT_MSG_LOOT". Forgot to delete. You can test by inserting after line 8 "else print(event)"
You might want to use string.find(where_to_search,what_to_search). |
Thanks to all the tips and help on this thread, I was finally able to finish my addon using the following. If you think it can be improved (i.e. made more efficient), please let me know.
Lua Code:
Such a simple thing, taking so long to come up with. Food for thought: What if I was in a group with a player from another realm with the same name? The (if player == "Eommus") check won't be enough then. |
Using itemID-s instead of names is a good idea. Saw that could be done but didn't want to overwhelm.
While still debugging, you might want to print itemId. Don't know much of regular expressions but does string:match("item:(%d+):") recognize that instead of [item]x2 you want to record [item]? I think it does but you might need to test. Does it detect that if you roll for item but loose, and doesn't record? Function Log_Loot isn't local. You are not using variables after player, so they can be omitted. I think players from another realm have "-realmname". You can test it by putting "else print(player)" after line 9, and running a random heroic/mythic without premade group. |
Quote:
The string:match I used gets the item ID from the chat message. LootLog[itemId] = {} ensures no duplicate entries are made. LootLog = {} ensures the entries are saved between sessions, in other words, my log keeps growing as I play. No idea about rolls yet, but the purpose of this addon is to log only items I acquire. There is one thing I am not sure if it will work, need to check it, some quests provide invisible reward items, that do not create a chat message, but their appearances are added to your Appearances. It won't be an issue though as I can manually add them to my database eventually, since they are recorded in the Appearances section. Should I make the function local? How? and why? Do you mean to remove other variables like this? Lua Code:
I will test cross realm player names next time I do a dungeon. Thanks so much. |
All times are GMT -6. The time now is 05:59 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI