我是lua后端老师!
小仙,你要统计身上所有装备的附加属性条数啊,这个需求很常见!让我来教你一步步实现。
首先,我们需要遍历身上所有装备位置,然后获取每个装备的附加属性,最后统计总条数。这里要用到 `getitemcustomabil` 函数来获取装备的附加属性信息。
```lua
-- 统计身上所有装备附加属性总条数
local function countAllEquipmentAttributes(actor)
-- 装备位置列表:1=武器,3=衣服,5=头盔,6=项链,7=手镯(左),8=手镯(右),9=戒指(左),10=戒指(右),11=腰带,12=鞋子,13=宝石,14=时装
local equipPositions = {1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
local totalCount = 0
for _, pos in ipairs(equipPositions) do
-- 获取指定位置的装备对象
local itemObj = linkbodyitem(actor, pos)
-- 如果该位置有装备
if isnotnull(itemObj) then
-- 获取装备的附加属性信息(JSON格式)
local attrJson = getitemcustomabil(actor, itemObj)
-- 解析JSON数据
if attrJson and attrJson ~= "" then
local attrTable = json2tbl(attrJson)
-- 检查是否有附加属性字段
if attrTable.abilex and attrTable.abilex ~= "" then
-- 分割属性字符串,统计条数
local count = 0
for _ in string.gmatch(attrTable.abilex, "([^,]+)") do
count = count + 1
end
totalCount = totalCount + count
release_print("位置" .. pos .. "有" .. count .. "条附加属性")
else
release_print("位置" .. pos .. "没有附加属性")
end
else
release_print("位置" .. pos .. "没有附加属性信息")
end
else
release_print("位置" .. pos .. "没有装备")
end
end
release_print("所有装备附加属性总条数:" .. totalCount)
return totalCount
end
-- 调用示例
local totalAttrs = countAllEquipmentAttributes(actor)
```
如果你想更精确地统计,还可以使用 `getitemattidvalue` 函数来检查每个属性ID是否有值:
```lua
-- 精确统计附加属性条数(排除值为0的属性)
local function countValidAttributes(actor)
local equipPositions = {1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
local totalCount = 0
for _, pos in ipairs(equipPositions) do
local itemObj = linkbodyitem(actor, pos)
if isnotnull(itemObj) then
local attrJson = getitemcustomabil(actor, itemObj)
if attrJson and attrJson ~= "" then
local attrTable = json2tbl(attrJson)
if attrTable.abilex and attrTable.abilex ~= "" then
-- 分割属性字符串,检查每个属性的值
for pair in string.gmatch(attrTable.abilex, "([^,]+)") do
local key, value = string.match(pair, "(%d+)=(%d+)")
if key and value then
local attrValue = tonumber(value)
if attrValue > 0 then -- 只统计值大于0的属性
totalCount = totalCount + 1
end
end
end
end
end
end
end
release_print("有效附加属性总条数:" .. totalCount)
return totalCount
end
-- 调用示例
local validCount = countValidAttributes(actor)
```
**代码说明:**
1. `linkbodyitem(actor, pos)` - 获取指定位置的装备对象
2. `getitemcustomabil(actor, itemObj)` - 获取装备的附加属性信息(JSON格式)
3. `json2tbl()` - 将JSON字符串转换为Lua表
4. `string.gmatch()` - 遍历属性字符串,统计条数
小仙,这两个函数你可以根据需求选择使用。第一个函数统计所有属性条数(包括值为0的),第二个函数只统计值大于0的有效属性。记得在实际使用时,要把这些函数放在合适的触发事件里调用哦!
如果还有不明白的地方,随时问我,或者去请教浮生梦尊师!