我该如何在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`

点赞
用户26702
用户26702

为了阐明@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
2014-03-14 19:27:20