How to use functions in Minecraft with Microsoft MakeCode.
What are functions?
Functions are a convenient way to split your code into useful re-usable blocks. Functions can make your code easier to read (useful when debugging code) and make your code more efficient.
How to define a function
1. Launch the Microsoft 'MakeCode' editor and create a new project.
2. Select the 'PLAYER' tab (A). Click and drag the 'on chat command' block (B) onto a blank canvas and change the text option to 'hi' (see image below).
2. Select the 'PLAYER' tab (A). Click and drag the 'on chat command' block (B) onto a blank canvas and change the text option to 'hi' (see image below).
3. Add the following 'say' blocks:
4. Go into the java script view (Under the hood view) by clicking the JAVASCRIPT button at the top of the editor.
5. Click on line 5 and type in the following:
// Function to display a message in the chat menu
function greeting() { } |
6. Select lines 2 and 3. Cut the highlighted lines of code (A) and paste them (B) onto line 7.
7. Go to line 2 and type in the following:
greeting()
|
Your code should look like this:
8. Open Minecraft and click on 't' top open the chat window.
9. Type in 'print' in the command line (followed by the Enter key) to run your code.
9. Type in 'print' in the command line (followed by the Enter key) to run your code.
Resources:
minecraft-function.mkcd |
Part 2 - How to build a Skyscraper with functions
You may also be interested in:
This work is not affiliated in any way with Mojang AB. Minecraft is a trademark of Mojang AB. The Minecraft Name, the Minecraft Brand and the Minecraft Assets are all property of Mojang AB or their respectful owner. This work adheres to the terms set out by Mojang AB at https://www.minecraft.net/terms