Minecraft Modding For Kids For Dummies
Book image
Explore Book Buy On Amazon

You can make lots of projectile effects in Minecraft in addition to exploding. Rather than rebuild all the projectile code from scratch, you can abstract the projectile state-machine (make it a projectile state-machine for any effect, not just exploding) and create a library that helps you build a lot of different projectiles.

A library is a mod that lets you call functions from other mods.

Set up the projectile mods

To set up the projectile mod, copy a projectile mod. Follow these steps:

  1. Click the Actions tab and then choose the Copy command.


    You see two versions of the projectile mod.

  2. Open one mod and rename it Projectile_Library.

  3. Open the other mod and rename it Exploding_Projectile.


    Here are the two renamed mods.


Outline the projectile library and explosion projectile mods

Open the mod that you renamed Projectile_Library. It looks like the code example shown here.


Making the Projectile_Library mod is a little tricky because it has to handle the following information:

  • All state information, like launching

  • The actual launching

  • Destruction of the projectile

But the Explosion_Projectile mod needs to handle

  • The type of block to launch

  • The actions that should happen when the block lands

Make one more copy of your mod and name it Original_Explosion_Projectile, just in case you mess up and need to get back to a working mod.

Change the main and launch functions

The following directions tell you how to actually make the changes in your code.

To make changes to the main function, follow these steps:

  1. Rename the main function init.

    Because this mod is now a library, the functions in it are called from other mods — though the library itself isn’t run in Minecraft.

  2. Export this function (make it accessible from other mods) so that it can be accessed from the Explosion_Projectile mod. This is how the function should look.


    You can find the export block under the Misc category.

  3. Add a parameter named launch_block to the init function that defines the type of block to be launched, and save the parameter in a variable named block_type.

  4. Delete the event.

    This step is handled by the Exploding_Projectile mod. The library doesn’t handle it, because you may want other things to trigger the effects later on.

  5. To name the exploding variable landing, click the drop-down arrow next to the word exploding and choose Rename Variable from the menu.


Renaming the variable is shown here.


Your init should look like this one.


After you complete this step list, you may want to make a minor change to the launch function. The image below shows how to change the bedrock block to the block_type variable instead; that way, the type of block is decided in the Explosion_Projectile mod.


The landing variable updates by itself when you rename it.

Change the explode function

To make changes to the explode function, you first need to change it to a function that has a return value. The purpose of this function, in the library, is to let the other mod know whether the block has already been launched. Follow these steps:

  1. Rename the function to check_if_launched.

    This shows the new function block you have.

  2. Make a new variable named launched and initialize (give it the value) it to false.


    This variable should be returned to let the other mod know whether the block has been launched.

  3. You need the if statement from the original explode function, but instead of sending a message and exploding and destroying anything around the block, just set the launched variable to true.

  4. Export this function too so that the other mod can check to see whether the block has been launched.


Change the destroy function

You also need to update the destroy function to look like this. Follow these steps:

  1. Add a parameter named block, which is the block that needs to be destroyed.

  2. Change the info’s block to the parameter block.

  3. Export the destroy function.

Congratulations! You have completed the Projectile_Library mod. The entire mod should look like the one shown here.

Figure 7-51
Figure 7-51

Change the Explosion_Projectile mod

After the library is written, you can define what happens in the Explosion_Projectile mod. First, open the Explosion_Projectile mod and import the Projectile_Library. Then follow these steps:

  1. Grab an import block from the Misc category.

  2. Type your LearnToMod nickname and then Projectile_Library. For example, if your LearnToMod nickname was sarah, your import block should look this.


    Now you should make changes to the main function because you have the library that you just created and you need to call those new functions.

  3. Remove everything from the main function, and delete all of the blocks except the event blocks. The two event blocks should go into a new function named on_land_event, but the function that should be called is a new function named on_land that has a parameter named info.

  4. Look in the Functions category and you should see three new functions from the library you just made.

  5. Add a call to the Projectile_Library.init function, with a parameter of Bedrock, and a call to the on_land_event function.


Finally, after you have fixed the main function, fill in the on_land function. Follow these steps:

  1. Add an if-statement that calls the Projectile_Library.check_if_launched function.

  2. Put the explosion code from the old explode function into the if-statement.

  3. Make a call to the Projectile_Library.destroy function.


Congratulations! You have completed the code for the Explosion_Projectile mod! Everything else in the mod can be deleted, so your entire mod should look like this.


Test your new library

If you’ve followed along with all the steps in this project, you should now have two mods:

  • Projectile_Library

  • Explosion_Projectile: When you test the Explosion_Projectile mod, you see a scene like this one.


Congratulations! You have successfully created a projectile library and used it to make an exploding projectile.

About This Article

This article is from the book:

About the book authors:

Sarah Guthals, Ph.D. is the CTO of ThoughtSTEM and has dedicated her life to coding education.

Stephen Foster, Ph.D. is the CEO of ThoughtSTEM, a company that teaches computer science to kids across America.

Lindsey Handley, Ph.D. is the COO of ThoughtSTEM and has hundreds of hours of experience as a classroom instructor for Minecraft based science and computer science classes.

This article can be found in the category: