Handling User Input
Part of the Logic & Flow section of Coddy's Lua journey — lesson 28 of 54.
Challenge
EasyExtend the game loop to read and display user commands using io.read().
You are provided with the startingRoom, gardenRoom, and player variables from the previous challenge.
Modify the game loop to:
- Print the description of the player's current room
- Print the prompt
"> "(greater-than sign followed by a space) without a newline - Read the user's command using
io.read() - Print
"You entered: "followed by the command the user typed - Check if the command is
"quit"- if so, setgameRunningtofalseto exit the loop
The loop should continue until the user types "quit".
Expected Output Format:
You find yourself in a dusty library filled with ancient books and scrolls.
>
You entered: look
You find yourself in a dusty library filled with ancient books and scrolls.
>
You entered: east
You find yourself in a dusty library filled with ancient books and scrolls.
>
You entered: quitTry it yourself
-- Room definitions
local startingRoom = {
description = "You find yourself in a dusty library filled with ancient books and scrolls."
}
local gardenRoom = {
description = "You are in a beautiful garden with blooming flowers and a fountain."
}
-- Player definition
local player = {
currentRoom = startingRoom
}
-- Create game loop
local gameRunning = true
while gameRunning do
print(player.currentRoom.description)
gameRunning = false
endAll lessons in Logic & Flow
1Advanced Table Iteration
Iterating with pairs()Iterating with ipairs()pairs() vs. ipairs()Recap - Character Sheet2More Table Library Functions
table.concat()table construction & unpack()table.sort()Custom Sorting with FunctionsRecap - High Score Board5Project: Text Adventure Engine
Project Setup: The RoomLinking Rooms