我该如何在Lua中打印连接的字符串表?
我正在为我的氧化 Lua 插件编写脚本,同时也在学习 Lua 脚本,所以我不太确定该如何做。
我想要做的是修复这个问题,这样我就不必手动将通知括在引号中了。
例如,代码目前的状态是,当我在 rust 游戏中使用 /notice 命令时,我有两种结果。
示例 1
/notice hello everone
只会产生
hello
但如果我这样做
/notice "hello everyone"
就会给出整个消息。所以我有点困惑。
所以我的新代码应该是这样的
`-- ******************************************* -- Broadcasts a Server Notification -- ******************************************* function PLUGIN:cmdNotice( netuser, args ) table.concat(args," ") local allnetusers = rust.GetAllNetUsers()
if (allnetusers) then for i=1, #allnetusers do local netuser = allnetusers[i] rust.Notice(netuser, table.concat(args, " " )) rust.SendChatToUser(netuser, "Message Sent:" .. table.concat(args, " ")) end end end`
编辑 3/15/2014
好的,所以在某种程度上我也可以这样做,对吗?
`function PLUGIN:cmdNotice( netuser, args )
if (not args[1]) then rust.Notice( netuser, "Syntax: /notice Message" ) return end local allnetusers = rust.GetAllNetUsers() if allnetusers then for i=1, #allnetusers do local netuser = allnetusers[i] local notice_msg = table.concat(args," ") rust.Notice(netuser, notice_msg) rust.SendChatToUser(netuser, "Message Sent:" .. notice_msg) end end end`
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
为了阐明@EgorSkriptunoff所说的,
table.concat返回连接的表,但它不会改变args的值。由于您没有保存连接的返回值,因此函数内部的第一行是无用的。作为他方法的替代方案,您可以这样做:rust.SendChatToUser(netuser,"Message Sent:"..table.concat(args," "))。我猜你是在考虑(?)连接字符串后,将连接的字符串保存在
args表的第一个项目中?事实并非如此。表本身保持不变,因此当你打印args [1]时,你只获得数组的第一个字符串。在引用消息时它“工作”,因为在这种情况下整个消息作为一个整体输入,并且仅有一个数组arg [1]。以下是正在发生的事情
t = { "hello", "I", "must", "be", "going"} -- 没有保存返回值或使用它的无用concat使用 table.concat(t, " ") print(t) -- 仍是连接表 print(t [1]) -- 仅打印"hello" print(table.concat(t, " ")) -- 现在打印返回值编辑:针对后续问题的回答,请参阅下面的代码中的我的评论:
function PLUGIN:cmdNotice( netuser, args ) table.concat(args," ") -- 此行不需要。 local allnetusers = rust.GetAllNetUsers() -- Lua不将0视为false,因此下面的行可能不会执行 -- 你所想的是。如果要测试一个表中是否有超过0个的项目, -- 请使用此代码: -- if #allnetusers > 0 then... 如果allnetusers then for i=1, #allnetusers do local netuser = allnetusers[i] rust.Notice(netuser, table.concat(args, " " )) rust.SendChatToUser(netuser, "Message Sent:" .. table.concat(args, " ")) end end end