Fox Studios logo Fox StudiosGame projects, devlogs and Scratch builds
Tutorials

How to Make an Inventory System in Scratch With Lists

The feedback thread for our last community jam had the same question come up eleven times: how do I let the player pick up a key and use it later? Most people had tried to do it with variables.

A laptop screen showing a colorful block coding editor with a small game character and a row of item slots at the bottom of the stage

The feedback thread for our last community jam had the same question come up eleven times: how do I let the player pick up a key and use it later? Most people had tried to do it with variables. One variable for the key, one for the sword, one for the potion, and by the time they reached the sixth item the code was a wall of if blocks that broke whenever they added something new.

Scratch has a better tool for this sitting right under the variable blocks, and plenty of people never touch it. Lists. An inventory in Scratch built on a list takes about twenty blocks, handles any number of item types, and lets you show, use and drop items without rewriting anything when you add a new one. This tutorial builds one from scratch, using the same setup that several jam entries ended up with after we worked through it together.

Why a list beats a pile of variables

A variable holds one value. A list holds as many as you want, in order, and Scratch gives you blocks to add, remove, find and count them. That matches what an inventory does. The player picks something up, it goes on the end. They use something, it comes off. You need to know whether they have the key, and how many slots are full.

With variables, every new item means a new variable and a new branch in every script that checks the inventory. With a list, a new item is just a new piece of text. The scripts that add, check and draw the inventory do not change at all. In the jam, one entry went from 140 blocks of inventory code to 31 after switching, and the bugs where the potion counted as a key disappeared.

Building the inventory step by step

Open a project with a player sprite and at least one item sprite on the stage. I will use a key, but anything works. Then follow these steps in order.

  1. Create the list. In the Variables category, click Make a List, name it inventory, and choose For all sprites. Every sprite needs to read it, so a local list will not work here.
  2. Clear it on start. In the stage, add when green flag clicked followed by delete all of inventory. Without this, last session's items stay in the list and the player starts with a key they never found.
  3. Pick up an item. In the key sprite, use when green flag clicked, then show, then wait until touching Player. After that, add key to inventory and hide. Use the exact same text every time you refer to this item. Key and key with a capital letter count as the same in Scratch comparisons, but a stray space does not.
  4. Check for an item. On the locked door, the check is one block: if inventory contains key then. Inside it, switch the door costume to open and let the player through.
  5. Use up an item. To remove the key once it is used, find its position with item # of key in inventory and pass that into delete (item # of key in inventory) of inventory. This removes only the first key, so if the player carries two, one remains.
  6. Set a size limit. Before the add block in each pickup script, wrap everything in if length of inventory < 6 then. Otherwise, say Bag is full for two seconds and leave the item where it is.

That is a working inventory. You can test it right away by showing the list monitor on the stage, the checkbox next to the list name. Seeing items appear and vanish is the fastest way to catch a typo in an item name.

Drawing the inventory on screen

The list monitor is fine for testing and looks awful in a finished game. For a proper bar, make one sprite called Slot with a costume for each item: an empty square, a key, a sword, a potion, named exactly like the text you add to the list. Then use clones.

When the game starts, the Slot sprite hides itself and creates six clones, one per slot, each with its own local variable called my slot set to 1 through 6. Each clone then runs a forever loop: if my slot is greater than the length of inventory, switch to the empty costume, otherwise switch to the costume named item (my slot) of inventory. Space them across the bottom by setting x to minus 150 plus my slot times 50.

Because the costume name matches the item text, adding a new item type later means drawing one costume and nothing else. If you have already built a menu screen, the same clone trick shows up in our title screen and menu tutorial, so the pattern may feel familiar.

The advice to store counts instead of copies is overrated

Older forum posts often say to keep two lists, one with item names and one with quantities, so the player with 14 coins has one entry instead of fourteen. In a big role playing project with dozens of stackable items, that makes sense. For most Scratch games our community makes, I think it is extra work that causes more bugs than it prevents.

Two parallel lists have to stay in sync forever. Delete from one and forget the other, and every item after that point shows the wrong count. In the jam, four of the six projects that tried parallel lists had exactly this bug at submission time. A single list with repeated entries is slower in theory, but Scratch lists hold up to 200,000 items and a game with a six slot bag will never notice. If you do need a coin total, keep coins as a normal variable, separate from the bag, the way you would handle the score in a score and high score setup.

Small fixes that save a lot of testing

A few habits from the jam feedback are worth stealing. Keep every item name lowercase and one word, like key, potion, gem. It removes a whole class of typo bugs. Put all item pickup logic in a custom block called pick up with an input for the item name, so the size check and the add live in one place. And if items should stay collected after the player dies, do not clear the list on respawn, only on the green flag.

One limitation to know about: Scratch cloud variables only store numbers, and there are no cloud lists. If you want an inventory that survives closing the browser, you need to encode it as digits, for example 1 for key and 2 for sword, then decode it on load. That is a project for another week, and honestly I would skip it for a first game.

If you get stuck, post your project link in the tutorials thread with the word inventory in the title. More beginner builds are collected in our tutorials section.

FAQ

How do I make an inventory in Scratch?

Create a list for all sprites, add item names to it when the player touches an item, and check it with the contains block when an item is needed.

How do I remove one item from a Scratch list?

Use delete with item # of the item name in your list. It removes the first matching entry and leaves any other copies alone.

Can Scratch save an inventory between sessions?

Not directly. Cloud variables hold only numbers, so you have to turn the list into a number code and turn it back when the game loads.

How many items can a Scratch list hold?

Up to 200,000 items. An inventory will never come close, so the real limit is how many slots you choose to draw on screen.

DF
Diego Fenn

Diego covers finished builds and the community around them, and runs the feedback threads that shape what the studio makes next.

More posts by Diego

More in Tutorials

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto