How to Write Minecraft Mods with Functions

By Sarah Guthals, Stephen Foster, Lindsey Handley

When players refer to Minecraft functions, it is referencing one of two scenarios: using functions when writing code for your own mod OR the in-game /function command that allows a player to run a specific list of commands. We’ll go through how, in both instances, functions can be used to effectively by players to manipulate the game, execute specific actions, and ultimately make the game easier. Minecraft functions are useful because they can run multiple commands in sequential order to launch a specific sequence of events or actions. Command blocks are an alternative but often tend to induce additional lag that can slow down your game.

Using Functions to Write Minecraft Mods

The following content references how to acquire your Write Your First Mod badge through the LearnToMod program. It’s a great introduction for learning how to use Minecraft functions to build useful blocks of code. Let’s go through how to write a Minecraft mod that has a function named main. From now on, it will be called the main function. When you run a mod, your mod always starts running from the main function.

Notice that the Saying Hello badge is similar to the Write Your First Mod badge, except that it has three messages instead of two. Below is the code you should write to complete the Saying Hello badge. Again, notice how the code includes 3 specific actions merged into 1 function entitled “main”.

minecraft functions in mod coding

To get help with completing badges, click the Click for Hints link on the progress bar at the top of the mod, or watch the video to see how to solve the challenge. Sometimes it’s tricky to position the blocks in the right spot, so you can always reorder them after you have applied them to your programming environment, though.

To earn the badge, you have to test your mod in the Minecraft simulator on the LearnToMod site. The progress bar in this mod says that you have completed writing 100 percent of the code using functions, but it reminds you to test the code in the simulator.

testing functions in minecraft modding code

To test your mod in the simulator, click the Simulator button, which is next to the Mod button.

LearnToMod code simulator

The Minecraft simulator sometimes doesn’t work in older versions of browsers. If you download the Unity Player plugin and the simulator still doesn’t work, update your browser or use a different one. You can download free and reliable browsers such as Chrome, Safari, or Firefox.

Unity Player is the third-party software that supports your being able to see Minecraft in the browser.

When you click the Simulator button, the simulator pops up. To test your mod in the simulator, simply press the M key on the keyboard.

testing your minecraft mod

After the mod runs, you earn the badge. You see a screen shot of you completing the badge.

LearnToMod main function coding badge

To enter the simulator, drag the mouse into it and click anywhere. To exit the simulator, press the Esc key on the keyboard.

Now that you have completed writing a specific mod with a basic function, start testing adding more advanced variables to your function to create a longer chain of actions!

The Function Command (In Game)

In addition to using functions in your Minecraft mods, you can also use the /function command in-game to run a specific function. However, these functions need to be stored as text files and saved in the appropriate folder within your Minecraft folder. For more about function syntax and the requirements to setup functions, visit Minecraft Gamepedia.

For individual usage, the player can simply type the following while logged into the game:

/function namespace:path/to/function/file (if|unless selector)

  • Namespace refers to the text file that hosts the function code.
  • The if arguments are optional and will make the function only run if the selector was found or not found within the world.