WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Getting item position in bags (https://www.wowinterface.com/forums/showthread.php?t=46741)

Spawnova 06-28-13 03:17 PM

Getting item position in bags
 
How would I get the item position of an item in my bags, I need to check the cooldown of an item that will have it's position changed regularly

Seerah 06-28-13 03:21 PM

iirc, you'll need to scan through each bag slot until you find the item you're looking for.

ShadowProwler420 06-28-13 03:24 PM

Does this help at all?

http://www.wowwiki.com/API_UseContainerItem

Spawnova 06-28-13 03:32 PM

Yea, I looked at everything I could find through google but I'm not sure how I would scan through the bags yet

Clamsoda 06-28-13 03:54 PM

Lua Code:
  1. -- Hearthstone
  2. local itemID = 6948
  3.  
  4. for i = 0, NUM_BAG_SLOTS do
  5.     for z = 1, GetContainerNumSlots(i) do
  6.         if GetContainerItemID(i, z) == itemID then
  7.             local _, duration = GetContainerItemCooldown(i, z)
  8.             if duration == 0 then
  9.                 print ("Item is ready!")
  10.             else
  11.                 print ("Cooldown is "..duration)
  12.             end
  13.             break
  14.         end
  15.     end
  16. end

The code iterates over every container slot in every bag until it finds a match to the defined item ID. Once matched, the cooldown for the item is polled. If the cooldown returns 0, the item is off of cooldown; else the cooldown value is printed.

You'll need to adapt it to your needs; and I doubt the duration will return in a favorable format, likely seconds. You'll need to format it to hh:mm:ss etc.

Edit: I am not sure how to break out of nested loops in Lua, the code may be able to be optimized a bit if someone sheds some light on that aspect.

p3lim 06-28-13 04:24 PM

You're better off using itemlinks and a function to get the position, links are more accurate.
Lua Code:
  1. local function GetBagPosition(itemLink)
  2.     for bag = 0, NUM_BAG_SLOTS do
  3.         for slot = 1, GetContainerNumSlots(bag) do
  4.             if(GetContainerItemLink(bag, slot) == itemLink) then
  5.                 return bag, slot
  6.             end
  7.         end
  8.     end
  9. end
  10.  
  11. -- Get bag position of the Hearthstone
  12. local _, itemLink = GetItemInfo(6948)
  13. local bag, slot = GetBagPosition(itemLink)

Spawnova 06-28-13 05:17 PM

Thanks for the replies guys but I just literally found out that GetItemCooldown does exactly what I need =) Your code snippets are a lot of help aswell though!

SDPhantom 07-01-13 01:56 AM

Quote:

Originally Posted by Clamsoda (Post 280529)
Edit: I am not sure how to break out of nested loops in Lua, the code may be able to be optimized a bit if someone sheds some light on that aspect.

What you'd have to end up doing is set a variable in the inner loop for a conditional in the outer loop to check for and break out if needed. The following example has the dual purpose of getting the position and breaking out of both loops when it's found.

Lua Code:
  1. --  Example of an item link
  2. local _,link=GetItemInfo(6948);
  3.  
  4. local bag,slot;--   These are nil to start with
  5. for i=0,NUM_BAG_SLOTS do
  6.     for j=1,GetContainerNumSlots(i) do
  7.         if GetContainerItemLink(i,j)==link then
  8.             bag,slot=i,j;-- Set vars
  9.             break;--    Breaks out of inner loop
  10.         end
  11.     end
  12.  
  13. --  Break out of outer loop if vars have been set by inner loop
  14.     if bag and slot then break; end
  15. end

Clamsoda 07-01-13 02:42 AM

Oh, that's easy enough. Thanks for the insight SDPhantom!


All times are GMT -6. The time now is 10:08 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI