Functions in Minecraft

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

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
load("scriptcraft/modules/bukkit/master.js");
Temp1546911778

This lets us use items and events

Information
Step 3
Export Functions

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

Temp1546911957
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

Temp1546912268
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!

Temp1546985494
Step 6
Export it

Exporting a function lets you use it in game!

Temp1546985457
Check Your Work
Step 7
Test it out!
Temp1546912659 Temp1546912764 Temp1546912979

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

Information
Step 8
Self

self - refers to the person using the function

Temp1546913161
Step 9
Heal your self
Temp1546985419
Check Your Work
Step 10
Test it out!
Temp1546913374 Temp1546913396 Temp1546913460
Challenge
Step 11
Create a new function to give out fish
Temp1546914104
Check Your Work
Step 12
It should display a message and give you a fish
Temp1546914212
Information
Step 13
Funtions with parameter
Temp1546985684

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

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

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

Temp1546986115
Check Your Work
Step 18
Call the function in game

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

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

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
Temp1546986812

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

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

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

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

Follow these links to see all available events:

Block Events

Entity Events

Inventory Events

Player Events

Information
Step 27
We can use these events to activate our code
Temp1547067647 Temp1547067830 Temp1547067857
Challenge
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:

Temp1546987717
Check Your Work
Step 29
Thats it!

Now you know the essentials of using functions in Minecraft!

Temp1546988060
Functions in Minecraft Info

Account

MVCode Clubs

Created By

Missing tiny jake wheaton

Course:

Fundamentals in Minecraft

Access Level

premium

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