setSpell() Function

Step 1
In this lesson we will define a function setSpell() that can be used to place a specific wand in your hand

Using this function you will be able to easily switch between spells without carrying a different wand for each spell. You won't even need to add a crafting recipe for each wand!

Step 2
Begin by defining a function setSpell() that just sends the player a message telling them they have changed their spell

NOTE: self can be used in the context of functions we can call directly in game. It will represent the player that called the function.

Check Your Work
Step 3
Wait what? I just defined that function in my plugin!

Call your function in game by typing the command /js setSpell()

We get an error saying the function is not defined! We have defined the function in our plugin, but it has not been exported to the global namespace

If we want to write a function that can be called in game using the /js command, we need to export it first using the syntax exports.functionName = functionName;

Step 4
Export the setSpell function

This line of code is setting the value of exports.setSpell to the setSpell function we have defined above.

exports.setSpell adds a globally accessible /js setSpell() function that will call the setSpell function from our plugin file.

Check Your Work
Step 5
Now we can call the function in game!

Once the function is exported to the global namespace we can call it in game using the /js command as shown here.

Step 6
Add a parameter for the name of the spell you want to cast

Add a parameter to the setSpell() function called spellName that we will use to determine which wand to place in the player's hand when they call the function.

We then check to see if the spellName provided was "Vanishing". If it is, we set the item in the player's hand to a magicWand_Vanishing and send them the message: "Spell set to Vanishing"

Check Your Work
Step 7
Make sure you can set you current spell to "Vanishing" using the setSpell() function

Type the command /js setSpell("Vanishing") in game. You should see this message as well as have the item in your main hand set to a magicWand_Vanishing that can be used to cast the vanishing spell on entities.

Make sure you get the correct wand and can successfully cast the spell!

Step 8
CHALLENGE: Add the ability to select any of your spells using the setSpell() function

Make sure you can use the setSpell() function to get any spell wand you have made so far in Wand World!

Temp1477616024 Temp1477616046
Step 9
CHALLENGE: Improve the setSpell() function by using the echo() function only once

We can simplify our setSpell() function by using the spellName parameter as part of the message we send to the player. This way, we only need one call to the echo() function in our setSpell() function and it will always send the player the right message!

setSpell() Function Info


MVCode Clubs

Created By

Mvcc logo new aaron


Wand World

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