获取仓库指定物品数量
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-03-10 18:21:16 |
👁️ 阅读: 84
我是lua后端老师!
小仙,要获取仓库中指定物品的数量,我们可以使用 `getstorageitems` 函数先获取仓库所有物品列表,然后遍历这个列表进行统计。
这里给你一个封装好的函数,可以直接使用:
```lua
--- 获取仓库指定物品数量
--- @param actor string 玩家对象
--- @param itemName string 物品名称
--- @param isBind integer 绑定状态:nil=忽略,1=非绑定,2=绑定
--- @return integer 物品数量
function GetStorageItemCount(actor, itemName, isBind)
-- 获取仓库所有物品
local items = getstorageitems(actor)
local count = 0
if items and #items > 0 then
for _, item in ipairs(items) do
-- 获取物品信息
local name = getiteminfo(item, "name")
-- 检查物品名称是否匹配
if name == itemName then
-- 如果指定了绑定状态,需要检查绑定状态
if isBind then
local bindState = getiteminfo(item, "bind")
if bindState == isBind then
count = count + 1
end
else
-- 不检查绑定状态,直接计数
count = count + 1
end
end
end
end
return count
end
```
**使用示例:**
```lua
-- 示例1:获取仓库中所有"金条"的数量(忽略绑定状态)
local goldCount = GetStorageItemCount(actor, "金条")
release_print("仓库金条数量:" .. goldCount)
-- 示例2:获取仓库中绑定的"疗伤药"数量
local bindPotionCount = GetStorageItemCount(actor, "疗伤药", 2)
release_print("仓库绑定疗伤药数量:" .. bindPotionCount)
-- 示例3:获取仓库中非绑定的"裁决之杖"数量
local unBindWeaponCount = GetStorageItemCount(actor, "裁决之杖", 1)
release_print("仓库非绑定裁决之杖数量:" .. unBindWeaponCount)
```
**函数说明:**
1. `getstorageitems(actor)` - 获取玩家仓库所有物品列表
2. `getiteminfo(item, "name")` - 获取物品的名称
3. `getiteminfo(item, "bind")` - 获取物品的绑定状态(0=未绑定,1=非绑定,2=绑定)
这个函数会遍历仓库中的所有物品,统计指定名称的物品数量,还可以根据需要筛选绑定状态。如果仓库中没有该物品,会返回0。
小仙,这样你就可以轻松获取仓库中指定物品的数量了!如果还有其他问题,随时问我哦~
← 返回问答库