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
根据浮生梦老师前端课程内容,你可以使用 `SL:GetMetaValue("BAG_DATA")` 来获取背包数据,然后遍历并打印。以下是修改后的 `Bag.GetItemData()` 函数:
```lua
function Bag.GetItemData()
Bag._itemList = {}
-- 使用 SL:GetMetaValue("BAG_DATA") 获取背包数据
local bagData = SL:GetMetaValue("BAG_DATA") or {}
if not bagData or type(bagData) ~= "table" then
SL:Print("背包数据为空或格式错误")
return
end
SL:Print("=== 开始打印背包数据表 ===")
-- 遍历背包数据表
for k, v in pairs(bagData) do
if type(v) == "table" then
SL:Print("物品索引:" .. k)
-- 打印物品详细信息
for key, value in pairs(v) do
SL:Print(" " .. key .. " = " .. tostring(value))
end
SL:Print("------------------------")
else
SL:Print(k .. " = " .. tostring(v))
end
end
SL:Print("=== 背包数据表打印结束 ===")
-- 继续原有的逻辑,查找特定物品
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
local itemIdx = SL:GetMetaValue("ITEM_INDEX_BY_NAME", itemName)
if not itemIdx then
SL:Print("物品名称不存在:" .. itemName)
else
local totalCount = SL:GetMetaValue("ITEM_COUNT", itemIdx)
if not totalCount or totalCount <= 0 then
SL:Print("背包中没有物品:" .. itemName)
else
-- 遍历背包查找具体位置
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
end
end
end
SL:Print("获取到指定物品数量:" .. #Bag._itemList)
if #Bag._itemList == 0 then
SL:Print("背包中未找到指定物品")
end
if Bag.UpdateItemDisplay then
Bag.UpdateItemDisplay()
end
end
```
这段代码会:
1. 使用 `SL:GetMetaValue("BAG_DATA")` 获取整个背包数据表
2. 遍历并打印背包数据表的所有内容
3. 继续原有的逻辑查找特定物品
4. 将找到的物品添加到 `Bag._itemList` 中供显示使用
这样你就能看到背包数据的完整结构,方便调试和了解物品数据的格式。