Thread Tools Display Modes
06-28-13, 03:17 PM   #1
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
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
  Reply With Quote
06-28-13, 03:21 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
iirc, you'll need to scan through each bag slot until you find the item you're looking for.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
06-28-13, 03:24 PM   #3
ShadowProwler420
A Flamescale Wyrmkin
 
ShadowProwler420's Avatar
Join Date: Feb 2008
Posts: 115
Does this help at all?

http://www.wowwiki.com/API_UseContainerItem
__________________

  Reply With Quote
06-28-13, 03:32 PM   #4
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
Yea, I looked at everything I could find through google but I'm not sure how I would scan through the bags yet
  Reply With Quote
06-28-13, 03:54 PM   #5
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
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.

Last edited by Clamsoda : 06-29-13 at 12:35 PM.
  Reply With Quote
06-28-13, 04:24 PM   #6
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
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)

Last edited by p3lim : 06-28-13 at 04:29 PM.
  Reply With Quote
06-28-13, 05:17 PM   #7
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
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!

Last edited by Spawnova : 06-30-13 at 02:40 PM.
  Reply With Quote
07-01-13, 01:56 AM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Clamsoda View Post
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
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
07-01-13, 02:42 AM   #9
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Oh, that's easy enough. Thanks for the insight SDPhantom!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Getting item position in bags


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off