setSpell() Function

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

Temp1477614073
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.

Temp1477614632
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;

Temp1477614857
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.

Temp1477615033
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.

Temp1477615150
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"

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

Temp1477615632
Challenge
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
Challenge
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!

Temp1477616431
setSpell() Function Info

Account

MVCode Clubs

Created By

Mvcc logo new aaron

Course:

Wand World

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