Intermediate Scratch 3.0 Mastery
Data Lists and Arrays
From One to Many
So far, you've used variables to hold single pieces of information: a player's score, a sprite's speed, or a name. This works great for individual values. But what happens when you need to track a collection of similar items? Imagine a game where the player can collect items: a key, a potion, and a sword. You could create three separate variables: item1, item2, and item3. But this gets clumsy fast. What if the player can hold 50 items? You don't want to make 50 variables.
This is where lists come in. A list is like a variable that can hold multiple values at once, all organized in a numbered sequence. Think of it as a single container with many compartments. Instead of having separate variables for each item, you can have one list called Inventory that holds all of them. In many other programming languages, this same concept is called an array or a list.
Use a list whenever you need to store and manage a group of related items whose quantity might change.
Managing List Items
To get started, go to the 'Variables' category in the block palette and click 'Make a List'. Once you've named your list (e.g., Inventory), a new set of blocks will appear, giving you full control over its contents.
Let's break down the essential blocks for adding and removing items.
| Block | Purpose | Example Usage |
|---|---|---|
add [thing] to [list] | Adds an item to the very end of the list. | add [Sword] to [Inventory] |
insert [thing] at [1] of [list] | Adds an item at a specific numbered position, or index . | insert [Shield] at [2] of [Inventory] |
delete [1] of [list] | Removes the item at a specific position. | delete [3] of [Inventory] |
delete all of [list] | Empties the list completely. | delete all of [Inventory] |
replace item [1] of [list] with [thing] | Changes the value of an item at a specific position. | replace item [1] of [Inventory] with [Magic Sword] |
Using these blocks, you can dynamically manage collections of data. For example, when a player picks up an item, you use the add block. When they use a potion, you find its position and use the delete block.
Reading and Searching Lists
Storing data is only half the battle. You also need blocks to read information from your lists. These are called 'reporter' blocks because they report a value back to you. They are oval-shaped and can be dropped into the input slots of other blocks.
Here are the key reporter blocks and what they do:
| Block | Purpose | Returns |
|---|---|---|
item [1] of [list] | Gets the item at a certain position. | The value at that position (e.g., "Sword"). |
item # of [thing] in [list] | Finds the position of a specific item. | The position number (e.g., 2). Returns 0 if not found. |
length of [list] | Counts the total number of items in the list. | A number (e.g., 5). |
[list] contains [thing]? | Checks if an item exists anywhere in the list. | A true/false value. |
The contains block is especially useful. You can drop it into an if block to check for a condition. For instance: if <[Inventory] contains [key]?> then... This is a much cleaner way to check if a player has a specific item than checking five or ten different variables.
Application High Scores
Let's build a simple high score system to see lists in action. Instead of one variable for High Score, we can use a list to store the top 5 scores.
When a game ends, your script would:
- Use the
addblock to add the player's final score to aHigh Scoreslist. - This is a bit advanced, but you could then sort the list so the highest scores are at the top. Scratch doesn't have a built-in sort block, so you would need to write a script that loops through the list to reorder the items.
- Use
deleteto remove any scores beyond the 5th position, keeping your list tidy. - Use a loop with the
item ofblock to display the top 5 scores on the screen.
// When game ends...
add (final score) to [High Scores v]
// A full sorting script is complex, but the idea is to compare items
// and swap them until they are in order.
// Keep only the top 5
if <(length of [High Scores v]) > [5]> then
delete (6) of [High Scores v]
end
By using a list, you've created a much more engaging high score system that goes beyond a single number. This same logic can be applied to create dialogue systems for characters, crafting recipes, level sequences, and much more.
What is the primary advantage of using a list instead of multiple separate variables?
Imagine you have an Inventory list. Which block would you use to check if the player has collected a 'key' before opening a door?
Lists are a major step up in programming logic, allowing you to build projects that are more organized, scalable, and powerful.
