local jinengqianghua = {}
function jinengqianghua.link(actor, msgid, arg1, arg2, arg3, sMsg)
if not cfg_jinengqianghua or not next(cfg_jinengqianghua) then
return
end
local skillIndex = cfg_jinengqianghua.SkillIndex or 1
local targetLevel = cfg_jin
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-03-02 21:40:08 |
👁️ 阅读: 8
我是lua后端老师!
小仙,看了你的代码,我发现了一个关键问题。你代码中的 `targetLevel` 是从配置表 `cfg_jinengqianghua.SkillLevel` 获取的,这个值在每次执行时都是固定的(例如1),所以你每次强化目标都是1级,而不是在当前等级基础上+1。
你需要先获取玩家**当前**的技能等级,然后计算**下一级**的配置索引。我来帮你修改一下:
```lua
local jinengqianghua = {}
function jinengqianghua.link(actor, msgid, arg1, arg2, arg3, sMsg)
if not cfg_jinengqianghua or not next(cfg_jinengqianghua) then
return
end
local skillIndex = cfg_jinengqianghua.SkillIndex or 1
-- 先获取当前技能等级
local skillNameid = cfg_jinengqianghua[1].nameid -- 假设第一个配置就是技能ID
local currentLevel = getskillinfo(actor, skillNameid, 2) or 0
-- 验证等级
if currentLevel >= 9 then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>技能已达到最高等级!</font>","Type":9}')
return
end
-- 目标等级是当前等级+1
local targetLevel = currentLevel + 1
-- 计算配置表中的索引:技能索引*9 + 目标等级
local configIndex = (skillIndex - 1) * 9 + targetLevel
if configIndex < 1 or configIndex > 54 then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>无效的技能等级配置!</font>","Type":9}')
return
end
local config = cfg_jinengqianghua[configIndex]
if not config then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>技能配置不存在!</font>","Type":9}')
return
end
-- 检测金币
local needMoney = config.value2
local currentMoney = querymoney(actor, 1)
if currentMoney < needMoney then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>金币不足,需要' .. needMoney .. '金币!</font>","Type":9}')
return
end
-- 检测技能书页
local needItemCount = config.value1
local currentItemCount = getbagitemcount(actor, "技能书页") -- 技能书页物品ID
if currentItemCount < needItemCount then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>技能书页不足,需要' .. needItemCount .. '个!</font>","Type":9}')
return
end
-- 获取当前保底计数
local baoDiCount = getplaydef(actor, "U15") or 0
-- 消耗金币和材料
changemoney(actor, 1, "-", needMoney, "技能强化", true)
takeitem(actor, "技能书页", needItemCount)
-- 计算成功率
local successRate = config.cur_attr_10
local randomRoll = math.random(1, 100)
-- 保底机制:20次必成功
if baoDiCount >= 20 then
setskillinfo(actor, skillNameid, 2, targetLevel)
sendmsg(actor, 1, '{"Msg":"<font color=\'#00ff00\'>恭喜!保底成功,技能强化到' .. targetLevel .. '级!</font>","Type":9}')
setplaydef(actor, "U15", 0) -- 重置保底计数
-- 成功获得技能心得
local xindeCount = getplaydef(actor, "U16") or 0
setplaydef(actor, "U16", xindeCount + 3)
sendmsg(actor, 1, '{"Msg":"<font color=\'#00ff00\'>获得技能心得*3!当前拥有:' .. (xindeCount + 3) .. '个</font>","Type":9}')
return
end
-- 随机判定
if randomRoll <= successRate then
-- 强化成功
setskillinfo(actor, skillNameid, 2, targetLevel)
sendmsg(actor, 1, '{"Msg":"<font color=\'#00ff00\'>恭喜!技能强化成功,提升到' .. targetLevel .. '级!</font>","Type":9}')
-- 重置保底计数
setplaydef(actor, "U15", 0)
-- 成功获得技能心得
local xindeCount = getplaydef(actor, "U16") or 0
setplaydef(actor, "U16", xindeCount + 3)
sendmsg(actor, 1, '{"Msg":"<font color=\'#00ff00\'>获得技能心得*3!当前拥有:' .. (xindeCount + 3) .. '个</font>","Type":9}')
else
-- 强化失败
setplaydef(actor, "U15", baoDiCount + 1)
-- 失败获得技能心得
local xindeCount = getplaydef(actor, "U16") or 0
setplaydef(actor, "U16", xindeCount + 1)
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff9900\'>强化失败!保底计数:' .. (baoDiCount + 1) .. '</font>","Type":9}')
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff9900\'>获得技能心得*1!当前拥有:' .. (xindeCount + 1) .. '个</font>","Type":9}')
end
end
return jinengqianghua
```
**主要修改点:**
1. **动态计算目标等级**:不再使用配置表中的固定`SkillLevel`,而是先获取当前等级`currentLevel`,然后`targetLevel = currentLevel + 1`
2. **修正了获取技能ID的方式**:从配置表第一个配置中获取`skillNameid`
3. **移除了`lib:getvalueb`调用**:直接使用引擎的`getplaydef`函数
4. **统一了消息发送**:都使用`sendmsg`函数,确保消息能正常显示
5. **修复了字符串拼接错误**:你原来代码中有几处字符串连接有问题
这样修改后,每次强化都会基于当前等级计算下一级的需求,不会再卡在1级了。如果还有问题,可以检查一下你的配置表结构是否正确,特别是`cfg_jinengqianghua[1].nameid`是否能正确获取技能ID。
我是lua后端老师!
小仙,看了你的代码,我发现了一个关键问题。你代码中的 `targetLevel` 是从配置表 `cfg_jinengqianghua.SkillLevel` 获取的,这个值在每次执行时都是固定的(例如1),所以你每次强化目标都是1级,而不是在当前等级基础上+1。
你需要先获取玩家**当前**的技能等级,然后计算**下一级**的配置索引。我来帮你修改一下:
```lua
local jinengqianghua = {}
function jinengqianghua.link(actor, msgid, arg1, arg2, arg3, sMsg)
if not cfg_jinengqianghua or not next(cfg_jinengqianghua) then
return
end
local skillIndex = cfg_jinengqianghua.SkillIndex or 1
-- 先获取当前技能等级
local skillNameid = cfg_jinengqianghua[1].nameid -- 假设第一个配置就是技能ID
local currentLevel = getskillinfo(actor, skillNameid, 2) or 0
-- 验证等级
if currentLevel >= 9 then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>技能已达到最高等级!</font>","Type":9}')
return
end
-- 目标等级是当前等级+1
local targetLevel = currentLevel + 1
-- 计算配置表中的索引:技能索引*9 + 目标等级
local configIndex = (skillIndex - 1) * 9 + targetLevel
if configIndex < 1 or configIndex > 54 then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>无效的技能等级配置!</font>","Type":9}')
return
end
local config = cfg_jinengqianghua[configIndex]
if not config then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>技能配置不存在!</font>","Type":9}')
return
end
-- 检测金币
local needMoney = config.value2
local currentMoney = querymoney(actor, 1)
if currentMoney < needMoney then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>金币不足,需要' .. needMoney .. '金币!</font>","Type":9}')
return
end
-- 检测技能书页
local needItemCount = config.value1
local currentItemCount = getbagitemcount(actor, "技能书页") -- 技能书页物品ID
if currentItemCount < needItemCount then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>技能书页不足,需要' .. needItemCount .. '个!</font>","Type":9}')
return
end
-- 获取当前保底计数
local baoDiCount = getplaydef(actor, "U15") or 0
-- 消耗金币和材料
changemoney(actor, 1, "-", needMoney, "技能强化", true)
takeitem(actor, "技能书页", needItemCount)
-- 计算成功率
local successRate = config.cur_attr_10
local randomRoll = math.random(1, 100)
-- 保底机制:20次必成功
if baoDiCount >= 20 then
setskillinfo(actor, skillNameid, 2, targetLevel)
sendmsg(actor, 1, '{"Msg":"<font color=\'#00ff00\'>恭喜!保底成功,技能强化到' .. targetLevel .. '级!</font>","Type":9}')
setplaydef(actor, "U15", 0) -- 重置保底计数
-- 成功获得技能心得
local xindeCount = getplaydef(actor, "U16") or 0
setplaydef(actor, "U16", xindeCount + 3)
sendmsg(actor, 1, '{"Msg":"<font color=\'#00ff00\'>获得技能心得*3!当前拥有:' .. (xindeCount + 3) .. '个</font>","Type":9}')
return
end
-- 随机判定
if randomRoll <= successRate then
-- 强化成功
setskillinfo(actor, skillNameid, 2, targetLevel)
sendmsg(actor, 1, '{"Msg":"<font color=\'#00ff00\'>恭喜!技能强化成功,提升到' .. targetLevel .. '级!</font>","Type":9}')
-- 重置保底计数
setplaydef(actor, "U15", 0)
-- 成功获得技能心得
local xindeCount = getplaydef(actor, "U16") or 0
setplaydef(actor, "U16", xindeCount + 3)
sendmsg(actor, 1, '{"Msg":"<font color=\'#00ff00\'>获得技能心得*3!当前拥有:' .. (xindeCount + 3) .. '个</font>","Type":9}')
else
-- 强化失败
setplaydef(actor, "U15", baoDiCount + 1)
-- 失败获得技能心得
local xindeCount = getplaydef(actor, "U16") or 0
setplaydef(actor, "U16", xindeCount + 1)
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff9900\'>强化失败!保底计数:' .. (baoDiCount + 1) .. '</font>","Type":9}')
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff9900\'>获得技能心得*1!当前拥有:' .. (xindeCount + 1) .. '个</font>","Type":9}')
end
end
return jinengqianghua
```
**主要修改点:**
1. **动态计算目标等级**:不再使用配置表中的固定`SkillLevel`,而是先获取当前等级`currentLevel`,然后`targetLevel = currentLevel + 1`
2. **修正了获取技能ID的方式**:从配置表第一个配置中获取`skillNameid`
3. **移除了`lib:getvalueb`调用**:直接使用引擎的`getplaydef`函数
4. **统一了消息发送**:都使用`sendmsg`函数,确保消息能正常显示
5. **修复了字符串拼接错误**:你原来代码中有几处字符串连接有问题
这样修改后,每次强化都会基于当前等级计算下一级的需求,不会再卡在1级了。如果还有问题,可以检查一下你的配置表结构是否正确,特别是`cfg_jinengqianghua[1].nameid`是否能正确获取技能ID。
← 返回问答库