Scratch 12 · Build a game inventory with lists

A fox and treasure chest sit beside a three-row inventory of gems, hearts and stars.

Scratch lesson 12 of 20 · Advanced · About 35–45 minutes

Build an explorer’s bag that remembers which items it holds. Add a key and map, block duplicate pickups and safely remove the key when it is used.

What you’ll make An interactive inventory menu that can collect a Brass Key or Trail Map, use the key and count the items in the bag.

Get ready

Open the Scratch editor (opens in a new tab) and start a new project.

  • Start a new project. Pick a single sprite and name it Keeper. It will run the menu and speak for the game.
  • In Variables, choose Make a List. Name the list Inventory and choose For all sprites. Leave its checkbox ticked to display the list monitor on the Stage.
  • Make a variable called Choice. Its monitor can stay hidden. Choose a simple backdrop with space on one side of the Stage for the Inventory monitor.
  • Use the item names Brass Key and Trail Map throughout. Consistent names make the example easy to extend.

To keep your project, choose File → Save to your computer in Scratch.

Choose an item. Check the game inventory. → Change the list. Add or remove an item. → Show the result. Check for duplicates.
Project flow · Choose an item → Change the list → Show the result

Build it in Scratch

Choose the sprite named above each stack. The colours help you find the block categories. Indented rows sit inside the block above. Follow the end marker to see where each loop or condition closes.

1. Start with an empty bag

Put the Inventory monitor where you can see it without covering Keeper. The orange Variables category now contains list blocks. Choose delete all of [Inventory] to clear the starter list each time you click the green flag. When you add the menu in step 4, you will extend this same green-flag stack. Click the flag to test this first part. The monitor will report zero items, which is the starting state for each new adventure.

Block stack for Keeper
when green flag clicked
delete all of [Inventory]

2. Pack an object only once

Choose My Blocks and make a block with the label pack and one number or text input called object. In the definition, add an if/else with the Inventory contains reporter from the list blocks. Drag the oval object argument from the definition into the reporter’s search slot. If the bag already contains that text, Keeper explains what happened. The other branch adds the object and says its name by using the green Join reporter. Notice that this one definition works for both items because the calling block supplies the name.

Block stack for Keeper
define pack (object)
if <[Inventory] contains (object)?> then
say [That is already in the bag.] for (1) seconds
else
add (object) to [Inventory]
say (join [Packed ] (object)) for (1) seconds
↳ End of block

3. Find and remove the item by its position

Make a second block named use item with one number or text input called object. The first list row is item 1, but the key might move to item 2 if you pick up the map first. Use the orange item # of reporter inside the Delete block to find the actual position every time. Keep the Delete block inside the Contains branch. When Keeper cannot find the object, the other branch explains that the bag does not hold it. Try imagining what would happen if you always deleted item 1 instead. Which object might disappear?

Block stack for Keeper
define use item (object)
if <[Inventory] contains (object)?> then
delete (item # of (object) in [Inventory]) of [Inventory]
say (join [You used the ] (object)) for (1) seconds
else
say [You need to pick that up first.] for (1) seconds
↳ End of block

4. Let one menu run the whole bag

Add the Forever block to the bottom of your existing green-flag stack. The cyan Ask block waits for the reader to submit an answer. Store that answer in Choice. The following if/else blocks select one action. Fit each new if/else into the lower branch of the previous one until you reach the final error message. The bag count is the orange Length of Inventory reporter. It works when the list is empty as well as when it contains objects. Keep this as the only menu stack so there is only one question on screen.

Block stack for Keeper
forever
ask [1 Pack key | 2 Pack map | 3 Use key | 4 Count items] and wait
set [Choice] to (answer)
if <(Choice) = [1]> then
pack [Brass Key]
else
if <(Choice) = [2]> then
pack [Trail Map]
else
if <(Choice) = [3]> then
use item [Brass Key]
else
if <(Choice) = [4]> then
say (join [Items in your bag: ] (length of [Inventory])) for (2) seconds
else
say [Choose 1, 2, 3 or 4.] for (1) seconds
↳ End of block
↳ End of block
↳ End of block
↳ End of block
↳ End of block

Test your project

  1. Click the flag and type 4. Keeper should report zero items. Type 3. Keeper should ask you to pick up the missing key.
  2. Type 2, then 1, then 1 again. The list should contain Trail Map followed by Brass Key, with the duplicate blocked.
  3. Type 3. The Brass Key should disappear even though it was in position 2. Type 4 and check the count is one.
  4. Type 9 and check that only the help message appears. Then click the green flag. Your next adventure should begin with an empty list.

If something goes wrong

The map disappears when you use the key.
Use the item number reporter inside Delete. It should search for the object argument in Inventory rather than using a fixed list position.
The list is invisible on the Stage.
Tick the checkbox next to Inventory in Variables. Confirm you made a List with its own monitor.
Brass Key appears twice.
Check that Add is inside the else branch. The Contains test and Add must both use the same object argument.

Make it your own

  • Add a Torch as option 5. Reuse the pack block and test both its first pickup and its duplicate.
  • Make a door that responds only when the player really has a Brass Key. Reuse the Inventory contains check before trying to open it.

What you’ve learned Lists can store an ordered collection, while variables hold one value. Test whether a list contains an item before using its position to change the list.