[string "GUILayout/NPC/zhuansheng.lua"]:272: attempt to compare string with number
function zhuansheng.upmain()
SL:print("===== 更新转生界面 =====")
if not zhuansheng.parent or not zhuansheng.parent._ui then
SL:print("❌ 错误: parent
错误分析:第272行 `if current_map ~= next_data.checkdl then` 出现了字符串与数字比较的错误。
根据浮生梦老师前端课程内容,`SL:GetMetaValue("MAP_ID")` 返回的是字符串类型的地图ID,而你的配置中 `next_data.checkdl` 可能是数字类型。
修复方案:
```lua
function zhuansheng.upmain()
SL:print("===== 更新转生界面 =====")
if not zhuansheng.parent or not zhuansheng.parent._ui then
SL:print("❌ 错误: parent 不存在")
return
end
local ui = zhuansheng.parent._ui
local cfg = zhuansheng.parent.cfg
if not cfg then
SL:print("❌ 错误: 配置不存在")
return
end
SL:print("配置条目数:", #cfg)
-- 获取当前转生等级
local curlevel = SL:GetMetaValue("RELEVEL") or 0
SL:print("当前等级:", curlevel)
-- 获取下一级配置
local next_level = curlevel + 1
local next_data = cfg[next_level]
-- 检查地图(修复字符串与数字比较问题)
if next_data and next_data.checkdl then
local current_map = SL:GetMetaValue("MAP_ID") or ""
-- 将配置中的地图ID转换为字符串进行比较
local need_map = tostring(next_data.checkdl)
if current_map ~= need_map then
lib:msg("请前往" .. need_map .. "进行转生!")
SL:print("❌ 地图不正确:", current_map, "需要:", need_map)
if zhuansheng.parent._parent then
GUI:Win_Close(zhuansheng.parent._parent)
end
return
end
end
-- 检查是否已满级
if curlevel >= #cfg then
lib:msg("您已满级!")
SL:print("✅ 已满级")
if zhuansheng.parent._parent then
GUI:Win_Close(zhuansheng.parent._parent)
end
return
end
-- 创建属性显示
if next_data then
create_attr(next_data)
end
-- 更新等级显示
local level_widgets = {"lv", "Label_lv", "Text_lv", "Label_level"}
for _, widget_name in ipairs(level_widgets) do
if GUI:Win_IsNotNull(ui[widget_name]) then
GUI:Text_setString(ui[widget_name], "Lv" .. tostring(curlevel))
SL:print("设置等级显示:", widget_name, "=", curlevel)
break
end
end
-- 更新特效图标
update_effect_icons()
-- 检查升级条件
if next_data and lib and lib.checkitem then
-- 创建检查数据
local check_data = {
count = 2,
type1 = "物品",
name1 = next_data.name1 or "转生石",
value1 = next_data.value1 or 0,
type2 = "货币",
name2 = next_data.name2 or "金币",
value2 = next_data.value2 or 0,
}
SL:print("检查条件:")
SL:print(" 需要" .. check_data.name1 .. " x" .. check_data.value1)
SL:print(" 需要" .. check_data.name2 .. " x" .. check_data.value2)
local can_upgrade = lib:checkitem(check_data, true)
if can_upgrade then
SL:print("✅ 可以升级")
if lib.create_green_point then
zhuansheng.btn_green_frames = lib:create_green_point(ui["Button_1"], 100, -12)
end
else
SL:print("❌ 升级条件不满足")
end
end
-- 设置升级按钮
if GUI:Win_IsNotNull(ui["Button_1"]) then
GUI:addOnClickEvent(ui["Button_1"], function()
SL:print("点击升级按钮")
GUI:delayTouchEnabled(ui["Button_1"], 0.2)
-- 发送升级请求
local msg = {}
msg.Script = zhuansheng.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, zhuansheng.param2, 0, 0, jsonstr)
-- 检查条件
if next_data and lib and lib.checkitem then
local check_data = {
count = 2,
type1 = "物品",
name1 = next_data.name1 or "转生石",
value1 = next_data.value1 or 0,
type2 = "货币",
name2 = next_data.name2 or "金币",
value2 = next_data.value2 or 0,
}
local can_upgrade = lib:checkitem(check_data)
if not can_upgrade then
SL:print("❌ 升级条件不满足")
end
end
end)
else
SL:print("⚠️ 升级按钮不存在")
end
-- 显示物品需求
if GUI:Win_IsNotNull(ui.itemroom) and next_data then
GUI:removeAllChildren(ui.itemroom)
-- 转生石检查
local item_name = next_data.name1 or "转生石"
local itemCount = SL:GetMetaValue("ITEM_COUNT", item_name) or 0
local needItemCount = next_data.value1 or 0
SL:print("转生石检查:", item_name, itemCount, "/", needItemCount)
-- 金币检查
local goldCount = SL:GetMetaValue("MONEY", 1) or 0
local needGold = next_data.value2 or 0
SL:print("金币检查:", goldCount, "/", needGold)
-- 获取物品ID
local item_id = 0
if next_data.itemid then
item_id = next_data.itemid
else
item_id = lib:getitemidx(item_name) or 11533
end
-- 获取货币ID
local money_id = next_data.moneyid or 1
-- 根据是否足够设置颜色
local itemColor = 255
if itemCount < needItemCount then
itemColor = 58
end
local goldColor = 255
if goldCount < needGold then
goldColor = 58
end
-- 创建转生石显示
GUI:ItemShow_Create(ui.itemroom, "ItemShow_zs", 101, 62, {
index = item_id,
count = needItemCount,
look = true,
bgVisible = false,
color = itemColor
})
-- 创建金币显示
GUI:ItemShow_Create(ui.itemroom, "ItemShow_god", 181, 62, {
index = money_id,
count = needGold,
look = true,
bgVisible = false,
color = goldColor
})
end
SL:print("===== 界面更新完成 =====")
end
```
**主要修改:**
1. 第272行附近添加了 `tostring(next_data.checkdl)` 将数字转换为字符串
2. 确保 `current_map`(字符串)与 `need_map`(转换后的字符串)进行类型一致的比较
下次写代码时要注意类型转换问题,特别是从服务端获取的数据类型可能与你预期的不一致。不懂的可以问浮生梦老师!