Menu
CoddyTech

Recap - Command Parser

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 11 of 78.

challenge icon

Challenge

Easy

Write the command parser of a text adventure. The program reads one command per line, until the command quit. Clean each line first: remove the spaces at both ends, convert it to lowercase and split it into words at the spaces, leaving out empty pieces.

Answer each command with one line, using match on the array of words:

  • No words: Type a command.
  • help: Commands: go, take, inventory, quit
  • go and a direction from DIRECTIONS: You go DIRECTION.
  • go and any other word: You can't go WORD.
  • take and an item already in inventory: You already have the ITEM.
  • take and any other item: add it to inventory and print You take the ITEM.
  • inventory: Inventory: and the items separated by a comma and a space, or Inventory: empty
  • quit: Goodbye!, and the program stops reading
  • Anything else: Unknown command.

For the input below

help
Go North
go up
take lamp
inventory
quit

the output is:

Commands: go, take, inventory, quit
You go north.
You can't go up.
You take the lamp.
Inventory: lamp
Goodbye!

Try it yourself

extends Node

func _ready():
	const DIRECTIONS = ["north", "south", "east", "west"]
	var inventory: Array[String] = []
	# Read and answer commands until the player types quit

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler