The Code that Builds the Field for Riding the Rail
The code in the not-grayed-out area near the middle of the screenshot below builds the field (e.g., fence, rail and control buttons).
All the on-chat commands that start the program (described in Part 2), end by calling two functions:
- First, they call the function init_beginning, which is the centerpiece function (described below) that prepares the Minecraft world and sets variables to their initial values. init_beginning and the functions it calls are described in the post.
- Second, they call one of the two function that prepare the spawn list according the specification of the on-chat-command that launched the program. (see Part 4).
The function init_beginning
The function init_beginning performs the following actions:
- Moves the player to a new position by calling the function MoveMe.
- Sets the position variable “center” to be where the player is initially standing.
- Initializes the spawn list to be an empty list (array).
- Sets the spawned_MOB_counter to 0. This counter will be incremented each time a MOB is spawned from the spawn list and, if the MOBs are to be spawned sequentially, will be used as an index into the spawn list to select the next MOB.
- Initializes the Boolean variable b_spawn_at_Spawn_Position to true. This variable will later be used to decide whether to spawn a MOB at the Spawn Position or at a random position inside the rail.
- Calls the function init_sides_and_corners, which sets several variables that provide position and size information about the fence and the rail.
- Launches “fence,” which is an on-chat command that builds the fence. An on-chat command is used instead of a function so that the fence will be built at the same time as the rail is being laid, thus speeding up the program.
- Calls the function lay_redstone_and_rail, which builds the powered rail.
- Calls the function lay_corner_blocks to place the four corners of the rail. The corners must be placed after the four sides (Minecraft requirement).
- Calls the function put_stuff_in_player_inventory to put items that may be useful (e.g., minecarts) in player inventory.
- Calls the function set_buttons_and_spawn_position to place the four blocks that will serve as control buttons and to define the variable spawn_position to one block west of the northwest corner of the rail square.
- Places a sunflower, which always faces east, just inside the fence so that the coder can conveniently know directions when building the rail. The sunflower is near the northwest corner of the fence.
- Finally, this function teleports the player to a convenient position near the control buttons and the Spawn Position.
The Functions moveMe and setCenter
The function MoveMe, which is called by init_beginning (see prior section), moves the player 200 blocks east and to the ground. The new MakeCode ground block could have been used to do this. MoveMe calls setCenter, which sets the variable “center” to the player position. Riding the Rail code does not depend on the position of the player except for initially setting the variable “center.” After center is set, the player can move anywhere for a better view of the action.
The on-chat command “moveMe” is provided in case a player wants to move after a game. After a particular run of Riding the Rail, the MOBs can be noisy (I am especially distracted by the disgusting noise that Shulkers make) so I move to a quite place.
Initialize Sides and Corner Positions
The function init_sides_and_corners, which is called by init_beginning, sets a number of variables that are used in fence and rail building. Notice that rail_corner and fence_corner both refer to a northwest corner, which when the player is facing east, is down and to the left of center so both adjustments from center are negative to get to a northwest corner. This function calls init_rail_corner_lists, which will be described below.
On-chat command to build he Fence
The on-chat command “fence” is launched by init_beginning to build the fence. This is an on-chat command, instead of a function, so that it will run at the same time (asynchronously) as the rail is build. This speeds up the program. fence can be run asynchronously without fear of interference with other functions because it changes no variables and uses no variables that might be changed elsewhere.
As shown in the drawing below, the four sides of the fence are built starting at the northwest corner and continuing in the following order:
- northwest corner to northeast corner
- northeast corner to southeast corner
- southeast corner to southwest corner
- southwest corner to northwest corner
The fill blocks in the graphic below are in this same order.
Notice that in each fill block, the middle coordinate Y varies from 0 to 2, which makes the fence 3 blocks high.
Function to Lay the Rail with Redstone Under It
The function lay_redstone_and_rail is called by init_beginning. It sets the variables used by the function builder_square and calls it twice: once the lay redstone under the rail and once to lay the powered rail.
Function to Build a Square with Missing Corners
The function bulder_square does build a square; however, the corner blocks are missing. The corners will be laid later because Minecraft requires that the straight sides be placed first so that it will know when to make a rail curve. I tried using fill to lay the rail segments; however, this often laid the rails in parallel rather than connecting them. Frustrating.
Function to Lay Rail Corners
The function lay_corner_blocks is called by init_beginning. This is a separate function so that it can also be called from a on-chat command corners. The on-chat command may be useful for repairing rails that have some square corners.
This function lays unpowered rail blocks because Minecraft will not turn powered rails into corners. No matter — when using power, momentum carries the minecarts swiftly around corners, sometimes too swiftly with heavy MOBs (they fly right off the rail when trying to make the turn).
This function uses two four-item lists of position (see below)s:
- rail_corners is the four positions of the rail corners, which will be the not-powered rail that should curve if all goes well.
- rail_under_corners is the four positions underneath the rail corners, which will be the cobblestone foundation. I picked cobblestone just so that it would look different from the redstone, which used everywhere else, to make clear that the corners are not powered.
By using these lists, a for_loop from 0 to 3 can be used to loop through the four corners.
In the loop, the variable “where” is set to the corner position where the rail will be. The variable “under_where” is set to the corner position under the rail where the cobblestone will be.
The fill statement using air is employed to remove a undesirable square corner rail before placing another in hopes that the new rail will curve. Through trial and error I found that the brief pause after replacing the old rail with air, then placing the new one makes corners works more often. But, unfortunately, not always.
Function to Set List of Rail Corner Positions
The two corners lists (below) are initialized by the function init_rail_corners_lists.
The two empty items in the rail_corners list are there so that the code will be vertical, not horizontal. The empty items have no effect on the size of the list or the code accessing the list. Empty items can also appear inside the list with no problems — empties seem to be ignored by MakeCode.
The Spawn Position is the rail position east of the corner (left in this picture). When a minecart is placed on the rail, the next MOB in the spawn list is spawned at the Spawn Position.
Four blocks are placed at the same level as the grass. I call these control buttons because when one of these is broken, it causes some action to take place. The broken block is replaced with either the same block or another block depending on the particular block.
Define Spawn Position and Place Control Blocks
The function shown below first defines the Spawn Position, then places the four control blocks. The Spawn Position is calculated as one block east of rail_corner, which was set in a previously defined function. The control blocks are set relative to the Spawn Position. Notice that, from SpawnPosition, the east/west coordinate X is incremented by 1 for each blocks will be side by side. The Y coordinate is -1 so that the blocks will be level with the grass. The Z coordinate is the same for each block so that the blocks will be in a row.
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
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.