WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Getting item details on hover (https://www.wowinterface.com/forums/showthread.php?t=58118)

Walkerbo 07-22-20 07:32 PM

Getting item details on hover
 
Hi all

I am looking for some help in regards to getting an itemID when I click an item in the adventure guide, my bags and my bank.

I have worked out how to get the itemID of items in my bags and bank but it requires that I have to pick up the item first.

Lua Code:
  1. local btn = CreateFrame("BUTTON", "myTest")
  2. SetBindingClick("ALT-CTRL-BUTTON1", "myTest","b1")
  3. SetBindingClick("BUTTON2", "myTest","b2")
  4. SetBindingClick("ALT-SHIFT-T", "myTest","st")
  5. btn:SetScript("OnClick", function(self, button)
  6.       if button == "b1" then
  7.          local cursorItemType, cursorItemID = GetCursorInfo()
  8.          print("mouse button 1 clicked",cursorItemType,cursorItemLink)
  9.       elseif button == "b2" then
  10.          print( "The mouse is over " .. GetMouseFocus():GetName() );
  11.          local cursorItemType, cursorItemID = GetCursorInfo()
  12.          print("mouse button 2 clicked",cursorItemType,cursorItemLink)
  13.       elseif button == "st" then
  14.          local cursorItemType, cursorItemID, cursorItemLink = GetCursorInfo()
  15.          print("Shift-T buttons clicked",cursorItemType,cursorItemLink)
  16.       end
  17. end)

Is there a way to get an itemID on a down press or under the cursor rather than having to pick up the item first? and/or is there a call such as GetItemDetailsUnderCursor()?

Also, I have had little luck finding a good tutorial on how to add keybindings to the binding UI so any help with pointing me to such an example/tutorial would be great.

Cheers

Xrystal 07-22-20 10:13 PM

You can cycle through any container, whether bags on your person, in the bank, the guildbank slots or even what you are wearing ( I think ).

This link should point you in the right direction .. browse around and look for sample code to try in your addon.

https://wow.gamepedia.com/World_of_Warcraft_API#Bags


This is specific to what you are asking..

https://wow.gamepedia.com/API_GetContainerItemID

https://wow.gamepedia.com/BagID

sezz 07-23-20 03:10 AM

Maybe something like this is what you're looking for?
It gets the itemLink from the item that is currently displayed on the tooltip and gets the itemID from the link.

Lua Code:
  1. local _, itemLink = GameTooltip:GetItem();
  2. if (itemLink) then
  3.     local itemID = tonumber(strmatch(itemLink, "item:(%d+):"));
  4. end

To add bindings to the default UI you can use bindings.xml:
https://wowwiki.fandom.com/wiki/Usin...for_your_addon

Walkerbo 07-26-20 11:06 PM

Hi all

Sorry for the late reply, RL got a bit hectic.

@Xrystal - Thanks for the links.
With the GetContainerItemID it requires the BagID and the BagSlot, I cannot see any function to get the itemID orBagID and BagSlot by hovering.

I did not know I could iterate through the dungeon guide however I would still hit the same issue as I need to get the itemID on hover.

@sezz
Your chunk would seem to be the answer that I am looking for however I have not been able to get it to work in game.

Based on the advice your both provided I have read up and mucked around to finally come up with the following;
Lua Code:
  1. local itemName, itemLink
  2. GameTooltip:HookScript('OnTooltipSetItem', function(self)
  3.     _, itemLink = self:GetItem()
  4.     print(itemLink)
  5. end)
This chunk spam prints out the itemLink on hover, however, I still have had no succes in capturing the itemLink for use.

I test all code chunks in game using the Rehack addon.

Any further help would be great.

Xrystal 07-27-20 03:51 AM

Quote:

This chunk spam prints out the itemLink on hover, however, I still have had no succes in capturing the itemLink for use.
Once you have the itemLink value .. you can use it with any function that accepts an itemLink.

Maybe if you explain what you want to use the link for.

Walkerbo 07-28-20 07:02 PM

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.

Walkerbo 08-02-20 07:33 PM

Hi actually

I have made some progress on this brick wall.

Now my toc looks like this;
Toc
Lua Code:
  1. ## Title: AAA |cffff0000 Testing Ground
  2. ## Interface: 80300
  3. ## Notes: |cffff0000 Testing Ground
  4. ## Version: AAA
  5. ## Date: Monday 3 August 2020
  6. ## SavedVariables: TableOfItems

AAA.lua
Lua
Lua Code:
  1. local itemName, itemLink, itemID
  2.  
  3. if not TableOfItems then
  4.     TableOfItems = {}
  5. end
  6.  
  7. local function addItemToTable(itemID)
  8.     print("add item function fired", itemID)
  9.     for k, v in pairs(TableOfItems) do
  10.         if v == itemID then
  11.             print("Item all ready listed")
  12.         else
  13.             table.insert(TableOfItems, 1, itemID)
  14.             print("Item added to list")
  15.         end
  16.     end
  17. end
  18.  
  19. SLASH_MYTEST1 = "/qwe"
  20. function SlashCmdList.MYTEST(msg, editbox)
  21.     print("slash command fired")
  22.     if #TableOfItems == 0 then
  23.         print("empty table")
  24.     else
  25.         for k, v in pairs(TableOfItems) do
  26.             print("item", k, v)
  27.         end
  28.     end
  29. end
  30.  
  31. local btn = CreateFrame("BUTTON", "myTest")
  32. SetBindingClick("F5", "myTest", "b1")
  33. SetBindingClick("BUTTON2", "myTest", "b2")
  34. SetBindingClick("G", "myTest", "b3")
  35. btn:SetScript("OnClick", function(self, button)
  36.     GameTooltip:HookScript("OnTooltipSetItem", function(self)
  37.             itemName, itemLink = self:GetItem()
  38.             itemID = tonumber(strmatch(itemLink, "item:(%d+):"))
  39.         end)
  40.     if button == "b1" then
  41.         addItemToTable(itemID)
  42.         print("F5 button clicked", itemLink, itemID)
  43.     elseif button == "b2" then
  44.         print("mouse button 2 clicked")
  45.     elseif button == "b3" then
  46.         print("G button clicked")
  47.     end
  48. end)
Now for some reason when I load in there are no errors showing on bugsack yet the button presses do nothing until I do a /reload.

Also adding the itemID to a table does not work; my prints show the slash command and add item functions are being fired yet no item is saved to the table.

I have probably made a noob mistake but I have not been able to nut this out.

Any further help would be great.

Rilgamon 08-02-20 10:43 PM

Delay your Setup after ADDON_LOADED or PLAYER_ENTERING_WORLD might help.

Fizzlemizz 08-02-20 11:33 PM

SavedVariables are global, TableOfItems is not really a unique enough name for a global. Same goes for frame names, "myTest" is not really a unique enough name for a global. You might be binding to some other "myTest" button.

Lua Code:
  1. local function addItemToTable(itemID)
  2.     print("add item function fired", itemID)
  3.     for k, v in pairs(TableOfItems) do
  4.         if v == itemID then
  5.             print("Item all ready listed")
  6.         else
  7.             table.insert(TableOfItems, 1, itemID)
  8.             print("Item added to list")
  9.         end
  10.     end
  11. end
This is possibly going to cause problems as you're inserting a new entry for each time there is no match. If you start with an empty table, there will also be no k, v to test against so no new entries will be added... ever.

Lua Code:
  1. local function addItemToTable(itemID)
  2.     if not TableOfItems then -- you could move this here if you don't want to create an event frame
  3.         TableOfItems = {}
  4.     end
  5.     print("add item function fired", itemID)
  6.     local found
  7.     for k, v in pairs(TableOfItems) do
  8.         if v == itemID then
  9.             print("Item all ready listed")
  10.             found = true
  11.             break
  12.         end
  13.     end
  14.     if not found then
  15.         table.insert(TableOfItems, 1, itemID)
  16.         print("Item added to list")
  17.     end
  18. end

Walkerbo 08-04-20 08:06 PM

Hi all

@Rilgamon, I didn't even think of that, I've been so focused on getting and using the hover to get the itemID that even the simplest of things passed over my head; thanks for reminding me :)

@Fizzlemizz, I have tried your code however I still have the exact same issues.
I have to /reload after entering world for the first time to get the button clicks to work, and, the list is still empty when I /qwe, I have also got the SavedVariables lua open so I can see if the itemIDs are saved on /reload just to ensure my /qwe was not working properly.
I repaced my orriginal addItemToTable function with your chunk and I also renamed my table, button and vars, (i went a bit overboard with the names).

Here is my current code;
Lua Code:
  1. local myTestingKeybindingItemName, myTestingKeybindingItemLink, myTestingKeybindingItemID
  2.  
  3.     if not myTestingKeybindingTableOfItems then
  4.         myTestingKeybindingTableOfItems = {}
  5.     end
  6.  
  7. local function addItemToTable(itemLink)
  8.     local found
  9.     for k, v in pairs(myTestingKeybindingTableOfItems) do
  10.         if v == itemLink then
  11.             print("Item all ready listed")
  12.             found = true
  13.             break
  14.         end
  15.     end
  16.     if not found then
  17.         table.insert(myTestingKeybindingTableOfItems, 1, itemLink)
  18.         print("Item added to list",itemLink)
  19.     end
  20. end
  21.  
  22. SLASH_MYTEST1 = "/qwe"
  23. function SlashCmdList.MYTEST(msg, editbox)
  24.     if #myTestingKeybindingTableOfItems == 0 then
  25.         print("empty table")
  26.     else
  27.         for k, v in pairs(myTestingKeybindingTableOfItems) do
  28.             print("item", k, v)
  29.         end
  30.     end
  31. end
  32.  
  33. local myTestingKeybindingButton = CreateFrame("BUTTON", "myTest")
  34. SetBindingClick("F5", "myTest", "b1")
  35. SetBindingClick("G", "myTest", "b3")
  36. myTestingKeybindingButton:SetScript("OnClick", function(self, button)
  37.     GameTooltip:HookScript("OnTooltipSetItem", function(self)
  38.         myTestItemName, myTestItemLink = self:GetItem()
  39.         myTestItemID = tonumber(strmatch(myTestItemLink, "item:(%d+):"))
  40.         end)
  41.     if button == "b1" then
  42.         addItemToTable(myTestItemLink)
  43.         print("F5 button clicked", myTestItemLink, myTestItemID)
  44.     elseif button == "b3" then
  45.         print("G button clicked")
  46.     end
  47. end)

I still have the issue that I have to /realod before the keybinds work.

How do I ensure that the keybinds are available when I first log in?

Fizzlemizz 08-04-20 08:25 PM

Lua Code:
  1. local myTestingKeybindingItemName, myTestingKeybindingItemLink, myTestingKeybindingItemID
  2.  
  3. local function addItemToTable(itemLink)
  4.     if not myTestingKeybindingTableOfItems then
  5.         myTestingKeybindingTableOfItems = {}
  6.     end
  7.     local found
  8.     for k, v in pairs(myTestingKeybindingTableOfItems) do
  9.         if v == itemLink then
  10.             print("Item all ready listed")
  11.             found = true
  12.             break
  13.         end
  14.     end
  15.     if not found then
  16.         table.insert(myTestingKeybindingTableOfItems, 1, itemLink)
  17.         print("Item added to list",itemLink)
  18.     end
  19. end
  20.  
  21. SLASH_MYTEST1 = "/qwe"
  22. function SlashCmdList.MYTEST(msg, editbox)
  23.     if #myTestingKeybindingTableOfItems == 0 then
  24.         print("empty table")
  25.     else
  26.         for k, v in pairs(myTestingKeybindingTableOfItems) do
  27.             print("item", k, v)
  28.         end
  29.     end
  30. end
  31.  
  32. local myTestingKeybindingButton = CreateFrame("BUTTON", "WalkerbomyTest")
  33. SetBindingClick("F5", "WalkerbomyTest", "b1")
  34. SetBindingClick("G", "WalkerbomyTest", "b3")
  35. myTestingKeybindingButton:SetScript("OnClick", function(self, button)
  36.     if button == "b1" then
  37.         addItemToTable(myTestingKeybindingItemLink)
  38.         print("F5 button clicked", myTestingKeybindingItemLink, myTestingKeybindingItemID)
  39.     elseif button == "b3" then
  40.         print("G button clicked")
  41.     end
  42. end)
  43.  
  44. GameTooltip:HookScript("OnTooltipSetItem", function(self)
  45.     myTestingKeybindingItemName, myTestingKeybindingItemLink = self:GetItem()
  46.     myTestingKeybindingItemID = tonumber(strmatch(myTestingKeybindingItemLink, "item:(%d+):"))
  47. end)

Walkerbo 08-05-20 08:47 PM

Hi Fizzlemizz

Thanks for your code, it is much more legible and easier to follow than mine.

However, it still has the same issue, I have to /reload after logging in to get the keybinds to work.

After much gnashing of teeth, a few print statements and a hell of a lot of experimentation I ended up solving the problem by creating the button itself in a function and then having another frame to run the new function on the player entering the world.

My full solution;
Lua Code:
  1. local myTestingKeybindingItemName, myTestingKeybindingItemLink, myTestingKeybindingItemID
  2.  
  3. local function addItemToTable(itemLink)
  4.     if not myTestingKeybindingTableOfItems then
  5.         myTestingKeybindingTableOfItems = {}
  6.     end
  7.     local found
  8.     for k, v in pairs(myTestingKeybindingTableOfItems) do
  9.         if v == itemLink then
  10.             print("Item all ready listed")
  11.             found = true
  12.             break
  13.         end
  14.     end
  15.     if not found then
  16.         table.insert(myTestingKeybindingTableOfItems, 1, itemLink)
  17.         print("Item added to list",itemLink)
  18.     end
  19. end
  20.  
  21. SLASH_MYTEST1 = "/qwe"
  22. function SlashCmdList.MYTEST(msg, editbox)
  23.     if #myTestingKeybindingTableOfItems == 0 then
  24.         print("empty table")
  25.     else
  26.         for k, v in pairs(myTestingKeybindingTableOfItems) do
  27.             print("item", k, v)
  28.         end
  29.     end
  30. end
  31.  
  32. local function keybindingButtonSetUp()
  33.     print("*** keybinding setup function fired ***")
  34.     local myTestingKeybindingButton = CreateFrame("BUTTON", "WalkerbomyTest")
  35.     SetBindingClick("F5", "WalkerbomyTest", "b1")
  36.     SetBindingClick("G", "WalkerbomyTest", "b3")
  37.     myTestingKeybindingButton:SetScript("OnClick", function(self, button)
  38.         if button == "b1" then
  39.             addItemToTable(myTestingKeybindingItemLink)
  40.             print("F5 button clicked", myTestingKeybindingItemLink, myTestingKeybindingItemID)
  41.         elseif button == "b3" then
  42.             print("G button clicked")
  43.         end
  44.     end)
  45. end
  46.  
  47. GameTooltip:HookScript("OnTooltipSetItem", function(self)
  48.     myTestingKeybindingItemName, myTestingKeybindingItemLink = self:GetItem()
  49.     myTestingKeybindingItemID = tonumber(strmatch(myTestingKeybindingItemLink, "item:(%d+):"))
  50.     print("item under mouse",myTestingKeybindingItemLink)
  51. end)
  52.  
  53. local myTestingKeybindingFrame = CreateFrame("FRAME", "WalkerboFrame")
  54. myTestingKeybindingFrame:SetScript('OnEvent', function(self, event, ...)
  55.     if event == 'PLAYER_LOGIN' then
  56.         print("*** Addon Loaded ***")
  57.         keybindingButtonSetUp()
  58.     end
  59. end)
  60. myTestingKeybindingFrame:RegisterEvent('PLAYER_LOGIN')

There is probaly a more efficent way of achieveing the same result but for now I am happy just to have it working.

The next thing on my list is to have the keybindings show in the bindings menu so users can swap the bindings to a key of their choice.
If I fail at that I will probably be back.

Thank you all for the help :)

Fizzlemizz 08-05-20 09:04 PM

I had no problem with the binding loging in from start-up, logout/login etc. Might I suggest you may have an addon binding the key(s) on a login event after your addons does and that event might not fire on /reload.

Just a thought.

F5 default is target Party Member 4.

Walkerbo 08-06-20 08:55 PM

Hi Fizzlemizz

No, I hadn't thought of that, it would make sense, however when you tested it at your end did you choose the same F5 key?

I have not been able to find an alternate F5 keybind in the bind UI.

Also a quick sideways, how do I bind the { or [ or \ keys in lua, in-game I can bind them in the binding UI but in lua I have no success?

Seerah 08-07-20 11:37 AM

Those are special characters in Lua. You need to escape them when using them in a string, or use a literal string (square brackets instead of quotes).

Lua Code:
  1. SetBindingClick("\\", "WalkerbomyTest", "b1")

or

Lua Code:
  1. SetBindingClick([[\]], "WalkerbomyTest", "b1")

Walkerbo 08-11-20 07:21 PM

Hi all

@Seerah thanks for the escape code help, I had tried it earlier however I had "\\-CTRL" which did not work as the escape also took in the -CTRL so it never worked, now that I have "CTRL-\\" it works properly.
I had never seen a literal string before, I will look into it more to understand what the difference is between a string and a literal string.

Once I have got this working I will post it here so other novices like myself can find it.

Thanks to you all for your help.

Walkerbo 08-20-20 07:22 PM

Hi all

As promised I am posting my working code here for other novices like myself.

Toc
Lua Code:
  1. ## Title: AAA |cffff0000 Testing Ground
  2. ## Interface: 80300
  3. ## Notes: |cffff0000 Testing Ground
  4. ## Version: AAA
  5. ## Date: Friday 21 August 2020
  6. ## SavedVariables: TableOfItems
Lua
Lua Code:
  1. local myTestingKeybindingItemName, myTestingKeybindingItemLink, myTestingKeybindingItemID
  2.  
  3. local function addItemToTable(itemLink)
  4.     if not myTestingKeybindingTableOfItems then
  5.         myTestingKeybindingTableOfItems = {}
  6.     end
  7.     local found
  8.     for k, v in pairs(myTestingKeybindingTableOfItems) do
  9.         if v == itemLink then
  10.             print('Item all ready listed')
  11.             found = true
  12.             break
  13.         end
  14.     end
  15.     if not found then
  16.         table.insert(myTestingKeybindingTableOfItems, 1, itemLink)
  17.         print('Item added to list', itemLink)
  18.     end
  19. end
  20.  
  21. local function keybindingButtonSetUp()
  22.     print('*** keybinding setup function fired ***')
  23.     local myTestingKeybindingButton = CreateFrame('BUTTON', 'WalkerbomyTest')
  24.     SetBindingClick('CTRL-\[', 'WalkerbomyTest', 'b1')
  25.     SetBindingClick('CTRL-\]', 'WalkerbomyTest', 'b2')
  26.     SetBindingClick('CTRL-\\', 'WalkerbomyTest', 'b3')
  27.     SetBindingClick('SHIFT-\[', 'WalkerbomyTest', 'b4')
  28.     SetBindingClick('SHIFT-\]', 'WalkerbomyTest', 'b5')
  29.     SetBindingClick('SHIFT-\\', 'WalkerbomyTest', 'b6')
  30.     myTestingKeybindingButton:SetScript(
  31.         'OnClick',
  32.         function(self, button)
  33.             if button == 'b1' then
  34.                 print('CTRL-\[ button clicked', myTestingKeybindingItemLink)
  35.                 addItemToTable(myTestingKeybindingItemLink)
  36.             elseif button == 'b2' then
  37.                 print('CTRL-\] button clicked', myTestingKeybindingItemLink)
  38.             elseif button == 'b3' then
  39.                 print('CTRL-\\ button clicked', myTestingKeybindingItemLink)
  40.             elseif button == 'b4' then
  41.                 print('SHIFT-\] button clicked', myTestingKeybindingItemLink)
  42.             elseif button == 'b5' then
  43.                 print('SHIFT-\\ button clicked', myTestingKeybindingItemLink)
  44.             elseif button == 'b6' then
  45.                 print('SHIFT-\] button clicked', myTestingKeybindingItemLink)
  46.             end
  47.         end
  48.     )
  49. end
  50.  
  51. SLASH_MYTEST1 = '/qwe'
  52. function SlashCmdList.MYTEST(msg, editbox)
  53.     if #myTestingKeybindingTableOfItems == 0 then
  54.         print('empty table')
  55.     else
  56.         for k, v in pairs(myTestingKeybindingTableOfItems) do
  57.             print('item', k, v)
  58.         end
  59.     end
  60. end
  61.  
  62. SLASH_MYTESTC1 = '/asd'
  63. function SlashCmdList.MYTESTC(msg, editbox)
  64.     myTestingKeybindingTableOfItems = {}
  65.     print('List cleared')
  66. end
  67.  
  68. GameTooltip:HookScript(
  69.     'OnTooltipSetItem',
  70.     function(self)
  71.         myTestingKeybindingItemName, myTestingKeybindingItemLink = self:GetItem()
  72.         myTestingKeybindingItemID = tonumber(strmatch(myTestingKeybindingItemLink, 'item:(%d+):'))
  73.     end
  74. )
  75.  
  76. local myTestingKeybindingFrame = CreateFrame('FRAME', 'WalkerboFrame')
  77. myTestingKeybindingFrame:SetScript(
  78.     'OnEvent',
  79.     function(self, event, ...)
  80.         if event == 'PLAYER_LOGIN' then
  81.             print('*** Addon Loaded ***')
  82.             keybindingButtonSetUp()
  83.         end
  84.     end
  85. )
  86. myTestingKeybindingFrame:RegisterEvent('PLAYER_LOGIN')

I will now start working out how to add these keybindings to the in-game keybinding UI.

Thanks to you all for your help, it is most appreciated.


All times are GMT -6. The time now is 07:01 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI