Code Part 1 — Overview of Riding the Rail in MakeCode for Minecraft

 

This post is a companion to Riding the Rail with MakeCode for Minecraft, which explains how to use Riding the Rail and  should be read first. This post is an overview of the code for Riding the Rails.

Code Goals

Goals for Riding the Rail included the following typical user-interface requirements:

  • For new players — a simple way to choose MOBs (animals and/or monsters) that will be available to ride in minecart and
  • For advanced players — the flexibility to choose any combination of MOBs.

As experienced coders know, to have both simplicity and flexibility in a user interface is difficult to achieve. After struggling with several approaches, Riding the Rail provides a reasonable solution to this problem.

A further goal was for the code to be able to place a MOB in a minecart rather than place MOBs and minecarts separately while hoping that some of the MOBs will jump into minecarts. Placing a MOB in a minecart proved troublesome; and, in fact, is the reason this program was not released months ago. The new on-item-used event enabled a tidy way to place MOBs in minecarts. Further, the ability to place MOBs and minecarts separately is retained because it can provide much amusement.

Another goal was for the code to build the powered rail without troubling a player with details. This goal was met except that, after the rail is built, on rare occasions, the corner rails are square, not rounded, as they should be. This is easily repaired by the player (see Riding the Rail with MakeCode for Minecraft), but it is a nuisance. While writing this, I was inspired to make yet another change to the rail-building code that I think may fix this problem. We’ll see.

Code Overview

The code for Riding the Rail consists of the following four logical parts:

  1. Part 2: on-chat Commands — The on-chat commands specify the MOBs that will be available to ride the rail.  The on-chat commands also specifies the order in which MOBs will be chosen for spawning (sequentially or randomly). The program remembers the source of MOBs and the order in which they are to be spawned, then moves on to build the field.
  2. Part 3: Building the Field — The code that builds the field on which Riding the Rail is played. The field consists of the following elements: the fence, the rail with the Spawn Position, the control buttons and a sunflower.
  3. Part 4: Spawn List — The code that initializes the “spawn list” using the information it saved earlier about the source of MOBs. The spawn list is the list of MOBs that will be available to ride the rail. The particular on-chat commnad that starts the program causes the variable mobs_to_spawn_list (the spawn list) to be filled with the chosen MOB or MOBs.
  4. Part 5: Event Processing — The code that processes the events that respond to player actions;  for example, right clicking on the Spawn Position to place a minecart with a MOB inside.

All the code for Riding the Rail is shown below.

code all

The post describing this code grew so long that it became nigh impossible to work on. Therefore, I have split the description into four posts, each with its own video, explaining a section of code. The four posts are as follows:

Code Part 2: on-Chat Commands for Riding the Rail in MakeCode for Minecraft

The code described in Part 2  processes on-chat commands that launch Riding the Rail. These chat commands define the MOBs to be spawned and the order in which they will be spawned. The code is the not-grayed-out code to the upper  left in the screenshot below.

code part 2

Code Part 3: Build the Field for Riding the Rail in MakeCode for Minecraft

The code described in Part 3 builds the field (e.g., fence, rail and control buttons). It is the not-grayed out routines near the middle of the screenshot.

code part 3

Code Part 4: Spawn List for Riding the Rail in MakeCode for Minecraft

The code described in Part 4 initializes the spawn list with the MOBs specified by the on-chat command that started the program. The code is the not-grayed-out areas (to bottom left and middle).

code part 4

Code Part 5: Event Processing for Riding the Rail in MakeCode for Minecraft

The code described in Part 5  processes events in response to player actions. It is the not-grayed-out areas in the screenshot below.

code part 5

Code Requires a Flat World

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Get the Code

Riding the Rail code is shared on Code Connection at this URL

https://makecode.com/_bPdMMvgg5Mjw

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.
Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button 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.