Functions in Minecraft

Step 1
In this lesson we will learn how to use Functions in Minecraft

A function is a section of code that can be called to activate it's effects

This helps to:

  • organize our code
  • make a repeatable effect

Step 2
To begin we need to ensure that the scriptcraft libraries are loaded

This lets us use items and events

Step 3
Export Functions

Export Functions are the simplest functions to use in Minecraft. They are called in game using /js functionName()

Step 4
Make a function to heal the user!

A function begins with { and ends with } to tell the computer which code is included in the function

Step 5
Output a message

echo outputs a message to the person using the function and is great for letting players know what they did and for testing code!

Step 6
Export it

Exporting a function lets you use it in game!

Check Your Work
Step 7
Test it out!

It says the message, but doesn't heal us yet!

Step 8

self - refers to the person using the function

Step 9
Heal your self
Check Your Work
Step 10
Test it out!
Step 11
Create a new function to give out fish
Check Your Work
Step 12
It should display a message and give you a fish
Step 13
Funtions with parameter

parameter - A number or value we can give a function when we call it to affect its result

Step 14
Create a new function
Step 15
Add an effect
Step 16
Export it
Step 17
Variable number of items

number is set when you call the function and determines how many fire charges the player is given

Check Your Work
Step 18
Call the function in game

The number you put after fire determines how many fire charges you are given

Step 19
Add a function to launch yourself upwards at a chosen speed
Step 20
Calling functions with events

Event - a trigger we can use to begin a sequence of code.

- We also can read information from an event if we attach event as a parameter to the function

Step 21
Create a new function

Use event as our parameter so we can read information from the event

Step 22
Make the creature from the event glow
Step 23
Attach our function to an event

creatureSpawn is an event that triggers whenever a creature appears in your world

Step 24
The event being called is used in the function
Check Your Work
Step 25
Creatures should now glow when they spawn in your world
Step 26
Choosing an event

Follow these links to see all available events:

Block Events

Entity Events

Inventory Events

Player Events

Step 27
We can use these events to activate our code
Step 28
Make a new function use an event

  1. Use the list of events above
  2. Look in the player events
  3. Find an event that is triggered when the player eats an item
  4. Use it to activate this function:

Check Your Work
Step 29
Thats it!

Now you know the essentials of using functions in Minecraft!

Functions in Minecraft Info


MVCode Clubs

Created By

jake wheaton


Fundamentals in Minecraft

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach