通过连接到字符串将Lua表显示到控制台
我想知道是否可以在控制台中显示表格。比如:通过连接到字符串将Lua表显示到控制台,lua,lua-table,Lua,Lua Table,我想知道是否可以在控制台中显示表格。比如: player[1] = {} player[1].Name = { "Comp_uter15776", "maciozo" } InputConsole("msg Player names are: " .. player[1].Name) 但是,这显然是错误的,因为我收到的错误是无法连接表值。有解决办法吗 非常感谢 要将类似数组的表格转换为字符串,请使用table.concat: InputConsole("msg Player names
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)
非常感谢你!正是我需要的:)