コンソールにテーブルを表示できるかどうか疑問に思っていました。何かのようなもの:
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, " "))
2 番目の引数は、各要素の間に配置される文字列です。デフォルトは""
です。
これで自分の生活を楽にするために...内側のテーブルの要素にも名前を付けることをお勧めします。これにより、ある目的のために意味のあるテーブル内の特定の値を取得する必要がある場合に、上記のコードが読みやすくなります。
-- 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)