如何在Lua中打印联接的字符串表?

raziel23x

好的,我正在为我的Oxide Lua插件编写脚本,并且我也只是在学习Lua脚本,所以我不确定如何做到这一点。

-- *******************************************
-- 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, args[1]))
      rust.SendChatToUser(netuser, "Message Sent:" .. args[1])
    end
  end
end

我正在尝试解决此问题,因此不必手动将通知放在“”中。

例如,如代码所示,如​​果我使用该/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
Telemachus

为了澄清@EgorSkriptunoff所说的内容,table.concat将返回联接的表,但不会更改的值args由于您不保存联接的返回值,因此函数内的第1行是无用的。作为他的替代方法,您可以这样做rust.SendChatToUser ( netuser, "Message Sent:" .. table.concat(args, " " )

我的猜测是您是否在考虑(?)联接的字符串将作为args表中的第一项保存在表中?那不是事实。该表本身保持不变,因此当您打印时args[1],您只会得到数组的第一个字符串。当您引用消息时,它“起作用”,因为在这种情况下,整个消息都作为一件事输入,并且数组只有一个arg[1]

这是怎么回事

t = { "hello", "I", "must", "be", "going"}

-- Useless use of concat since I don't save the return value or use it
table.concat(t, " ")

print(t) -- Still an unjoined table
print(t[1]) -- Prints only "hello"

print(table.concat(t, " ")) -- Now prints the return value

编辑:针对后续问题,请在下面的代码中查看我的评论:

function PLUGIN:cmdNotice( netuser, args )
  table.concat(args," ") -- This line is not needed.
  local allnetusers = rust.GetAllNetUsers()

  -- Lua doesn't count 0 as false, so the line below probably doesn't do
  -- what you think it does. If you want to test whether a table has more
  -- than 0 items in it, use this:
  -- if #allnetusers > 0 then...
  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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章