繁体   English   中英

通过连接到字符串将Lua表显示到控制台

[英]Displaying Lua tables to console by concatenating to string

我想知道是否可以在控制台中显示表格。 就像是:

player[1] = {}
player[1].Name   = { "Comp_uter15776", "maciozo" }

InputConsole("msg Player names are: " .. player[1].Name)

但是,这显然是错误的,因为我收到有关它无法连接表值的错误。 这有解决方法吗?

非常感谢提前!

要将类似数组的表转换为字符串,请使用table.concat

InputConsole("msg Player names are: " .. table.concat(player[1].Name, " "))

第二个参数是放在每个元素之间的字符串; 它默认为""

为了让自己的生活变得更轻松......我建议在内部表格中命名元素。 这使得当您需要获取表中具有某些意义的特定值时,上述代码更容易阅读。

-- this will return a new instance of a 'player' table each time you call it.  
-- if you need to add or remove attributes, you only need to do it in one place.
function getPlayerTable()
    return {FirstName = "", LastName = ""}
end

local players = {}

local player = getPlayerTable()
player.FirstName = "Comp_uter15776"
player.LastName = "maciozo"
table.insert(players, player)

... more code to add players ...

local specific_player = players[1]
local specific_playerName = specific_player.FirstName.. 
                            " ".. specific_player.LastName
InputConsole("msg Some message ".. specific_playerName)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM