3

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

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

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

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

非常感谢提前!

4

2 回答 2

4

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

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

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

于 2012-07-29T09:33:08.900 回答
1

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

-- 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)
于 2012-07-29T14:50:40.733 回答