I had wanted to build a fence from the first day I saw MakeCode for Minecraft. Why? To hold all those raining chickens, of course, Due to one of my most endearing flaws, I couldn’t just build a simple, straight-forward fence. I set out to discover the easies, most understandable and fastest way to build a fence using MakeCode in the Minecraft world.
I explored fences using MakeCode fill, line, and place . I giggled as I watched that funny little agent struggle to build a fence. I compared and raced various fence algorithms against each other. All that exploration may be the subject of a future post, but this post is about the winner (so far) — a fence using the MakeCode builder.
Although I started with a simple fence with a fixed size and material, I wasn’t content with that. I wanted a “utility” fence that I could use in various programs. To that end, I added an argument (parameter) so that the fence could be any size, although it is limited to being square. I also added an argument to specify the material (e.g., wood, stone) from which the fence is constructed. Two fences are shown below.
How Builder Fence Works
The on chat command that launches Builder Fence has two arguments. num1 is the number of blocks per side. num2 is the index of the block to be used to build the fence. Notice that nBlocksPerSide and fenceBlockIndex are global variables, which are set to the respective values of the arguments. The arguments num1 and num2 should not be used as global variable unless you want to make yourself really crazy. The global variables are required because in MakeCode blocks, functions cannot have arguments — too bad, but maybe someday …
The two global variables are not validated here because they are validated in the buildFence function. After the global variables are set, the function setOrigin is called before the function builderFence is called.
The array fenceChoices contains blocks that can be chosen for the fence. The on chat argument num2, which is stored in global variable fenceBlockIndex may have values 0, 1 or 2 specifying the wood, stone or sunflower fence, respectively. More choices can be added to this array, and doing so does not require any other change to the builderFence code.
The function builderFence (below) does the magic. It expects two global variables to have been set prior to calling it: nBlocksPerSide and fenceBlockIndex. The function validateBuilderFenceParams is called to make sure the two global variables are within the allowed values, and, if not, are set to default values. builderFence then calls makeEastPointerFromOrigin and setOrigin, which sets origin if it has not been already set. After this initialization, the real fence-building action starts.
The main part of builderFence starts when the builder teleports to origin. By facing the builder East and doing right turns, the fence will be to the east-south of origin (the positive X and Z quadrant). This knowledge may be important to a program that calls builderFence.
The mark placed by the builder just before the repeat block, is be the starting place of a path that will be traced. During each of the four passes through the repat loop, the builder moves forward one block fewer than nBlocksPerSide then turns right. One fewer blocks is required because the builder is standing on one of the blocks before it moves making the total blocks on each side be the correct number. Only after the builder has traveled the entire path (after the loop), the builder does the trace path with the block from the array fenceChoices designated by fenceBlockIndex
An obvious upgrade to the fence described here, is to enable the fence to have a different width and length rather than be square. Also, other fence material options would be good. I have also experimented with fences that are more than one block high.
Get the Code
Builder Fence code is shared on Code Connection at this URL
To get and use the code, follow these steps.
Click the Import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Click the Import URL button , which is on the right, to open the window shown below.
Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
Click the Go ahead! button .
The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.
We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.