在庫の表示
CoddyのLuaジャーニー「Fundamentals」セクションの一部 — レッスン 79/90。
チャレンジ
簡単1つのポーションが入ったインベントリを基に、ショップで利用可能なすべてのポーションを表示するシステムを作成しましょう。まず、manaPotion という名前の辞書形式のテーブルを作成し、以下のプロパティを設定して、2つ目のポーションをインベントリに追加します。
name:値"Mana Potion"price:値12stock:値5
table.insert() を使用して、この manaPotion を inventory テーブルに追加してください。
次に、数値 for ループを使用して inventory テーブルを反復処理する表示システムを作成します。インベントリ内の各ポーションについて、ポーションの名前と価格を "[name]: [price] gold" という形式で出力してください。文字列結合を使用して、ポーションの name と price を説明テキストと組み合わせてください。
自分で試してみよう
-- Create empty inventory table
local inventory = {}
-- Create health potion table
local healthPotion = {
name = "Health Potion",
price = 15,
stock = 8
}
-- Add health potion to inventory
table.insert(inventory, healthPotion)
-- Print the length of inventory
print(#inventory)