Menu
Coddy logo textTech

Project: Inventory Item

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 63 of 73.

challenge icon

Challenge

Easy

Start the inventory project by defining its core type and two items.

  • Declare a generic type alias Item<T> with the fields id: number, name: string and data: T.
  • Create sword of type Item<{damage: number}> with id = 1, name = "Iron Sword" and data = {damage = 25}.
  • Create potion of type Item<{heal: number}> with id = 2, name = "Health Potion" and data = {heal = 40}.

Then print, each on its own line:

  1. sword.id
  2. sword.name
  3. sword.data.damage
  4. potion.id
  5. potion.name
  6. potion.data.heal

Try it yourself

-- Write code here
-- 1) declare the generic type Item<T> with id, name and data
-- 2) create sword (Item<{damage: number}>) and potion (Item<{heal: number}>)
-- 3) print the six requested fields, one per line

All lessons in Introduction To Luau

Practice on your own: Online Lua compiler