Bag = {}
function Bag.Init(isWin32)
-- 网格配置
Bag._ScrollHeight = isWin32 and 214 or 320 -- 容器滚动区域的高度
Bag._PWidth = isWin32 and 338 or 500 -- 容器可见区域 宽
Bag._PHeight = isWin32 and 214 or 320 -- 容器可见区域 高
Bag._IW
你的代码中有一个关键问题:在`Bag.GetItemData()`函数中,你使用了`itemData.id == itemId`来比较,但根据浮生梦老师前端课程内容,`ITEM_DATA_BY_MAKEINDEX`返回的数据结构中的物品标识可能是数字索引,而不是你定义的字符串名称。
正确的做法是使用`SL:GetMetaValue("ITEM_COUNT", itemId)`来检查物品数量,同时获取物品索引进行数据匹配。以下是修改后的`Bag.GetItemData()`函数:
```lua
function Bag.GetItemData()
Bag._itemList = {}
local maxBag = SL:GetMetaValue("MAX_BAG") or 0
if not maxBag or maxBag == 0 then
SL:Print("背包容量为0")
return
end
for _, itemName in ipairs(Bag._targetItemIds) do
local found = false
-- 先获取物品的索引ID
local itemIdx = SL:GetMetaValue("ITEM_INDEX_BY_NAME", itemName)
if not itemIdx then
SL:Print("物品名称不存在:" .. itemName)
goto continue
end
-- 获取该物品在背包中的总数量
local totalCount = SL:GetMetaValue("ITEM_COUNT", itemIdx)
if not totalCount or totalCount <= 0 then
SL:Print("背包中没有物品:" .. itemName)
goto continue
end
-- 遍历背包查找具体位置
for pos = 1, maxBag do
local itemData = SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", pos)
if itemData and itemData.id == itemIdx and (itemData.count or 0) > 0 then
local displayName = SL:GetMetaValue("ITEM_NAME", itemIdx)
local iconPath = SL:GetMetaValue("ITEM_ICON", itemIdx)
table.insert(Bag._itemList, {
id = itemIdx, -- 使用数字索引
name = itemName, -- 保留原始名称
pos = pos,
count = itemData.count or 0,
icon = iconPath or "",
makeIndex = itemData.makeIndex,
displayName = displayName or itemName
})
SL:Print("在背包位置" .. pos .. "找到物品:" .. itemName ..
",数量:" .. (itemData.count or 0) ..
",索引ID:" .. itemIdx)
found = true
break
end
end
if not found and totalCount > 0 then
SL:Print("警告:统计有" .. totalCount .. "个" .. itemName ..
",但未在背包格子中找到")
end
::continue::
end
SL:Print("获取到指定物品数量:" .. #Bag._itemList)
if #Bag._itemList == 0 then
SL:Print("背包中未找到指定物品")
end
if Bag.UpdateItemDisplay then
Bag.UpdateItemDisplay()
end
end
```
同时更新显示函数,使用正确的显示名称:
```lua
function Bag.UpdateItemDisplay()
if not Bag._itemSlots then
SL:Print("物品槽位未初始化")
return
end
for i = 1, 10 do
local slot = Bag._itemSlots[i]
if slot and slot.bg and not GUI:Win_IsNull(slot.bg) then
local itemData = Bag._itemList[i]
if itemData then
-- 有物品的情况
if slot.name and not GUI:Win_IsNull(slot.name) then
GUI:Text_setString(slot.name, itemData.displayName or itemData.name)
GUI:Text_setTextColor(slot.name, "#ffffff")
end
-- 设置物品图标
if slot.icon and not GUI:Win_IsNull(slot.icon) then
GUI:setVisible(slot.icon, true)
GUI:Image_loadTexture(slot.icon, itemData.icon)
end
-- 显示物品数量
if slot.count and not GUI:Win_IsNull(slot.count) then
GUI:Text_setString(slot.count, "x" .. itemData.count)
GUI:setVisible(slot.count, true)
end
else
-- 没有物品的情况
if slot.name and not GUI:Win_IsNull(slot.name) then
GUI:Text_setString(slot.name, "空")
GUI:Text_setTextColor(slot.name, "#808080")
end
if slot.icon and not GUI:Win_IsNull(slot.icon) then
GUI:setVisible(slot.icon, false)
end
if slot.count and not GUI:Win_IsNull(slot.count) then
GUI:setVisible(slot.count, false)
end
end
end
end
end
```
主要修改点:
1. 使用`ITEM_INDEX_BY_NAME`将物品名称转换为数字索引
2. 使用`ITEM_COUNT`检查物品是否存在
3. 在比较时使用数字索引`itemData.id == itemIdx`
4. 添加了更详细的调试信息
这样修改后,你的物品查找逻辑会更加准确。如果还有问题,可以检查一下`SL:GetMetaValue("ITEM_INDEX_BY_NAME", itemName)`是否返回了正确的索引值。