local function GetItemCountInBag(itemId)
local bagData = SL:GetValue("BAG_DATA")
if not bagData then return 0 end
local count = 0
for _, itemData in pairs(bagData) do
if itemData.Index == itemId then
count = count
根据你的需求,这里提供两个函数,分别用于获取指定道具ID的总数量和获取指定唯一ID(MakeIndex)的道具数量。
```lua
-- 获取背包中指定道具ID的总数量(所有堆叠数量累加)
local function GetItemCountByItemId(itemId)
local bagData = SL:GetValue("BAG_DATA")
if not bagData then return 0 end
local count = 0
for _, itemData in pairs(bagData) do
if itemData.Index == itemId then
count = count + (itemData.OverLap or 1)
end
end
return count
end
-- 获取背包中指定唯一ID(MakeIndex)的道具数量
local function GetItemCountByMakeIndex(makeIndex)
local bagData = SL:GetValue("BAG_DATA")
if not bagData then return 0 end
for _, itemData in pairs(bagData) do
if itemData.MakeIndex == makeIndex then
return itemData.OverLap or 1
end
end
return 0
end
```
第一个函数 `GetItemCountByItemId` 通过遍历背包数据,匹配 `Index` 字段(道具ID),累加所有匹配道具的 `OverLap` 字段(堆叠数量),返回该道具ID的总数量。
第二个函数 `GetItemCountByMakeIndex` 通过遍历背包数据,匹配 `MakeIndex` 字段(道具唯一ID),直接返回该唯一道具的 `OverLap` 字段(堆叠数量),如果未找到则返回0。