Project: Inventory Item
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 63 of 73.
Challenge
EasyStart the inventory project by defining its core type and two items.
- Declare a generic type alias
Item<T>with the fieldsid: number,name: stringanddata: T. - Create
swordof typeItem<{damage: number}>withid = 1,name = "Iron Sword"anddata = {damage = 25}. - Create
potionof typeItem<{heal: number}>withid = 2,name = "Health Potion"anddata = {heal = 40}.
Then print, each on its own line:
sword.idsword.namesword.data.damagepotion.idpotion.namepotion.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
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions7Project: Typed Task Manager
Project: The Task ShapeFunction to Add a Task10Project: Generic Inventory
Project: Inventory ItemFunction: Add Items2Core Types
Basic Types: num, str, boolThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps