Answer Item Queries
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 73 of 80.
Challenge
MediumAnswer a player who asks about one item.
- Delete the lines under
-- Try it. - Under it, read
local category = io.read()andlocal id: number = tonumber(io.read()) or 0. - For
"weapon", printdescribeWeapon(item)forfindItem(weapons, id); otherwise usepotionsanddescribePotion. - Print
Item not foundwhenfindItemreturnsnil.
Press Run Code: with the input weapon and 2, you see Weapon: Fire Axe (damage 40).
Try it yourself
-- The item shape every inventory shares
type Item<T> = {
id: number,
name: string,
data: T,
}
local sword: Item<{damage: number}> = {id = 1, name = "Iron Sword", data = {damage = 25}}
local potion: Item<{heal: number}> = {id = 2, name = "Health Potion", data = {heal = 40}}
-- Make an item with the next free id, store it, return it
local function addItem<T>(inventory: {Item<T>}, name: string, data: T): Item<T>
local item: Item<T> = {id = #inventory + 1, name = name, data = data}
table.insert(inventory, item)
return item
end
type Weapon = Item<{damage: number}>
type Potion = Item<{heal: number}>
local weapons: {Weapon} = {}
addItem(weapons, "Iron Sword", {damage = 25})
addItem(weapons, "Fire Axe", {damage = 40})
-- The item with this id, or nil
local function findItem<T>(inventory: {Item<T>}, id: number): Item<T>?
for _, item in ipairs(inventory) do
if item.id == id then
return item
end
end
return nil
end
local potions: {Potion} = {}
addItem(potions, "Health Potion", {heal = 40})
-- One description per item kind
local function describeWeapon(item: Weapon): string
return `Weapon: {item.name} (damage {item.data.damage})`
end
local function describePotion(item: Potion): string
return `Potion: {item.name} (heals {item.data.heal})`
end
-- Try it
print(describeWeapon(weapons[2]))
print(describePotion(potions[1]))
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 ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions7Project: Typed Task Manager
Project: The Task ShapeFunction to Add a Task10Project: Generic Inventory
Project: Inventory ItemFunction: Add Items2Core Types
Number, String and BooleanThe '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 TablesReading a GridBuilding a Gridtable.unpackRecap: Arrays and MapsPractice on your own: Online Luau compiler