Building bigger things
Variables, loops inside loops, and functions, by building a glass platform and a hut with one command.
Placing one block is one block. Placing a hundred needs a loop, a way to count, and a way to not repeat yourself. This lesson builds all three.
Variables
A variable is a named box for a value. Make one with Make a variable in the Variables section, then use set to to put something in it and the variable's own block to read it back.
Variables start empty every time an event happens, and keep their value until that run of the script is over, waits included. They are perfect for counting inside one run. They cannot remember anything from one event to the next.
A glass platform
/platform puts a 5 by 5 glass floor under your feet. It walks along x from -2 to 2, and for each x it walks
along z from -2 to 2, placing glass at every spot:
Follow it through once. x starts at -2. The inner repeat runs five times with z going -2, -1, 0, 1, 2,
placing a row of five blocks. Then x goes up by one and the next row starts. Five rows of five are 25 blocks.
The -1 in offset by is one block below your feet, so you stand on the platform instead of inside it.
offset by moves the location itself
offset by does not make a new location. It moves the one you give it and hands it back. That is why the platform asks for location of player again for every block: each time it is a fresh copy, so the offsets never add up.
Sometimes adding up is exactly what you want. Keep one location in a variable and move it each time, and you get a staircase:
Functions
A hut needs a floor, four corner posts and a roof. The floor and the roof are both platforms, and the four posts are the same post in four places. Instead of copying blocks around, make functions: your own blocks that you define once and use as often as you like.
Make a function under My Blocks creates a define block with a name and parameters. Parameters are the slots of your new block: whatever is plugged in when it is used, the function can read by that name.
The command reads like a building plan: a plank floor, four posts, a glass roof. floor is the platform from
before with two slots added, how high up and which material. pillar stacks blocks upwards from a corner.
Type /hut and you are standing inside it.
Every run has a budget
A single run of a script can take up to 10,000 steps, where every block and every value counts as one. The hut takes around a thousand. If a script goes over, the server stops it and says so in its log. Very large builds are best split over several commands, or spread out with wait.
Try it yourself
- Give
floorasizeparameter so it can build any square, not only 5 by 5. - Add a door: after the posts, set two blocks in one wall back to air.
- Build a tower: a
pillarin a repeat, with the height going up each time.
Next, put all of this to work in a real minigame: TNT Run.