我是lua后端老师!
小仙,看了你的代码,问题出在洗练逻辑中清理旧属性和添加新属性的环节。根据你的打印信息,`data.oldAttributes` 在洗练后变成了 `nil`,导致后续无法正确保存旧属性。
主要问题在于 `npc:refined` 函数中的这一段:
```lua
-- 保存旧的属性
if data.newAttributes then
data.oldAttributes = {}
for k, v in pairs(data.newAttributes) do
data.oldAttributes[k] = v
end
end
```
这里的问题是:当第一次洗练时,`data.newAttributes` 是 `nil`,所以 `data.oldAttributes` 没有被初始化。然后在后面的随机属性生成中,你尝试使用 `data.oldAttributes` 来获取锁定属性,但此时它是 `nil`。
修复方法是在函数开头就初始化 `data.oldAttributes`:
```lua
-- 设置洗练
function npc:refined(actor, param3, str)
local itemObj = linkbodyitem(actor, param3)
local itemOnlyID = getiteminfo(actor, itemObj, 1)
local data = npc:getRefineData(actor, param3)
-- 确保 oldAttributes 有值
if not data.oldAttributes then
data.oldAttributes = {}
end
-- 清理旧属性
if data.oldAttrStr and data.oldAttrStr ~= "" then
release_print("清除所有洗练属性: " .. data.oldAttrStr)
setaddnewabil(actor, -2, "-", data.oldAttrStr, itemObj)
refreshitem(actor, itemObj)
data.oldAttrStr = ""
else
release_print("旧属性为nil")
end
-- ... 其他代码保持不变 ...
-- 在随机属性生成部分,检查 oldAttributes 是否存在
for i = 1, entryCount do
if data.lockedAttrs[i] and data.lockedAttrs[i].locked == true and data.oldAttributes[i] then
-- 如果属性被锁定,使用旧的属性
attributeTbl[i] = data.oldAttributes[i]
release_print("属性"..i.."被锁定,保持原属性")
else
-- 随机生成新属性
-- ... 随机属性生成代码 ...
end
end
-- 保存新的属性
data.newAttributes = attributeTbl
data.oldAttributes = attributeTbl -- 这里正确保存为下次使用
-- ... 其他代码 ...
end
```
另外,在属性字符串生成部分,你使用了 `ipairs` 但属性表可能不是连续的数字索引。建议使用 `pairs`:
```lua
local attrStr = ""
for k, attr in pairs(attributeTbl) do
-- 血量
if attr.type == 1 and attr.hp_value > 0 then
attrStr = attrStr .. "3#1#"..attr.hp_value.."|"
end
-- 攻击力
if attr.type == 2 and attr.att_min > 0 and attr.att_max > 0 then
attrStr = attrStr .. "3#3#"..attr.att_min.."|3#4#"..attr.att_max.."|"
end
-- 防御力
if attr.type == 3 and attr.ac_min > 0 and attr.ac_max > 0 then
attrStr = attrStr .. "3#9#"..attr.ac_min.."|3#10#"..attr.ac_max.."|"
end
end
```
这样修改后,旧属性就能正确保存和覆盖了。如果还有问题,记得检查 `getRefineData` 函数是否每次都正确返回数据,以及锁定状态的解析是否正确。
小仙,写脚本要细心哦,特别是这种状态管理的地方。如果还有不懂的,可以问问浮生梦尊师!