根据浮生梦老师前端课程内容,你的代码缺少了 `UpdateBaseAttri` 函数。这个函数用于更新角色基础属性界面,需要在 `PlayerBaseAtt.main()` 函数初始化后被调用。
以下是完整的 `PlayerBaseAtt.lua` 代码,包含了必要的 `UpdateBaseAttri` 函数:
```lua
PlayerBaseAtt = {} --角色面板 状态
PlayerBaseAtt._ui = nil
PlayerBaseAtt._baseAttributeStr = {
"职 业",
"等 级",
"当前经验",
"升级经验",
"真实充值",
"魂师等级",
"武 魂",
}
local function getAttValue(key)
local str = ""
if key == 1 then
str = SL:GetMetaValue("JOB_NAME") --职业名
elseif key == 2 then
local level = SL:GetMetaValue("LEVEL") --等级
local reinLv = SL:GetMetaValue("RELEVEL") --转生等级
if reinLv and reinLv > 0 then
str = string.format("%s转%s级", reinLv, level)
else
str = string.format("%s级", level)
end
elseif key == 3 then
str = SL:GetMetaValue("EXP") --经验值
elseif key == 4 then
str = SL:GetMetaValue("MAXEXP") --升级经验
elseif key == 5 then
str = lualib:getRecharge(true)
elseif key == 6 then
str = HunshiOBJ:getname()
elseif key == 7 then
str = LuckyOBJ:getwh()
end
return str
end
-- 头盔和斗笠分离出格子 需要对应相应装备位置
PlayerBaseAtt.realUIPos = {
[GUIDefine.EquipPosUI.Equip_Type_Super_Cap] = 1019,
[GUIDefine.EquipPosUI.Equip_Type_Super_Helmet] = 1021,
}
PlayerBaseAtt.fictionalUIPos = {
[1019] = GUIDefine.EquipPosUI.Equip_Type_Super_Cap,
[1021] = GUIDefine.EquipPosUI.Equip_Type_Super_Helmet,
}
function PlayerBaseAtt.main(data)
PlayerBaseAtt.posSetting = {
17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 43, 45 ,106,107,1021 ,1019,71
}
local parent = GUI:Attach_Parent()
local path = "player/player_base_attri_node_win32"
GUI:LoadExport(parent, path)
PlayerBaseAtt._ui = GUI:ui_delegate(parent)
if not PlayerBaseAtt._ui then
return false
end
PlayerBaseAtt._parent = parent
PlayerBaseAtt._hideNodePos = {}
local fashionSwitch = SL:GetMetaValue("GAME_DATA", "Fashionfx")
PlayerBaseAtt._show_naked_mold = fashionSwitch and tonumber(fashionSwitch) or 0 --是否显示裸模 0开启 1关闭
PlayerBaseAtt.playerSex = SL:GetMetaValue("SEX") --角色性别
PlayerBaseAtt.playerHairID = SL:GetMetaValue("HAIR") --发型
PlayerBaseAtt.playerJob = SL:GetMetaValue("JOB") --职业
--初始化装备槽
PlayerBaseAtt.InitEquipCells()
--初始化是否显示时装开关
PlayerBaseAtt.InitEquipSetting()
PlayerBaseAtt._index = 0 --添加的属性条编号
PlayerBaseAtt.UpdateBaseAttri()
return true
end
function PlayerBaseAtt.UpdateBaseAttri()
local list = PlayerBaseAtt._ui.ListView_base
GUI:removeAllChildren(list)
for k, v in ipairs(PlayerBaseAtt._baseAttributeStr) do
local listData = {
str = v,
strValue = getAttValue(k)
}
PlayerBaseAtt.CreateAttri(list, listData)
end
--脚本添加的额外属性
local variableInfo = SL:GetMetaValue("EX_ATTR")
local variableList = string.split(variableInfo, "|")
for i, v in ipairs(variableList) do
local attList = string.split(v, "#")
if attList[1] and attList[2] then
local listData = {
str = attList[1],
strValue = attList[2]
}
local cell = PlayerBaseAtt.CreateAttri(list, listData)
if string.find(attList[2], "&<(.-)>&") then
SL:CustomAttrWidgetAdd(attList[2], cell.Text_attValue) --脚本变量额外属性 添加监听更新
end
end
end
-- 红点变量 客户端game_data配置 m2需对应配置
local redValue = SL:GetMetaValue("GAME_DATA", "RedPointValue")
local data = string.split(redValue, "|")
for i, v in ipairs(data) do
local temp = string.split(v, "#")
local name = temp[1]
local value = temp[2]
if value and name then
local newValue = nil
if tonumber(value) then
newValue = string.format("&<TITEMCOUNT/%s>&", value)
else
newValue = string.format("&<REDKEY/%s>&", value)
end
local listData = {
str = name,
strValue = newValue
}
local cell = PlayerBaseAtt.CreateAttri(list, listData)
SL:CustomAttrWidgetAdd(newValue, cell.Text_attValue) -- 添加监听更新
end
end
end
function PlayerBaseAtt.CreateAttri(parent, data)
PlayerBaseAtt._index = PlayerBaseAtt._index + 1
-- 这里需要创建属性条的具体代码
-- 由于原代码不完整,这里只是函数声明
-- 实际实现需要根据你的界面布局来编写
return { Text_attValue = "text_widget" }
end
function PlayerBaseAtt.InitHideNodePos()
PlayerBaseAtt._hideNodePos = {}
for _, i in ipairs(PlayerBaseAtt.posSetting) do
if i ~= 17 and i ~= 18 and i ~= 45 and i ~= 21 then
if PlayerBaseAtt._ui[string.format("Node_%s", i)] then
local visible = GUI:getVisible(PlayerBaseAtt._ui[string.format("Node_%s", i)])
if not visible then
PlayerBaseAtt._hideNodePos[i] = true
end
end
end
end
end
function PlayerBaseAtt.InitEquipCells()
-- 服务器开关 时装是否开启首饰
local openFEquip = SL:GetMetaValue("SERVER_OPTION", SW_KEY_OPEN_F_EQUIP)
if openFEquip and openFEquip == 0 then
table.insert(PlayerBaseAtt.posSetting, 42)
table.insert(PlayerBaseAtt.posSetting, 44)
local newPosSetting = { 17, 18 }
for i, pos in ipairs(PlayerBaseAtt.posSetting) do
if not newPosSetting[pos] then
local equipPanel = PlayerBaseAtt._ui["Panel_pos" .. pos]
if equipPanel then
GUI:setVisible(equipPanel, false)
end
end
end
PlayerBaseAtt.posSetting = {}
PlayerBaseAtt.posSetting = newPosSetting
return
end
local equipPosSet = SL:GetMetaValue("SERVER_OPTION", SW_KEY_EQUIP_EXTRA_POS) or 0--额外的装备位置
local showExtra = equipPosSet == 1
if showExtra then
table.insert(PlayerBaseAtt.posSetting, 42)
table.insert(PlayerBaseAtt.posSetting, 44)
else
GUI:setVisible(PlayerBaseAtt._ui.Panel_pos44, false)
GUI:setVisible(PlayerBaseAtt._ui.Panel_pos42, false)
end
end
function PlayerBaseAtt.InitEquipSetting()
GUI:setVisible(PlayerBaseAtt._ui.Text_shizhuang, false)
GUI:setVisible(PlayerBaseAtt._ui.CheckBox_shizhuang, false)
GUI:CheckBox_addOnEvent(PlayerBaseAtt._ui.CheckBox_shizhuang, function()
SL:SetMetaValue("SUPEREQUIP_SHOW", GUI:CheckBox_isSelected(PlayerBaseAtt._ui.CheckBox_shizhuang))
SL:SendSuperEquipSetting(1) --通知服务器 时装显示开关 2 --设置显示神魔 1 --设置时装显示
end)
PlayerBaseAtt.UpdateSettingShow()
end
function PlayerBaseAtt.UpdateSettingShow()
local showSetting = SL:GetMetaValue("SUPEREQUIP_SHOW")
GUI:CheckBox_setSelected(PlayerBaseAtt._ui.CheckBox_shizhuang, showSetting)
end
--[[
创建装备回调
item 装备
返回 item
]]
function PlayerBaseAtt.CreateEquipItemCallBack(item)
return item
end
--[[
创建人物模型回调
]]
function PlayerBaseAtt.CreateModelCallBack(model)
return model
end
return PlayerBaseAtt
```
主要修改:
1. 添加了 `_baseAttributeStr` 表定义基础属性显示文本
2. 添加了 `getAttValue` 函数获取各个属性的值
3. 在 `main()` 函数末尾添加了 `PlayerBaseAtt._index = 0` 和 `PlayerBaseAtt.UpdateBaseAttri()` 调用
4. 实现了完整的 `UpdateBaseAttri()` 函数,包括:
- 清空列表:`GUI:removeAllChildren(list)`
- 遍历基础属性并创建显示项
- 处理脚本额外属性
- 处理红点变量
5. 添加了 `CreateAttri()` 函数声明(具体实现需要根据你的界面布局完善)
这样就不会出现 "attempt to call field 'UpdateBaseAttri' (a nil value)" 的错误了。