Stories and Interactive projects with Lists

lists for scratch dialogue

Stories in Scratch are filled with dialogue. In a simple story, the code can stretch down the page and across several code blocks.

In a previous lesson, we created a simple interactive presentation. The presentation included several lines of dialogue and question prompts. You can take a look at that project with the links below. I encourage you to work through that lesson first.

Alex’s Aquarium interactive presentation lesson

The presentation includes ten lines of dialogue and two question prompts. It doesn’t seem like many lines, but this is just the introduction.

dialogue code blocks

In this lesson, we are going to reproduce the presentation using Lists to organize the dialogue and question prompts.

Lists are used to store and organize information for programs. The information is anything we need to reference with our code. Lists are known as Arrays in the field of programming. Arrays do the same thing that lists do in Scratch. Lists in Scratch are an Array.

Lists reduce the length of our code in a variety of ways. Much of it depends on the product you are creating. The original presentation in the lesson uses ten lines of dialogue in the code. The presentation consists of one scene. You can imagine the number of dialogue lines needed in a full story.

The best way to understand lists is to dive right in and use them. Use the link below to download the resources for the project. The resources include a diver sprite and an underwater aquarium scene.

Download the sprite and backdrop

Go to https://scratch.mit.edu and create a new project. Set the title of the project to Alex’s Aquarium Introduction.

project name field

Download the project assets using the link above. Click the Add Sprite button and choose the upload Sprite option.

upload sprite option

Select the diver sprite and open it to begin the upload process.

sprite file selected

We don’t need the Scratch cat sprite. Select the cat Sprite and click the delete button.

cat sprite

Use the Backdrop menu to upload the underwater scene.

upload backdrop

Select the underwater scene image.

underwater scene image selected

This is our actor and scene.

diver and scene backdrop

Select the Code tab.

code tab

Go to the Variables section; click the Make a list button.

make a list button

Name the list dialogue; click the OK button.

list name

The list is placed on stage with our actor and backdrop. The list is empty and ready for our data.

list over stage

Click the Plus button to begin adding content to the list.

add to list button

Type Hello into the first line.

first list item

Press the Return key and type —Welcome to Alex’s Aquarium.

second list item

The second line stretches beyond the list width. Use the lower right corner to widen the list view.

list bottom right corner

There is more dialogue but don’t worry –I won’t expect you to write it all out. In Scratch, we have the option to upload a prepared list. The list must be in Plain Text format. I prepared the list for you. Use the link below to download the list.

Plain text lists can be created using any simple word processor. The list below was created in Google Docs. The link exports the document as a plain text file.

I would encourage you to have students write out the dialogue in a word processor and save it as a plain text file.

Download plain text file

The text file is a list. Each line of our dialogue is on a separate line. Open the text file and take a look.

Right-click on the list. There are options to import and export list information. Use the import option to get the text file from your computer.

import export options

The import process replaces the current list items with those imported.

imported list

The first six items are the introduction of the presentation. The next line is a prompt for the visitor to enter their name.

Go to the Events code section; place the When Green Flag clicked code onto the canvas.

green flag code

Attach a Repeat Until loop from the Control section.

repeat until loop

The purpose of the loop is to repeat the contents until we reach a condition. That condition, in our example, is the number of lines in the first part of our dialogue. The first part of the dialogue consists of six lines. The Sixth line prompts for the visitor's name. At this point, we need to break out of the loop and provide the visitor an opportunity to respond.

We need to count the number of lines. For that, we need a variable.

Go to the Variables section. A variable is created for us with each new Scratch project. We need to change the name.

variable

Right-click the variable name; select Rename variable.

rename variable option

Set the name to Counter.

variable name set

Place the –Set Variable To– code block between the Green Flag and the Repeat Until loop. This sets the counter at zero each time we begin the program. This is called initiating a variable. It is common practice in coding.

set counter code

Place the –Change Counter by 1– code inside the loop.

change counter code

Place the variable block on the canvas. Don’t place it in the code yet.

counter variable on canvas

Place the Equal Operator from the Operator Section inside the –Repeat Until Loop– parameter.

equal operator

Right-click the Counter variable; select duplicate.

duplicate option

Place the Counter variable inside the left side of the Equal Operator. Change the value on the right to 6.

value changed

Place a –Say…for…seconds– block in the loop and below the –change counter by– block.

say code block

Go to the Variable section. The list we created includes several code blocks. Find the block that reads –item 1 of dialogue.

item code block

Place the code block into the –Say code– parameter.

item code in parameter

Duplicate the Counter variable; place it inside the Item parameter.

counter variable in parameter

Find the List block and remove the checkmark. This hides the list box on the stage.

list checkbox

Click the green flag and watch our actor go through the dialogue.

green flag

Compare the number of lines used in this code to those I presented at the beginning of the lesson.

Code Explained

This is what we are doing with the code.

Each item in our list is represented by a number. The list begins with number 1. A typical array is indexed and begins with the number 0. Beginning with number 1 makes sense for young coders.

We have a variable called Counter. This counter is used to keep track of our count beginning at zero.

We have a loop that repeats a series of steps until we reach a specific counter value. That value is 6.

The loop contains a Say code block. The Say parameter text is replaced by the item number of our list. The item number is replaced by the counter variable.

The first time through the loop; the counter is incremented by one. The next line displays the contents of the first line of the dialogue list. This is done through the Say code. That line is Hello. It shows the text for 4 seconds and returns to the beginning of the loop.

The loop checks to see if the counter is equal to 6. The counter is now set to the value 1; the loop continues.

The counter is incremented by 1; the counter is now 2. The Say code shows the contents of the second line in the list.

The process repeats until the counter value is 6.

There is no code after the loop ends, so the program stops.

Updating the dialogue

With lists, we don't need to change the code to change the dialogue. Go to the Variables section; place a checkmark next to the dialogue list.

dialogue checkbox

Change the diver's name to anything you like –e.g., your name.

list item updated

Adding content to lists

Click on item 6; press the Return key. A new line is created to enter more information.

line 7 in list

Prompt the visitor to use the keyboard.

content added to list line 7

We need to update the code to include the extra line of dialogue. Change the count number in the loop from 6 to 7.

counter updated

Questions and answers

Lists are valuable for dialogue that includes questions and answers. Return to the Variables section. Click the –Make a List– button. It's useful to separate categories of lists with separate lists.

make a list button

Set the name to Q&A dialogue.

list name

Remove the checkmark from the dialogue list to hide it.

list checkbox

I have some response dialogue ready for the list. Use the link below to download the text file.

Download the text file

Right-click the Q&A list and select the import option. Select the Q&A dialogue text file for import.

import export options

The list contains four responses to questions we are going to ask.

list items

Let’s begin with the first question. Go to the Sensing code block section; place an –Ask…and…wait– block after the loop. Leave the prompt set at What is your name?

ask code

Attach a –Say…for– code block to the –Ask– code.

say block

Go to the Operators section. Find the Join block and place it inside the Say parameter.

join operator

Go to the Variables section. Place the -item…of…list– block into the left side of the Join operator. Item 1 of the dialogue list contains the word Hello. This is what we want.

item code block

Go back to the Sensing section. Find the Answer variable; place it on the right side of the Join code.

answer variable

The question prompting for the visitor’s name is not on the list Q&A list. Go to the Q&A dialogue list; click the Plus button to add an item.

list add button

Enter the question –What is your name?– into line five.

list item

Place the –item…of– block into the Ask code parameter.

item code block

Click the List selector; choose the Q&A dialogue list.

list selector

Enter the number 5 for the list item.

item variable

Go to the Variables section; hide the Q&A dialogue list. Click the Green Flag. Answer the question when prompted.

The words Hello and your name aren’t separated by a space. We will fix that next.

dialogue bubble

Pull out the Answer variable from the Join operator. Leave it on the canvas.

answer variable

Place a Join operator on the right side of the current Join operator. It seems strange to include two Join operators but this is one way to add space between elements.

join code

Place the Answer variable on the right side of the new Join operator. Erase the contents on the left side and press the space bar once to include a space.

answer variable in join parameter

Click the green flag to go through the program again. There should be a space between the word Hello and your name.

dialogue bubble

We have three lines of dialogue remaining. The lines are in the list from 8 to 10. This is another good use of the Repeat Until loop.

Right-click the Repeat Until loop; select the Duplicate option.

duplicate option

Attach the copy to the bottom of the code.

attaching duplicate code

The copied code includes the last two lines from the previous code. Leave it attached for now.

cod blocks

Change the Repeat Until counter from 7 to 9.

loop counter updated

The counter stopped at 7 in the previous loop. The second loop will continue from 7 and add 1. That takes us to the 8th item in the list. The dialogue will loop until it reaches the 9th item.

Questions

We are going to ask a question in the next part. The question is in the main dialogue list. It should be in the Q&A dialogue list.

Place a checkmark on the Q&A dialogue list to have it appear on stage. Add a new line to the list. Enter the question –Would you like to begin?

add list item button

Select line 10 in the dialogue list; click the X to delete the item.

delete button

Go to the Ask block and change the item number from 5 to 6.

ask item updated

We don’t need the last line of code. Drag the code to the Code panel to remove it.

removal of code block

Attach a Repeat Until loop to the code.

repeat until loop

Place an OR operator into the loop parameter.

OR operator

Insert Equal operators inside each of the OR operator parameters.

equal operators

Place the Answer variable into the left side of each Equal operator. Use Yes for the left equal operator and No for the right equal operator.

answer variables

This loop prompts the visitor if they want to take the tour. It will repeatedly ask the question until it receives a Yes or No. We went through this in the previous lesson. The link to that lesson is available at the top of this lesson.

Place an If…then…else code block inside the loop.

if…then…else code block

Place an If…then…else code block within the Else part of the previous condition code.

embedded code block

Place an Equal operator inside each of the If parameters. Place the Answer variable inside the left side of each Equal operator. Compare the answer to Yes in the first comparator and No for the second.

code blocks in parameters

Insert a Say block for each of the If…then…else options. Insert a list item for each parameter of the Say blocks. Select the Q&A dialogue list for each list item. Select items 1, 2, and 3 for each list item.

item code blocks

Add one more Say block after the first in the Else section. Query item 4 from the Q&A dialogue list.

attached say block

The question needs to be included within the loop. Detach the Ask code.

detached code blocks

You need to separate one first then the other.

ask code separated

Place the Ask code before the If…then…else condition. Connect the code blocks to the main code.

completed code

Run through the program to make sure everything is working properly. Lists provide lots of versatility. We will use lists more in the coming lessons.

Previous
Previous

Multiple language dialogue in Scratch projects

Next
Next

Interactive presentations with Scratch