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


The Code that Builds the Spawn List for Riding the Rail

The code in the not-grayed-out area on the bottom left and near the middle of the screenshot below initializes the spawn list for a particular run of Riding the Rail. It does this by using the information specified in the on-chat command that launched the program .

code part 4

The Spawn List

As depicted in the drawing below, the spawn list is the link between the on-chat commands (e.g., animals) that start the program and the player-initiated events that enable interaction, for example,  the on-minecart-used event picks a MOB from the spawn list and spawns it in the minecart.

spawn List Big Picture.jpg

The on-chat-commands code put MOBs into the spawn list, which can be any length. Player-initiated events cause MOBs in the spawn list to be used either sequentially or randomly, depending on the player-choice in the initiating on-chat commands. If the MOBs are being used sequentially, when the last MOB in the list is removed, the selection will start over at the beginning of the list. MOBs are never removed from the list.

The variable mobs_to_spawn_list is a list that is initialized the code described in the post. In this post mobs_to_spawn_list is referred to by the short name “spawn list.” The spawn list is set to empty in the function init_beginning, which must be called before the main function described here, which is add_mobs_to_spawn_list_by_source. Note that earlier during coding of Riding the Rail and writing these posts describing the code, the name of the function was ended with “by_method” instead of “by_source” as it is now. It’s the same function.

Add MOBs to Spawn List by Source

The function “run” was described in Code Part 2 — on-Chat Commands for Riding the Rail in MakeCode for Minecraft.  It calls a function that adds MOBs to the spawn list depending on the argument num1 (aka spawn_list_source_of_mob which specifies the source of MOBs and can have values 0 to 3. The following four on-chat commands calls run with the appropriate arguments:

  • 0 — “selected”
  • 1 — “animals”
  • 2 — “monsters”
  • 3 — “all”

This post describes the function in the graphic below add_mobs_to_spawn_list_by_source and the functions it calls. The function is essentially a switch that calls different functions depending on the value of the variable spawn_list_source_of_mobs (aka MOB  source) as follows:

  • If MOB source is 0, the called function is select_mobs_to_spawn, which is modifiable by the player and will be described after all the simpler functions.
  • If MOB source is 1, the called function is add_animals_to_spawn_list.
  • If MOB source is 2, the called function is add_monsters_to_spawn_list.
  • If MOB source is 3, the functions for both animals and monsters are called.
  • If MOB source is any other valu9e, a error message is said.

add mobs by source

Function to Add Animals to Spawn List

In the last block this function calls add_mob_list_to_spawn_list, which does the work of adding one list to another. The preceding three statements set the variables used by the called function. The variable aList is the list to be added, which in this case is set to animalList (initialized when Riding the Rail starts and described below). The variable number_of_each_MOB_to_add is set to 1 here because only one of each animal type is needed. The variable number_of_MOB_types_to_spawn is set to the length of animalList because all animals are wanted.

add animals

Function to Add Monsters to Spawn List

The function to add the monsters list to the spawn list is similar to the one to add animals, except of course, it adds uses monsterList instead of animalList.

add monsters

The Animal List and Monster List

The lists animalList and monsterList  are initialized when the program starts. The on-chat command “animals” places all the animals in animalList in the spawn list, “monsters” places all the monsters in monsterList in the spawn list, and “all” puts both in the spawn list. These lists are also available for use for the advanced player using the “selected” on-chat command.
MOB lists

These lists can be modified by the player. For example, the player might want the items in the lists to be in a different order. She might want to exclude those MOBs that are not suitable for riding in a minecart (e.g., endermen, bats). In other situations, some monsters are not  available in the version of Minecraft being used. For example, as of this writing, only the first three monsters — shulker, vindicator and evoker — are available in Minecraft Education Edition. The other monsters should be removed from the list to avoid spawning “invisible” monsters, which is what will appear to happen if the unavailable monsters are left in the list and are used in the spawn list. Shulker, vindicator and evoker are the first three monsters in the monsterList so that they will be easier to retain and/or use by Education Edition players.

Player-Modifiable Function to Select MOBs to be Spawned

The function select_mobs_to_spawn  is provided for advanced players who want

  • complete control over which MOBs available to be spawned,
  • the MOB order in the spawn list and
  • the number of each to be used.

The function can have any number of sections, which are each terminated by a call to one of the functions add_mob_list_to_spawn_list or add_one_MOB_type_to_spawn_list. The sections can be in any order and more than one of each section type can be included in the function.

select mobs to spawn

The first section shown above adds animal types from animalList starting with index 0 and adding  number_of_MOB_types_to_spawn to the list . The second section is similar but uses the monsterList to add monsters. The last three sections each add one MOB type to the spawn list, which is specified by the variable MOB_to_add. The number to be added is specified by number_of_each_MOB_to_add.

Functions to Add MOBs to Spawn List

The function select_MOBs_to_spawn above terminates each section with a call to one of the following functions:

  • add_one_MOB_type_to_spawn_list or
  • add_mob_list_to_spawn_list.

The first function adds the MOB specified by the variable MOB_to_add to the spawn list number_of_each_MOB_to_add times.

add one type

This function adds number_of_mobs_from_list MOBs from aList to the spawn list. If number_of_mobs_from_list is less than the length of the list, all the MOBs in aList are added. the variable aList was set before this function was called to be either animalList or monsterList. By using the math block min (minimum) of the length of the list and the number_of_MOB_types_to_spawn, the code guarantees that it will not try to add more MOBs than are the aList.

add mob list

Output  Spawn List Information

Finally, messages are said to the player that provide the following information:

  • the number of MOBs in the spawn list. This is particularly helpful to the player who is learning to use “selected” MOB source to verify that the modified function does select the expected number of MOBs.
  • the spawn method: sequential or random.
  • a reminder to the player to select minecart in the Hotbar before clicking or tapping to spawn a minecart and MOB.

say number and surce

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 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 “Code Part 4 — Spawn List for Riding the Rail in MakeCode for Minecraft

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