SL:CustomAttrWidgetAdd 这个接口具体用法, 写个示例
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-11 15:50:56 |
👁️ 阅读: 1
SL:CustomAttrWidgetAdd 函数用于为脚本变量额外属性添加监听更新。当变量值变化时,关联的文本控件会自动更新显示。
**函数说明:**
```lua
SL:CustomAttrWidgetAdd(variableString, textWidget)
```
- **参数1 variableString**: 字符串,包含动态变量的文本(如 "&<TITEMCOUNT/1001>&")
- **参数2 textWidget**: 文本控件对象,用于显示变量值
**使用示例:**
```lua
-- 创建父界面
local parent = GUI:Win_Create("GMMain", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 创建文本控件显示属性值
local textWidget = GUI:Text_Create(parent, "Text_attValue", 100, 100, 16, "#ffffff", "0")
-- 添加脚本变量监听(如物品数量)
local variableString = "&<TITEMCOUNT/1001>&"
SL:CustomAttrWidgetAdd(variableString, textWidget)
-- 或者添加红点变量监听
local redVariable = "&<REDKEY/redpoint_key>&"
SL:CustomAttrWidgetAdd(redVariable, textWidget)
```
**实际应用场景(来自浮生梦老师前端课程):**
```lua
-- 在属性显示列表中动态更新属性值
local cell = PlayerBaseAtt.CreateAttri(list, listData)
if string.find(attList[2], "&<(.-)>&") then
-- 检测到动态变量格式,添加监听
SL:CustomAttrWidgetAdd(attList[2], cell.Text_attValue)
end
-- 红点变量更新示例
local newValue = string.format("&<REDKEY/%s>&", value)
local cell = PlayerBaseAtt.CreateAttri(list, listData)
SL:CustomAttrWidgetAdd(newValue, cell.Text_attValue)
```
**注意事项:**
1. 变量字符串必须包含 `&<` 和 `>&` 标记来标识动态变量
2. 支持 `TITEMCOUNT/物品ID`(物品数量)和 `REDKEY/红点键`(红点状态)两种格式
3. 文本控件初始值会被变量实际值替换
4. 当服务端变量变化时,关联的文本会自动更新
这样就能实现属性值的动态显示和自动更新功能了。
← 返回问答库