Introduction
15 minLast lesson ended with a bug waiting to happen.
Your wall needed the number 10 in two places — once in the inner repeat, and once in the move back that walks the Agent home. Change one and forget the other, and the wall comes out leaning like a staircase. Nothing warns you. Nothing goes red.
Today that stops being possible.
A variable is a number with a name
A variable is a box with a name on it, and a number inside.
You put a number in the box once, at the top of the program. Everywhere you would have typed the number, you use the name instead. The Agent looks in the box each time it needs the value.
So the number exists in one place. Change that one line and everything that uses the name changes with it — because there was only ever one number to begin with.
The second half: let the player fill the box
Here is the better trick. If the number lives in one place, why should that place be inside your code at all?
The on chat command block has a small (+) on it. Click it and the command grows a number you type in the chat box. Type wall 12 and the program receives 12. Type wall 30 and it receives 30.
You never open Code Builder again. The program asks; the player answers.
One idea, wearing two hats
That is why these two things are one lesson and not two:
| Who puts the number in the box? | |
|---|---|
| a variable you set | you, in the program, once |
| a chat number | the player, when they run it |
Both are a named box holding a number. The only difference is who fills it. In fact the chat number is a variable — it arrives called num1, and you can rename it to whatever you like.
Warm-up — the recipe
A recipe says serves 4 at the top, then lists amounts all the way down. To cook for eight, a good recipe makes you change one number at the top. A bad one makes you double thirty lines and hope you did not miss any.
Your wall last lesson was the bad recipe. Today you write the good one.
What you will be able to do
- Make a variable, give it a name, and put a number in it.
- Use that name anywhere a number used to be.
- Resize a whole build by editing one line.
- Add a number to a chat command so the player chooses the size without touching the code.
Basic
20 minFollow along on your own computer. Everyone should finish this part with the same working program.
Step 1 — Find the number that repeats
Open your wall program from Lesson 4:
Two tens. They are not two numbers that happen to match — they are the same number written twice, and that is the problem.
Step 2 — Make a variable
- Press
Cand open your project. - Click the Variables drawer — it is dark red.
- Click Make a Variable…, type
size, and click OK.
Three new blocks appear in the drawer, including set size to 0.
Step 3 — Fill the box once
Drag set size to 0 to the very top of your program, above the teleport, and change the 0 to 10.
It goes outside every loop, for the same reason the teleport does: setting it once is the whole point.
Step 4 — Use the name instead of the number
Now drag the rounded size block out of the Variables drawer and drop it on top of each 10 — once in the inner repeat, once in the move back.
Challenge 1
15 minA tower to order.
Your tower from Lesson 3 is always the same height. Make the player choose.
- Press
Cand start a new chat command calledtower. - Click the (+) on the command block and rename the number it gives you to
height. - Build a tower that is
heightblocks tall, using the name — not a number — wherever the height is needed. - Bring the Agent back to the ground at the end, whatever the height. You solved this exact problem in Lesson 3; the same solution still works.
- Go back to the world, press
T, and typetower 8. Thentower 20. Thentower 3.
It works if: typing tower followed by a number builds a tower of exactly that many blocks, three different numbers give three different towers, and there is no number typed into the program anywhere that decides the height.
Challenge 2
20 minA room to order.
Lesson 4's room was 5 by 5. Now the player decides.
- Press
Cand start a new chat command calledroom. - Click the (+) and rename the number to
side. - Build the hollow room from Lesson 4 — four walls, no roof, no floor — but 2 blocks tall this time, with
sidedeciding how long each wall is. - Think carefully about which loops should use
sideand which should keep a plain number. Not every number in the program is the same number. - Type
room 4, thenroom 6, thenroom 8.
Keep the number you type at 8 or less. Four walls of 8, two levels high, is 64 blocks — the whole slot. Ask for more and the room will stop halfway round, and now you know why.
It works if: three different numbers give three different hollow rooms, each one square with all four walls closed, and the height stays 2 whatever you type.
Challenge 3
25 minTwo numbers, one command.
One number was useful. Two is a building you can describe out loud.
- Press
Cand start a new chat command calledhut. - Click the (+) twice. You get two numbers. Rename them
sideandheight. - Build a hollow hut with four closed walls, where
sidedecides how long each wall is andheightdecides how many levels tall it is. - Before you test it, work out the largest
sideandheightyou can safely ask for, and write it down. Four walls,sidelong,heighthigh — and 64 blocks in the slot. - Type
hut 4 3. Then tryhut 6 2. Then try the biggest pair you worked out in step 4.
It works if: hut 4 3 and hut 6 2 build two visibly different huts, each with four closed walls, and you can say out loud which typed number controls which part of the building.
Extra challenge
spare timeThe growing spiral.
Every build so far has used the same number all the way through. What if the number grew as it went?
The Variables drawer holds a second block: change size by 1. It does not set the box to a new number — it adds to whatever is already in there.
- Press
Cand start a new chat command calledspiral. - Set a variable to
1at the start. - Build a shape of 10 sides, where the first side is 1 block long, the next is 2, the next is 3, and so on — turning the same way at every corner.
- The
changeblock goes inside the loop. That is the whole trick: the number is different on every lap. - Type
spiral, then fly up and look down at what you made.
Ten sides growing 1, 2, 3 … 10 is 55 blocks, so it fits in one slot. Eleven sides would not.
It works if: looking down from above you see a spiral that winds outward, with each arm visibly longer than the one before it.
Why this one matters
Until now a variable has been a better way to write a number down. Here it is something else entirely: a number that changes while the program runs.
Nothing you have built so far could make this shape. There is no number you could type into a repeat block that grows on its own — the box has to be opened and refilled on every lap.
That is the idea the rest of the section leans on, and it is worth ten minutes of staring at your spiral from above.