How to Write the Wall and Roof Functions to Build a One-Click House in Minecraft - For Dummies

You can make a Minecraft mod that builds a house. This process is known as building a one-click house, because whenever you’re in Minecraft with one-click capability, you can have a house. Once you have the first function written, you will need to add walls and a roof to your house.

Prepare to write the second function for your house

Before writing the second function, you have to make sure that the drone is in the correct location. At the end of the first function, the drone moves up and back to the beginning of the line. Then the drone needs to reset to make the second wall appear in the correct place.

Write this reset code in the main function.

image0.jpg

When you work with any of the code in this project, make sure you have already completed the badges in the Functions, Drones and Locations, and Introduction to Loops categories. You can always revisit a badge you have already earned for a refresher, or even ask questions on the LearnToMod online forums if you’re still having trouble.

Write the wall functions for your house

Write the second function.

image1.jpg

Continue to test and code until you have written all four functions to create the four walls of your house.

Sketch the roof function

The roof of your house is a pyramid, which is a bit tricky to make, so take out the paper and pencil for this one. You know that the base of the triangle should be 6 x 6 (so that it’s one unit bigger than the house). Here is one way to decompose the pyramid.

image2.jpg

It looks like it might be useful to have a function for each of the drawings. Place functions for the roof.

image3.jpg

Write the functions for the roof

The 6 x 6 function is shown below.

image4.jpg

It creates a square around the top of the house.

image5.jpg

Reset the drone and then write the 4 x 4 and 2 x 2 functions.

image6.jpg

You can see the complete mod at mod.learntomod.com/programs/sarah-Simple_House.

Refactor the roof functions

After you have a completed house, you can refactor your code roof to make it simpler to understand.

Before you refactor, copy the mod and create another version of it named Simple_House_Refactored. That way, if you accidentally introduce new bugs, you can always revert to the original, working version.

The only difference between the three functions shown here is the number of times the loops repeat.

image7.jpg

Rather than have three different functions, you can write one square function that takes a parameter named size.

A parameter is a kind of variable that can be used in a function to make it do something slightly different every time it’s called. For example, if you have a function named jump, you could add a parameter named how_many_times. Then every time you call the jump function, you specify how many times it should make the character jump. The function still does the same thing (makes a player jump), but the slight change is that it jumps a different number of times (depending on what you specify).

To make the square function, follow these steps:

  1. Bring in a new function, name it square, and click on the blue star in the upper left corner.

    image8.jpg
  2. Drag a new input into the square function.

    image9.jpg
  3. Rename the input to become size.

    image10.jpg
  4. Click on the blue star again to close the input dialog box.

    image11.jpg

    When you call the function, you specify the value for the parameter and then, throughout the function, the parameter (in this case, size) will have the specified value (in this case, 6).

    image12.jpg

This allows you to call the same function, with different parameters, giving you slightly different outcomes.

image13.jpg

Inside the square function, put the four loops that are in the 2 x 2, 4 x 4, and 6 x 6 functions. Rather than loop by a certain number, loop by the parameter size (found under variables). The image above shows how the square function should be written, and how you should call the square function from the main function.

You can replace the calls to the 4 x 4 and 2 x 2 functions with calls to the square function, just passing in a different value for the parameter size.

The final refactored code can be found at mod.learntomod.com/programs/sarah-Simple_House_Refactored.