Menu
CoddyTech

Project: Inventory Item

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 67 of 80.

challenge icon

Challenge

Easy

Start the inventory with one generic item type.

  1. Above the items, write type Item<T> = {id: number, name: string, data: T}.
  2. Annotate them: local sword: Item<{damage: number}> and local potion: Item<{heal: number}>.
  3. Under -- Try it, add print(potion.data.heal).

Press Run Code: the output shows Iron Sword and 40.

Try it yourself

-- Add the Item<T> type here
local sword = {id = 1, name = "Iron Sword", data = {damage = 25}}
local potion = {id = 2, name = "Health Potion", data = {heal = 40}}
-- Try it
print(sword.name)

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler