function fashion_reinforcement(actor) --时装强化
local item = getboxitem(actor, 0) -- 获取OK框中的物品唯一id(为空返回"0")
local item_name = getiteminfo(actor, item, 7) -- 获取OK框中的物品名称
local new_item_name = getiteminfo(actor, item, 8
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-03-18 15:06:05 |
👁️ 阅读: 4
我是lua后端老师!
修仙者,你这个代码问题很典型啊!让我看看你的代码,装备属性丢失的原因在于你创建了一个**全新的属性表**,把原来的属性覆盖掉了。
你看这段代码:
```lua
-- 如果装备没有自定义属性,就创建一个空的属性表
if not current_tbl or not current_tbl["abil"] then
current_tbl = {
["abil"] = {
{
["i"] = 0,
["t"] = "基础属性:",
["c"] = 180,
["v"] = {}
}
}
}
end
```
这里的问题是:当装备**有属性**时,这个if条件不成立,但你还是执行了`current_tbl["name"] = item_name .. "+1"`。但`current_tbl`可能是一个**nil值**,或者结构不完整!
正确的做法应该是:
```lua
function fashion_reinforcement(actor) --时装强化
local item = getboxitem(actor, 0) -- 获取OK框中的物品唯一id(为空返回"0")
local item_name = getiteminfo(actor, item, 7) -- 获取OK框中的物品名称
if item == "0" then
sendcentermsg(actor, 180, 0, "请放入需要打造的时装", 0, 3)
return
end
-- 先获取装备当前的属性数据
local current_json = getitemjson(actor, item)
local current_tbl = json2tbl(current_json)
-- 如果current_tbl是nil,就创建一个新的表
if not current_tbl then
current_tbl = {}
end
-- 只修改名字部分,保留原有的属性数据
current_tbl["name"] = item_name .. "+1"
-- 如果原来没有abil属性,就创建一个空的
if not current_tbl["abil"] then
current_tbl["abil"] = {
{
["i"] = 0,
["t"] = "基础属性:",
["c"] = 180,
["v"] = {}
}
}
end
-- 将修改后的Lua表转换为JSON字符串并设置到物品上
setitemcustomabil(actor, item, tbl2json(current_tbl))
refreshitem(actor, item) -- 刷新物品信息到前端
updateboxitem(actor, 0) -- 更新OK框物品
sendcentermsg(actor, 180, 0, "时装强化成功", 0, 3)
end
```
关键修改点:
1. 先检查`current_tbl`是否为nil,如果是就创建空表
2. **先修改名字**,再处理属性结构
3. 只有确实没有`abil`属性时才创建新属性组
这样就能保证:
- 原来有属性的装备:只改名字,属性不变
- 原来没属性的装备:改名字并创建基础属性结构
下次写这种逻辑要注意判断顺序哦,不懂的可以问浮生梦尊师!
← 返回问答库