Here, you find out how to complete the Functions challenge in Minecraft and earn the Functions badge. To start, be sure that you have opened the Functions challenge by clicking on the badge.
By the time you reach the end, you will know how to define two new functions: creeper1 and creeper2. This is the final code that you write.
When you test this code, you see this scene.
In the Functions badge, the block named perform command performs the command that is in the text block for a certain player, as defined by the player block. This shows the perform command block, which summons a cow to you.
Minecraft has a set of commands that you can run from inside the game. You can search the Internet for the term Minecraft commands to see millions of web pages that introduce them.
For example, if you want to summon a cow without writing a mod, you can type the command /summon Cow in Minecraft to summon one.
To make Minecraft run the perform command block, you have to place the block inside the main function.
Test your code at this point, to make sure that it does what you want. When you run this code in Minecraft, you see a scene similar to this one.
As you can see in the completed code for this challenge, you need to define two new functions. Define the first one and test your code to make sure it works. Then define the second one. If you finish writing your code without testing it, you make errors harder to find, because you’ll have lots of code to trace.
To create and then call the creeper function, follow these steps:
Drag a function block into the programming environment.
Click on the words do something and rename the function creeper.
Add a perform command block in the creeper function and type summon creeper in the text area.
Run your mod in Minecraft or the simulator.
Notice that only a cow is summoned, not a creeper. The reason is that only the main function is called (from Minecraft or the simulator). You did not call the creeper function, so it’s ignored.
When you view the Functions category, notice that a new block has been created — the creeper call block.
Call creeper from the main function.
Run your mod in Minecraft or the simulator.
A cow and a creeper are summoned.
To better understand what is happening when you run your mod, trace your code using paper-and-pencil.
To trace each line of code, follow these steps:
The main function is called, and the first thing it does is run the perform command block that summons a cow, and that makes a cow appear, so draw a cow.
The creeper function is called, so Minecraft looks into the creeper function. You should look at it too.
When the perform command block that summons a creeper is run, it makes a creeper appear, so draw a creeper.
The creeper function has no more lines of code, Minecraft looks back (and you do, too) to the main function.
The main function has no more lines of code, so the code is complete.
In the same way you created the first creeper function, create the second creeper function. Step 3 in the Functions challenge gives you instructions on this task.
If you need help, you can ask questions in the Comment area below the badge by clicking the Are You Stuck? link.