Part II · Growing the Project

Chapter 5: Collections of Things

For the rest of this book, we'll assume your player found the right answer and made it through the door. (If they didn't, the game just ends, and that's a perfectly good ending too. Feel free to keep both paths in your own version.) Now that they're inside, they're going to find things. That means it's time for your game to hold more than one value at once.

Why Not Just Use More Variables?

Here's a question worth sitting with for a second: if your player finds a sword, a torch, and a rope, why not just do this?

QF Code example 1
var item1 = "sword"
var item2 = "torch"
var item3 = "rope"

You could. But what happens when they find a fourth item? A tenth? You'd be adding a new variable every time, and writing new code every time, just to handle one more thing. This is the exact wall I hit myself, years ago, first learning to program. Separate variables work fine until the number of things you're tracking stops being fixed. Once it isn't fixed, you need a different tool.

Making an Array

An array is a single variable that holds a list of values, in order.

QF Code example 2
var inventory = ["torch", "rope", "sword"]

That's one variable holding three things, not three variables. You can also make an empty one and fill it in later:

QF Code example 3
var inventory = array()

Reading and Changing Items by Index

Every item in an array has a position, called an index, starting at zero, not one.

QF Code example 4
writeln(inventory[0])   // torch, the first item
writeln(inventory[1])   // rope, the second item
writeln(inventory[2])   // sword, the third item

Starting at zero trips up almost everyone the first time. The first item is index 0, and the last item's index is always one less than however many items you have. You can change an item the same way:

QF Code example 5
inventory[0] = "lantern"

Growing and Shrinking

Three functions handle adding and removing items:

QF Code example 6
append(inventory, "map")           // adds "map" to the end
insert(inventory, 0, "key")        // inserts "key" at position 0
remove(inventory, 2)               // removes whatever is at position 2

And length() tells you how many items are currently in the array:

QF Code example 7
writeln("You are carrying " + length(inventory) + " items.")

The Moment This Clicks

Here's the anecdote from the introduction, made concrete. An array by itself is just a labeled shelf. What makes it powerful is pairing it with a loop, so you can do something to every item without writing a separate line for each one.

QF Code example 8
for each item in inventory
    writeln("- " + item)
end for

That one small block prints your entire inventory, however many items are in it, whether that's three or thirty. Change the array, and the loop still works, no edits required. That pairing, a collection plus a loop that walks through it, is the moment arrays stop being a curiosity and start being the reason you'd choose them over separate variables at all.

Growing the Adventure

Give your player somewhere to actually collect things.

QF Code example 9
// The Hollow Cottage
// Chapter 5

const cottageName = "The Hollow Cottage"

writeln("You are standing outside " + cottageName + ".")

var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ".")

var tries = 0
var solved = false

while tries < 3 and not solved
    tries = tries + 1
    var answer = input("There is a riddle carved into the door. It asks: what has keys but no locks? ")

    if answer == "piano" then
        writeln("The door swings open. You step inside.")
        solved = true
    else
        writeln("Nothing happens.")
    end if
end while

if not solved then
    writeln("The door refuses to budge. You'll have to find another way in.")
else
    writeln("The cottage is dusty but intact.")

    var inventory = array()
    append(inventory, "rusty key")
    append(inventory, "half-burned candle")

    writeln("On the floor, you notice:")
    for each item in inventory
        writeln("- " + item)
    end for

    writeln("You pick up both items.")
end if

Make It Yours. Add a third item that fits your version of the story, then remove one of the original two using remove(). Print the inventory again afterward so you can see the change take effect.

Chapter Checkpoint

Open either known-good checkpoint directly in QF Code, or download the .qfc file to keep locally. The IDE opens in a new tab and does not run the program automatically.

Starting code: the Chapter 4 checkpoint.

Completed code (Chapter 5):

QF Code example 10Open in QF Code
// The Hollow Cottage
// Chapter 5

const cottageName = "The Hollow Cottage"

writeln("You are standing outside " + cottageName + ".")

var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ".")

var tries = 0
var solved = false

while tries < 3 and not solved
    tries = tries + 1
    var answer = input("There is a riddle carved into the door. It asks: what has keys but no locks? ")

    if answer == "piano" then
        writeln("The door swings open. You step inside.")
        solved = true
    else
        writeln("Nothing happens.")
    end if
end while

if not solved then
    writeln("The door refuses to budge. You'll have to find another way in.")
else
    writeln("The cottage is dusty but intact.")

    var inventory = array()
    append(inventory, "rusty key")
    append(inventory, "half-burned candle")

    writeln("On the floor, you notice:")
    for each item in inventory
        writeln("- " + item)
    end for

    writeln("You pick up both items.")
end if

Next: your code is starting to sprawl a little. Chapter 6 shows you how to organize it into named, reusable pieces.


Copyright © 2026 Edison Mooers. Published by Gibidda Press. Free for personal learning and qualifying noncommercial educational use. Commercial training, organizational use, redistribution, and adaptation require written permission. Full use terms.