View Single Post
07-28-20, 07:02 PM   #6
Walkerbo
A Cobalt Mageweaver
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 233
Hi all

My use case is I need to add the itemID of the item under the mouse to a table using a keybind.

If the item is already listed a fail message must print, otherwise add the itemID to the table.

Chunk of pseudo code;
Lua Code:
  1. local itemName, itemLink
  2. local TableOfItems = {}
  3.  
  4. local function addItemToTable (itemID)
  5.     for k, v in pairs(TableOfItems) do
  6.         if v == itemID then
  7.             print("Item already listed")
  8.         else
  9.             table.insert(TableOfItems, 1, itemID)
  10.             print("Item added to list")
  11.         end
  12.     end
  13. end
  14.  
  15. local btn = CreateFrame("BUTTON", "myTest")
  16. SetBindingClick("SHIFT-BUTTON1", "myTest","b1")
  17. btn:SetScript("OnClick", function(self, button)      
  18.       if button == "b1" then        
  19.         GameTooltip:HookScript('OnTooltipSetItem', function(self)
  20.             local  _, itemLink = self:GetItem()
  21.              local itemID = tonumber(strmatch(itemLink, "item:(%d+):"))
  22.              addItemToTable(itemID)
  23.          end)            
  24.       end
  25. end)
As the hover link spams I run the risk of having the first itemID added thus printing out the success message, yet the second and subsequent itemID fail messages will spam chat.

Idealy I would use the binding F5-BUTTON1 to operate on up/release to fire the addItemToTable function one time only.

Last edited by Walkerbo : 07-28-20 at 09:26 PM.
  Reply With Quote