How Do You Check Number Of Items Held In Lua?

Language: JP EN DE FR
New Items
2023-11-19
users online
Forum » Windower » General » How do you check number of items held in lua?
How do you check number of items held in lua?
Offline
Posts: 40
By Kainminter 2022-12-08 02:15:38
Link | Quote | Reply
 
Can anyone point me in the right direction on how to return either a count of how many of an item are in your inventory or otherwise a true/false check if it is in your inventory?

for example to do something along these lines;

--ITEM IDS:
--5870 Trump Card Case
--2974 Trump Card

if itemcount.inventory[2974] == 0 and itemcount.inventory[5870] >= 1 then
send_command('wait 0.9;input /item "Trump Card Case" <me>')
end
 Carbuncle.Arakon
Offline
Server: Carbuncle
Game: FFXI
user: arakon
Posts: 141
By Carbuncle.Arakon 2022-12-08 03:28:37
Link | Quote | Reply
 
You neet to iterate through all the inventory bags/storage, then if the id matches, then you add the number of items to the count

This is the code I use, I store the name and id of the items I want to track in nameIdMap.items and updateInventory iterates all the usage storage looking for the items and store them in gameinfo.inventory table.

When I need an item, I get it using get_item_count. Example of nameIdMap.items is

nameIdMap.items['shihei'] = 1179
Code
local function updateInventory()
    gameinfo.inventory = {}
    for name,id in pairs(nameIdMap.items) do
        gameinfo.inventory[id] = 0
    end
    local bags = S{0,8,10,11,12,13,14,15,16}
    for bag in bags:it() do
        if (windower.ffxi.get_bag_info(bag).enabled) then
            local inventory = windower.ffxi.get_items(bag) -- only items in inventory and wardrobe
            for index = 1, inventory.max do
                local item = inventory[index]
                if gameinfo.inventory[item.id] ~= nil then
                    gameinfo.inventory[item.id] = gameinfo.inventory[item.id] + item.count
                else
                    gameinfo.inventory[item.id] = item.count
                end
            end
        end
    end
    gameinfo.update.inventory = false
end

local function get_item_count(name)
    local nameLower = name:lower()
    local id = nameIdMap.items[nameLower]
    if not id then
        return 0
    end
    if not gameinfo.inventory[id] then
        return 0
    end
    return gameinfo.inventory[id]
end
[+]
 Asura.Chiaia
VIP
Offline
Server: Asura
Game: FFXI
user: Demmis
Posts: 1652
By Asura.Chiaia 2022-12-08 04:17:57
Link | Quote | Reply
 
Based on your example,, I'm guessing your wanting to use this inside GS. Which already has a way to handle this.

player.inventory

Table keyed with the name of equipment pieces. Keys for both short name and long name exist, if applicable. Value is a table containing containing item id, count, short name (shortname), and optionally long name (longname).
[+]
Offline
Posts: 40
By Kainminter 2022-12-08 04:29:04
Link | Quote | Reply
 
Thank you guys. That was very helpful and informative.
player.inventory ended up working well for what I needed, using something along these lines;

Code
CardCheck = player.inventory['Trump Card']
CaseCheck = player.inventory['Trump Card Case']

if CaseCheck and not CardCheck then
send_command('wait 1;input /item "Trump Card Case" <me>')
end
[+]
Log in to post.