发送文字信息
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-21 18:00:00 |
👁️ 阅读: 1
在996引擎中发送文字信息,可以通过创建按钮并绑定发送功能来实现。根据浮生梦老师前端课程内容,以下是一个完整的示例:
```lua
-- 先创建父节点界面
local parent = GUI:Win_Create("ChatWindow", 0, 0, 800, 600, false, false, true, false, false, false, 1)
-- 创建输入框
local inputText = GUI:TextInput_Create(parent, "TextInput_msg", 100, 500, 400, 30)
GUI:TextInput_setPlaceHolder(inputText, "请输入消息...")
GUI:TextInput_setPlaceHolderColor(inputText, "#888888")
-- 创建发送按钮
local sendButton = GUI:Button_Create(parent, "Button_send", 520, 500, "res/public/1900000612.png")
GUI:Button_setTitleText(sendButton, "发送")
GUI:Button_setTitleColor(sendButton, "#ffffff")
GUI:Button_setTitleFontSize(sendButton, 16)
-- 创建显示区域
local listView = GUI:ListView_Create(parent, "ListView_msg", 100, 100, 600, 380, 1)
-- 发送按钮点击事件
GUI:addOnClickEvent(sendButton, function()
local msg = GUI:TextInput_getString(inputText)
if msg and string.len(msg) > 0 then
-- 1. 发送到聊天频道
SL:SendGMMsgToChat(msg)
-- 2. 在本地界面显示
local item = GUI:Layout_Create(listView, "msg_item", 0, 0, 600, 30, false)
local textLabel = GUI:Text_Create(item, "Text_msg", 10, 15, 16, "#ffffff", msg)
GUI:Text_setFontName(textLabel, "fonts/font5.ttf")
GUI:ListView_pushBackCustomItem(listView, item)
-- 3. 清空输入框
GUI:TextInput_setString(inputText, "")
-- 4. 打印日志
SL:Print("发送消息:", msg)
else
SL:Print("消息不能为空")
end
end)
-- 缓存命令历史(可选功能)
local function cacheCommand(msg)
local varName = "chat_cmd_history"
local history = {}
local jsonStr = SL:GetLocalString(varName)
if jsonStr and string.len(jsonStr) > 0 then
history = SL:JsonDecode(jsonStr) or {}
end
-- 限制历史记录数量
if #history >= 50 then
table.remove(history, 1)
end
table.insert(history, msg)
SL:SetLocalString(varName, SL:JsonEncode(history))
end
```
**功能说明:**
1. **创建界面组件**:
- 使用 `GUI:Win_Create` 创建父窗口
- `GUI:TextInput_Create` 创建文字输入框
- `GUI:Button_Create` 创建发送按钮
- `GUI:ListView_Create` 创建消息显示列表
2. **发送功能**:
- `GUI:TextInput_getString` 获取输入框内容
- `SL:SendGMMsgToChat` 发送消息到游戏聊天频道
- `GUI:ListView_pushBackCustomItem` 在本地界面显示消息
3. **相关函数参数**:
- `GUI:Button_Create(parent, id, x, y, image)` - 创建按钮
- `GUI:TextInput_Create(parent, id, x, y, width, height)` - 创建输入框
- `SL:SendGMMsgToChat(msg)` - 发送GM消息到聊天
- `GUI:TextInput_getString(widget)` - 获取输入框文本
- `GUI:TextInput_setString(widget, text)` - 设置输入框文本
**使用建议:**
- 消息发送后会自动清空输入框
- 可以添加回车键发送功能提升体验
- 历史记录功能可以帮助玩家快速发送常用消息
- 建议对消息长度做限制,避免超长消息
这样就能实现完整的文字信息发送功能了,有什么不明白的可以继续问浮生梦尊师!
← 返回问答库