Thursday, April 11, 2019

Using functions in Minecraft (Education Edition and Bedrock)

I teach game design and development and I encourage my students to use command blocks and redstone to automate functions in their games. It's pretty much essential as automation in student created games is required for many game elements including automatically spawning mobs,  opening doors, creating check points, changing the game mode, cloning areas of their game to reset them, and so much more.

In Minecraft Education Edition, NPCs are pretty awesome as they help to drive / narrate the story but also have the ability to issue commands, and not just one command, but as many commands as you want them to execute. Command blocks are great as well, but one limiting factor is that you can only execute one command from each command block. You can use chain command blocks to execute a number of consecutive commands, but you cannot simply enter line after line of code in one command block (something I keep begging the minecraft team for :) ) . Well, now there's an answer...

You can create functions in a text file and call upon that function from a command block (or from the command line in the game.)

There are a number of steps to make this whole thing work as it essentially works within a behavior pack.

So, here goes...

Step 1:

Download a standard behavior pack. I suggest going to the Minecraft Add-Ons page  or you can just download the Vanilla Behavior pack here.

Add caption
Step 2:

Once you download the pack, you will want to extract the behavior pack. I suggest renaming the folder to something unique for you (rather than Vanilla Behavior Pack).

Step 3:

Place this folder in the com.mojang/behavior_packs folder.

The path to the com.mojang folder is as follows:

Minecraft for Windows 10
C:\Users\(your pc username)\AppData\Local\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang\

Apple iOS
Apps/com.mojang.minecraftpe/Documents/games/com.mojang/
Step 4:

Open the folder and edit the manifest.json file with notepad or a similar text editor

Original manifest.json file looks like this:

{
    "format_version": 1,
    "header": {
        "description": "Example vanilla behavior pack",
        "name": "Vanilla Behavior Pack",
        "uuid": "ee649bcf-256c-4013-9068-6a802b89d756",
        "version": [0, 0, 1]
    },
    "modules": [
        {
            "description": "Example vanilla behavior pack",
            "type": "data",
            "uuid": "fa6e90c8-c925-460f-8155-c8a60b753caa",
            "version": [0, 0, 1]
        }
    ],
    "dependencies": [
        {
            "uuid": "743f6949-53be-44b6-b326-398005028819",
            "version": [0, 0, 1]
        }
    ]
}

I'm sure this looks confusing. This is the manifest file that is bundled with the behavior pack. In order to use it for behavior packs, resource packs, or in this case functions, you need to make a few changes.

First off, you should change the description and name to something more relevant to you. You also need to include the version of minecraft and the minimum version. You need to change the uuid for the header and modules sections. You can use a uuid generator. There is also an open source UUID generator created by Alan Reed. If you are not using resource packs, you should delete the "dependencies" section or you will receive an error indicating that a dependency is missing - might not be a big deal, but let's do our best to get rid of any errors.

Here's what my updated manifest.json file looks like:

{
    "format_version": 1,
    "header": {
        "description": "functiontest",
        "name": "functiontest2",
        "uuid": "cc97a5a3-5bad-11e9-8647-d663bd873d93",
        "version": [1, 9, 3],
  "min_engine_version": [1, 9, 3]
    },
    "modules": [
        {
            "description": "functiontest",
            "type": "data",
            "uuid": "7ea17070-5bae-11e1-8647-d663bd873d93",
            "version": [1, 9, 3]
        }
    ]
}

I changed the description and name in the header and the description in the modules section. I changed both uuids using a uuid generator. As mentioned previously, you can also find an open source UUID generator here.

Step 5: 
Create the function folder and file. The folder must reside inside the behavior pack folder that you are working with and the file must have the extension .mcfunction. For example, you can create a folder called functions and a file called function_house.mcfunction. You can use a sub folder within the functions folder to keep organized if you will be  using multiple functions. If so, you can call upon them using the subfolder name / file name. You should edit this file in notepad or a similar text editor.

My test.mcfunction file looks like this:



Here's what the folder structure looks like:


Step 6: Package your behavior pack to add it to the game!

Now you need to compress or zip this folder back up so that you can use it with the game. Go to the directory for the behavior pack (in my example above it would be called functiontest2. Right click on the folder in windows explorer. Choose send to -> compressed .zip file. This will compress the folder. The resulting file (compressed file) will be something.zip (i.e. functiontest2.zip). Change the extension to .mcpack or .mcworld (i.e. rename it to functiontest2.mcpack or functiontest2.mcworld. I haven't played with the .mcworld but that's the extension for a minecraft (education edition or win10) world while .mcpack is the extension for a behavior pack.




Step 7:  Add the behavior pack to a minecraft world. 

You can actually double click (or open) the .mcpack file. This will import it into Minecraft Education Edition or Minecraft Windows 10 Edition so that you will easily find it when you are ready to add it to your world. You can create a new world and add the behavior pack.


Select the behavior pack and click on the + to add it to this world.

Step 8: Play your game and test your function!

You can enter the function command in the command line...



or you can add it in a command block ...


Either will result in executing the function from the .mcfunction file.


If you recall from above, the function was set to give the player a diamond sword, and diamond chestplate, build a class cube in the air (using the fill command) and summoning some mobs including an ender dragon.

Pretty cool, eh? Can you imagine all the incredible possibilities?  What are you waiting for? Create and execute a function to create an arena with a bunch of mobs and teleport the player into the arena with a sword and armor to defend the honor of the king!!!