Riding the Rail with MakeCode for Minecraft


Riding the Rail Cam 2 smaller


This code is dedicated to the new MakeCode for Minecraft block

on item placed

without which Riding the Rails would not be nearly so awesome. Shout-out to The MakeCode Team for this and all the other marvelous new features. I built the first version of this program several months ago. I didn’t release it because I could not figure out how to spawn a MOB (animal or monster) inside  a minecart — until the on-item-used block.

With the new on-item-used block, a MOB can  easily be spawned in a minecart by placing the  minecart  at a know spawn position. The code below shows how. With a minecart selected in the Hotbar, the player right clicks the Spawn Position (left picture) to place a minecart. This triggers the on-minecart-used event and the MOB is spawned in the cart. Yippee!

The pictures below show several results of placing a minecart at the Spawn Position.

Using Riding the Rail

Riding the Rail builds a powered rail inside a fence. The player uses the control buttons to add minecarts and MOBs. The fence prevents mobs that can be contained from leaving the fenced area. Some MOBs will jump into a minecart. Or, the player may chose to have them spawned in the cart. Some MOBs will crowd into a cart even if it already occupied. Heavy MOBs sometimes wreck as they go around a rail corner. I haven’t found an end to the fun and experiments.

Below is the field on which Riding the Rail is played. Inside a square 3-block-high fence is a square powered rail with the needed redstone under it. The corners of the rail are not powered, which is a Minecraft requirement. The sunflower in the bottom left corner is, as always, facing east. Knowing one’s direction is essential when exploring the code.

field 3

The circled rail segment shown below is a special position called the “Spawn Position.” The Spawn Position is used to spawn MOBs in minecarts.

spawn position 2

To Place a Minecart with a MOB in It

Right clicking the Spawn Position will cause a MOB to be spawned in a minecart if the following two conditions are met:

  1. In the Hotbar, the minecart is selected (see picture above). Set it to one by touching it or typing 1 on the keyboard with chat windows closed.
  2. The control block nearest the corner  is gray, not black. Left clicking the gray  block (breaking it) will cause it to turn gray. If black, clicking will turn it gray.

To Place a Minecart by Itself and Spawn a MOB at the Spawn Position

If the above two condition are met, right clicking anywhere on the rail other than the Spawn Position will place a minecart at the rail position and spawn a MOB at the Spawn Position.

Using a Touch Interface (iPad and Windows 10 Touch)

At this time, on a touch -interface (e.g., iPad, touch Windows 10), to place a minecart with a MOB in it, requires as extra step.

  1. First, tap  (right-click equivalent) the Spawn Position to place the minecart.
  2. Then, tap and hold (left-click equivalent) the brown button to cause MOB to be spawned in the minecart.

This two-step process is required the on-item-used event is not operating as expected when using a touch interface. This is  described in my post Weirdness in the On-Item-Used Event with Touch-Interface MakeCode for Minecraft

To Place a Minecart by Itself — No MOB

If the gray/black button is black, right clicking anywhere on the rail will pace a minecart at the clicked position. No MOB will be spawned. The gray/black button is the one nearest the corner. If the button is gray, turn it black by left clicking to destroy it. It will be replaced by a black button. Click again to turn back to gray.

To Spawn a MOB by Itself — No Minecart

Left clicking the green button will cause a MOB to be spawned at a random location inside the rail square. This is useful when exploring which MOBs will jump into minecarts (most will eventually).

Specifying Mobs to be Spawned

The simplest way to specify which MOBs will be spawned, is the start the program by typing the name of a MOB in the chat window.

select by name

For example, if the program is started by typing “llama” in the chat window, every MOB spawned will be a llama.


If the program is started by typing “spider” in the chat window, every MOB spawned will be a spider.


If the program is started by typing “shulker” in the chat windows, every MOB spawned will be a shulker. As of this writing, only three monsters can be spawned in Minecraft Education Edition: shulker, vindicator, and evoker; but, all are available in MakeCode for Minecraft on Windows 10.


If more than one type of MOB is desired, one of the following commands can be typed in the chat window:

  • selected — this is the most advanced method of selecting MOBs to be spawned and will be described in the section Advanced MOB Selection. The MOBs selected will be spawned in the order they were specified.
  • animals — all animals in the animalList will be available to be spawned in the order they are in the list.
  • monsters — all monsters in the monsterList will be available to be  spawned in the order they are in the list.
  • all — all animals in animalList and monsterList will be available to be spawned, with animals first.
  • selected_random — same as “selected” except that specified MOBs are spawned in random order and may be repeated.
  • animals_random — same as “animals” above except that specified MOBs are spawned in random order and may be repeated.
  • monsters_random — same as “monsters” above except that specified MOBs are spawned in random order and may be repeated.
  • all_random — same as “all” above except that specified MOBs are spawned in random order and may be repeated.


The command above such as “animals” are simply more memorable ways of using the “run” command with number arguments specifying the MOB source and the order in which they are to be spawned. Of course, one can use the run command with the appropriate arguments. “run” with no arguments, which means that both arguments are zero, is the same as using “selected.”

In the picture below,  the program was started with the command “animals” spawning the animals in the order in which they are in animalList (see next section).


Similarly, the command “monsters_random” enables spawning the monsters chosen randomly from monsterList. For each spawn a monster is selected randomly from the entire monsterList; therefore, the same type of monster may be spawned multiple times.

monsters 5

The on-chat command “selected and “selected_random” cause the MOBs to be selected by the function “specify_mobs_to_spawn,” which can be modified by the player. It enables the player to have complete control over which MOBs and how many of each are to be spawned.  the function is described in the following section. The picture below shows the result of using the function to create the spawn list as 5 chickens and 1 ocelot. This was an experiment was to see if the ocelots would kill  the chickens.  After spawning 20 MOBs, 17 were chickens and 3 were ocelots.

ocelots and chickens

After a few minutes, the situation was as shown below: the ocelots had killed the chickens in the minecarts next to them. Then, one ocelot  jumped out to get the chicken in the middle.

ocelots and chickens eaten

Wonder what the situation would be later?

MakeCode Lists Specify the Available MOBs

Available MOBs are specified in  animalList and monsterList as shown below.  The player can change the lists as desired. All possible MOBs that make sense to spawn in a minecart are included here. MOBs not included are those — like fish and bats — that either will not survive or just will not stay inside a cart. A player might want to rearrange the order of MOBs in a list or even remove some MOBs. Since as of this writing, only the first three monsters work in Minecraft Education Edition, the player should remove all others.


Advanced MOB Selection

As an option, the player can further specify the MOBs to be used by changing the function below. This function is only used if either of the commands “selected” or “selected_random” start the program.  This function  puts the MOBs to be used in a list called mobs_to_spawn_list. The four sections of this function have blocks that can be changed as follows:

  1. Specify the stop_MOB_index_p1 (“p1” means plus one) for the animalList. Also specify the number of each of the selected animals to be spawned. This number can be 0,  in which case no animals will be included.
  2. Specify the stop_MOB_index_p1 for the monsterList.
  3. Set MOB_to_add to be a specific animal and specify the number of animals of this type to be spawned, usually 1.
  4. Set MOB_to_add to be a specific monster and specify the number of monsters of this type to be spawned, usually 1.
  5. The last to section can be duplicated if multiple animals and/or monsters are desired, which is what I did to get the chickens and ocelots in the proportions I wanted in the experiment described above.

specify mobs

Stopping and Starting Minecarts

Using a left click to break the circled sandstone block (left picture), will cause the redstone blocks under the rail to be replaced by sandstone (right picture). This removes all power from the rails and causes the carts to slow and stop except for those with enough momentum to continue for a time.  Breaking the circled redstone block (right picture) will replace all sandstone with redstone restoring power to the rails and start the carts moving again.

on-chat command “corners”

Sometimes some of the four rails corners may not be round. This is a Minecraft issue beyond my control  If have thought that it might be a timing issue: the program runs much faster than a player can “manually” lay track. I have experimented with pauses before laying corners to no avail. An on-chat command “corners” is supplied to correct the corners that are not round; however, often once a corner has decided to be square likely running corners again will not correct it. Usually using another chat command, like “animals,” will  generate proper corners. Stopping and starting the program does not help.

A way to fix the square corners that always works is to  employ the following steps:

  1. Destroy the wrong corner by left clicking it.
  2. Right click the same place with the unpowered rail selected in the Hotbar.

I have noticed that if there is one square corner, there is likely another. Flying above the rail to verify that all corners are correct before starting to spawn MOBs is good practice — or one can just wait until some carts with their occupants pile up at the square corner. That’s fun, too.

on-chat command “moveme”

The command moves the player 200 blocks east, which is likely a clear area of the world. It is not necessary to use this command because when the program starts, it will do it.  I use “moveme” when the MOB sounds  get annoying.

To Jump into a Minecart Yourself

A player might want to ride the rail herself. To do so follow these two steps:

    1. Place a minecart on the rail in.
    2. Right click inside the minecart.
  • To get out out of the cart, jump or fly out.


The Code

The code  will be described in my next post. To whet your appetite, the picture below displays all the code for Riding the Rail.

all code

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 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.

One thought on “Riding the Rail with MakeCode for Minecraft

  1. Pingback: Code Part 1 — Overview of Riding the Rail in MakeCode for Minecraft | We Code MakeCode