local npc = {}
EquipPos = {[0] = "衣服",[1] = "武器",[2] = "勋章",[3] = "头盔",[4] = "项链",[5] = "手镯(右)" ,
[6] = "手镯(左)",[7] = "戒指(右)",[8] = "戒指(左)",[10] = "腰带",[11] = "靴子"}
npc.Attribute = {
attName = {
[1] = "血量",
[3] = "攻击下限", [4] = "
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-03-16 19:27:06 |
👁️ 阅读: 9
我是lua后端老师!
小仙,你好!看了你的代码,我发现了一个关键问题。在`refined`函数中,你虽然正确地解析了锁定状态,但在生成新属性时,没有正确地从`data.oldAttributes`中读取被锁定的旧属性值。
主要问题在于:当属性被锁定时,你生成了`hp_attr`、`att_attr`、`ac_attr`这些变量,但后续的`attrStr_hp`、`attrStr_att`、`attrStr_ac`字符串构建时,使用的是这些变量的字段值,而不是直接使用`data.oldAttributes`中的值。
让我帮你修复`refined`函数中的关键部分:
```lua
-- 随机属性
local attrStr_hp = ""
local attrStr_att = ""
local attrStr_ac = ""
local hp_attr = nil
local att_attr = nil
local ac_attr = nil
-- 第1个复选框:血量
if lockStatus[1] == true and data.oldAttributes and data.oldAttributes[1] then
-- 如果血量被锁定且有旧属性,使用旧的血量
hp_attr = data.oldAttributes[1]
attrStr_hp = "3#1#"..hp_attr.hp_value -- 这里使用hp_attr.hp_value
release_print("血量被锁定,使用旧值: "..attrStr_hp)
else
-- 否则生成新的血量
local hp_value = math.random(100, 500)
-- 根据颜色规则确定血量颜色
local hp_color = "#FFFFFF"
if hp_value >= 100 and hp_value <= 199 then
hp_color = "#FFFFFF"
elseif hp_value >= 200 and hp_value <= 299 then
hp_color = "#00FF00"
elseif hp_value >= 300 and hp_value <= 500 then
hp_color = "#FF0000"
end
hp_attr = {
type = 1,
name = "血量",
hp_value = hp_value,
color = hp_color,
index = 1
}
attrStr_hp = "3#1#"..hp_value
release_print("血量未锁定,生成新值: "..attrStr_hp..", 颜色: "..hp_color)
end
-- 第2个复选框:攻击
if lockStatus[2] == true and data.oldAttributes and data.oldAttributes[2] then
-- 如果攻击被锁定且有旧属性,使用旧的攻击
att_attr = data.oldAttributes[2]
attrStr_att = "3#3#"..att_attr.att_min.."|3#4#"..att_attr.att_max -- 这里使用att_attr.att_min和att_attr.att_max
release_print("攻击被锁定,使用旧值: "..attrStr_att)
else
-- 否则生成新的攻击
local min_attack = math.random(0, 10)
local max_attack = math.random(5, 20)
-- 根据颜色规则确定攻击颜色
local min_color = "#FFFFFF"
if min_attack >= 0 and min_attack <= 4 then
min_color = "#FFFFFF"
elseif min_attack >= 5 and min_attack <= 8 then
min_color = "#00FF00"
elseif min_attack >= 9 and min_attack <= 10 then
min_color = "#FF0000"
end
local max_color = "#FFFFFF"
if max_attack >= 5 and max_attack <= 10 then
max_color = "#FFFFFF"
elseif max_attack >= 11 and max_attack <= 15 then
max_color = "#00FF00"
elseif max_attack >= 16 and max_attack <= 20 then
max_color = "#FF0000"
end
-- 确定主颜色
local main_color = "#FFFFFF"
local min_priority = 999
local max_priority = 999
if min_color == "#FFFFFF" then
min_priority = 1
elseif min_color == "#00FF00" then
min_priority = 2
elseif min_color == "#FF0000" then
min_priority = 3
end
if max_color == "#FFFFFF" then
max_priority = 1
elseif max_color == "#00FF00" then
max_priority = 2
elseif max_color == "#FF0000" then
max_priority = 3
end
if min_priority <= max_priority then
main_color = min_color
else
main_color = max_color
end
att_attr = {
type = 2,
name = "攻击力",
att_min = min_attack,
att_max = max_attack,
min_color = min_color,
max_color = max_color,
color = main_color,
index = 2
}
attrStr_att = "3#3#"..min_attack.."|3#4#"..max_attack
release_print("攻击未锁定,生成新值: "..attrStr_att..", 颜色: "..main_color)
end
-- 第3个复选框:防御
if lockStatus[3] == true and data.oldAttributes and data.oldAttributes[3] then
-- 如果防御被锁定且有旧属性,使用旧的防御
ac_attr = data.oldAttributes[3]
attrStr_ac = "3#9#"..ac_attr.ac_min.."|3#10#"..ac_attr.ac_max -- 这里使用ac_attr.ac_min和ac_attr.ac_max
release_print("防御被锁定,使用旧值: "..attrStr_ac)
else
-- 否则生成新的防御
local min_defense = math.random(1, 10)
local max_defense = math.random(5, 20)
-- 根据颜色规则确定防御颜色
local min_color = "#FFFFFF"
if min_defense >= 1 and min_defense <= 4 then
min_color = "#FFFFFF"
elseif min_defense >= 5 and min_defense <= 8 then
min_color = "#00FF00"
elseif min_defense >= 9 and min_defense <= 10 then
min_color = "#FF0000"
end
local max_color = "#FFFFFF"
if max_defense >= 5 and max_defense <= 10 then
max_color = "#FFFFFF"
elseif max_defense >= 11 and max_defense <= 15 then
max_color = "#00FF00"
elseif max_defense >= 16 and max_defense <= 20 then
max_color = "#FF0000"
end
-- 确定主颜色
local main_color = "#FFFFFF"
local min_priority = 999
local max_priority = 999
if min_color == "#FFFFFF" then
min_priority = 1
elseif min_color == "#00FF00" then
min_priority = 2
elseif min_color == "#FF0000" then
min_priority = 3
end
if max_color == "#FFFFFF" then
max_priority = 1
elseif max_color == "#00FF00" then
max_priority = 2
elseif max_color == "#FF0000" then
max_priority = 3
end
if min_priority <= max_priority then
main_color = min_color
else
main_color = max_color
end
ac_attr = {
type = 3,
name = "防御力",
ac_min = min_defense,
ac_max = max_defense,
min_color = min_color,
max_color = max_color,
color = main_color,
index = 3
}
attrStr_ac = "3#9#"..min_defense.."|3#10#"..max_defense
release_print("防御未锁定,生成新值: "..attrStr_ac..", 颜色: "..main_color)
end
```
另外,你还需要确保在第一次洗练时,`data.oldAttributes`能够正确保存。我注意到你在代码中已经做了这个,但为了保险起见,可以在`getRefineData`函数初始化时确保`oldAttributes`字段存在:
```lua
function npc:getRefineData(actor, param3)
-- ... 前面的代码 ...
if not npc.RefineData[player_ID][itemOnlyID] then
release_print("创建新的洗练数据对象,itemOnlyID: "..itemOnlyID)
npc.RefineData[player_ID][itemOnlyID] = {
itemOnlyID = itemOnlyID,
refineCount = 0,
oldAttributes = {}, -- 确保这个字段存在
oldAttrStr = ""
}
else
-- 确保oldAttributes字段存在
if not npc.RefineData[player_ID][itemOnlyID].oldAttributes then
npc.RefineData[player_ID][itemOnlyID].oldAttributes = {}
end
release_print("使用已存在的洗练数据,itemOnlyID: "..itemOnlyID..", refineCount: "..(npc.RefineData[player_ID][itemOnlyID].refineCount or 0))
end
return npc.RefineData[player_ID][itemOnlyID]
end
```
小仙,你现在的思路是对的,复选框和词条一一对应。主要问题是在属性字符串构建时,需要确保使用正确的旧属性值。修复后应该就能正常锁定了!
如果还有其他问题,随时问我哦!不懂的也可以请教浮生梦尊师!
← 返回问答库