In this lesson we will use similar code that we wrote in part 1 to make walls that have more interesting shapes using different blocks. Rather than write the code again this time we will ‘import’ code that I’ve already written for you to experiment with.

Step 1 – Start Code Builder by pressing ‘C’

If you see this screen select the first option.

Step 2: Click the Import button

Might also look like this:


Step 3: This step is tricky – read carefully!

Right-click the ‘Code’ button below and select ‘Copy link address’ in the pop-up menu. Then click the ‘Import URL…’ option in Minecraft (as pictured above). On iPads you tap and hold the button below and then select ‘Copy Link URL’ or ‘Copy’.

Code

Step 4: Paste the link address (URL) in the box

Click in the white text box (pictured above) and then press Ctrl-V (paste) to paste in the address from the code button in the previous step. You can also right click the text box and choose ‘Paste’. On iPads you tap the text box then select paste.


Step 5: Go ahead!

Click the green ‘Go ahead!’ button to complete the import process. It should look similar to the picture here (not identical). If it doesn’t – did you forget to copy or paste in the address?

Step 6 – Examine Code

If the import process worked then you should see something like this in your code builder:

Let’s start with the easy one – the ‘tpa’ chat command. This does only 1 thing – it teleports the agent to our location. We’ve done code like that before.

Now let’s look at the code on the right side. It’s inside a dark blue ‘function’ block. The function is called ‘buildWall’ and it builds a wall very similar to the code you wrote previously. Please do not change or remove it. It has a few import differences that make it a little more useful. When you use this function you can tell it how long and how tall you want the wall to be and it will create it. You cannot use this function from the chat command window. The correct word for using a function is ‘call’. You ‘call’ a function just like you might call someone to order a pizza. You tell them exactly what you want and they bring it to you.

Now let’s look at the ‘wall’ chat command on the left side.

This command is an example of how you can create complex walls by ‘calling’ the buildWall function many times to build walls with different lengths and heights. Each dark blue blue that says ‘call buildWall’ will build a different section of wall. The first number is the length of the wall and the second is the height. You will also notice that I turn the agent before building the 2nd and 3rd walls. Who says a wall has to be straight? You could build almost anything now.

Now it would be a pretty boring world if all your walls were made of the same block. It doesn’t have to be! We use the block ‘agent set active slot’ to change which inventory slot the agent uses to build. The agent has 27 inventory slots and in creative mode you never run out of blocks. In the ‘wall’ command I change the active slot to 1 to build the first wall, then to 2 to build the 2nd wall and finally to 3 to build the 3rd wall. You don’t have to change the slot each time you build a new wall. If you don’t change it the agent will keep using the last slot that you set, but it always starts at 1.

Step 7 – Set your agent’s inventory

Now it’s time to see what the ‘wall’ command does, but first we need to give our agent some blocks to work with. Use the ‘tpa’ command to teleport the agent to your location, then move some blocks to your inventory (press E to access your inventory) and finally right-click your agent and move blocks from your inventory into slots 1, 2 and 3 of your agent. Choose whatever blocks you like. Not all of them work, but you will have to experiment to find out which ones. I don’t think saplings or torches will work.

Step 8 – Run the ‘wall’ command

I hope you now how to do this by now. A shortcut I learned recently is that instead of pressing ‘T’ to enter the chat command you can also press [Enter] then the command and then [Enter] again. When you run the command you will see the agent build 3 sections of wall. Notice how after each section the agent positions itself on the ground at the end of the wall it just built ready to build an adjoining section.

Step 9 – Change the ‘wall’ command

Hopefully you can see how each code block in the ‘wall’ command affects the wall that is created. Now it’s time to experiment. First you might want to change the length or height of some walls, change the blocks or the turn directions.

Next you should try adding even more walls. To do this you can duplicate the existing function calls by right-clicking them in the code builder and selecting ‘Duplicate’. You can do this with any block you want to copy. It’s very convenient.

Alternatively you can drag another function call block from the code menu. First you click the ‘Advanced’ menu, then ‘Functions’ then you drag the buildWall block into your code.