Minecraft Blogs / Tutorial

Function Data Packs for Dummies #5 | Let's play tag! (Load, tick, uninstall and other functions)

  • 6,667 views, 5 today
  • 47
  • 17
  • 22
Bertiecrafter avatar Bertiecrafter
Site Moderator
Level 67 : High Grandmaster Stack
628
This series of tutorials will teach you how to create your very own data packs, assuming you know absolutely nothing about commands. Although data packs could just contain files that replace built-in assets like resource packs, these posts will focus on adding new features and mechanics to the game. In every part I assume you've read the previous parts.

Getting (even more) hooked into Minecraft

Last time you learned how to write a function, but you were probably wondering why all those datapacks are able to do so much magic without ever having to type a single /function command. Data packs hook into Minecraft game events to create their own game cycle and call functions based on certain conditions. These hooks follow a simple mechanism: Call all functions in certain tags and allow any data pack creator to register a function in those tags. Minecraft doesn't know what functions will be in the tag during development and you don't know which tags are called at what times unless documented.

The tags we're going to be looking at are: "minecraft:tick" and "minecraft:load". The first set of functions is executed after every tick (20 times per second) and the second set of functions is executed after every /reload.

Let's start by creating a function named "tick" and a function named "load". Technically these functions could have any name, but it's easy to spot them in a larger list of functions if you call them that way. Their contents can simply be /say, /tellraw or /title commands. Test if they output something by manually running "/function <namespace>:path/to/function/tick" and one similar for "load" in the minecraft chat.

Now open up the "data" folder of your datapack. Create a new folder named "minecraft", with a "tags" folder inside. This last folder has another folder called "functions" inside. As you could have guessed, the "minecraft" folder causes the tags to be in the minecraft namespace (the part before the colon in the tag names above). Now create a "load.json". Use this format for it's contents. You should know how to write JSON by now. Make sure to include the namespaced id of the function in the values array. The "namespaced id" is the function namespace and path to the load function, the same piece of text you would put into the /function command. Now run /reload and check if the load function you made is fired.

Create a tick.json file in the same folder as load.json. The contents are roughly the same, but in the new tick.json file you should be referring to the tick function. After reloading, see if chat receives output 20 times a second. You might not want chat output 20 times a second forever, so you might want to clear the contents of the tick function after.

What about the uninstall function?

There is no official uninstall tag. However, it's best to provide a function to clean up any mess that the load command makes. For example the function could remove any scoreboard objectives or reset gamerules. Make sure to inform the player on load that this function exists.


Can I create my own tags?

Absolutely! Just put the following command anywhere in your data pack.
/function #<namespace>:<path-to-function>For example, you can run /function #minecraft:load to call all load functions. Anyone using your data pack to extend theirs could register a function in your custom tag using the method described above. Note that you only have to write JSON for registering functions in tags, you don't have to define any new tags in your data pack using some kind of JSON file. Using function tags is one way of creating an API, a set of connection points that other data pack developers can hook into.

Challenge Yourself


After every tutorial, I'll include a section where you are challenged to apply what you've learned. I recommend you playing with what you've learned, it helps you getting familiar with new concepts and be able to find solutions to problems. You don't have to do exactly what's written below, you can always challenge yourself in a different way.

This data pack will make time run backwards. Make sure that the data pack freezes time using the /gamerule command and displays a nice coloured message in chat saying that the data pack is loaded with a little bit of credit to you, using the minecraft:load tag. Also print the function that should be called if the player would want to uninstall the data pack. Inside the uninstall function, undo the changes applied to the gamerule. You don't have to take the previous value into account, just make time tick on uninstall regardless of what the setting was before. Every tick, use the "/time add" command to add just below 1 day every tick. This creates the illusion that you are removing some value from time, even though there is no "/time remove" command. The same trick is used to make these water droplets fall upwards!

What's next?

Next up we're going to look selector variables/arguments and relative locations.
Subscribe if you want to get notified of new posts.

Function Data Packs for Dummies #5 | Let&#039;s play tag! (Load, tick, uninstall and other functions)
Function Data Packs for Dummies #5 | Let&#039;s play tag! (Load, tick, uninstall and other functions)
Tags

Create an account or sign in to comment.

1
05/27/2022 12:37 am
Level 1 : New Miner
Latelypoo
Latelypoo avatar
How do i create multiple tick.json files? Creating another file makes it a load.json file.
1
06/06/2022 3:38 pm
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
Apologies for the late reply!

The .json files contain arrays (lists) that can contain multiple function names. So even though you can only have 1 tick and 1 load .json file, you can make each file list/reference multiple function files. Each function file can also call multiple other function files using the "function" command (yes, the same command you use to run functions in chat, can also be used inside other functions!).

See one of my other comments for an example tag .json file.
2
05/05/2022 1:10 pm
Level 26 : Expert Dragonborn
EndermanDotDat
EndermanDotDat avatar
this seems so much easier somehow all of a sudden..
1
02/14/2022 8:38 pm
Level 1 : New Miner
Flintloxx
Flintloxx avatar
Hi! I have tried many upon many attempts to figure out what to put on the .json files. but can't figure it out at all. Can you please give a more in-depth explanation on what is happening in the file and how to make it function properly?
1
02/15/2022 1:06 am
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
There are spoilers with the file contents in the comment section below. Minecraft doesn't know each and everyone's function file names, so instead it calls function tags (= groups). The .json files define that your function is part of that group.
1
02/09/2022 1:51 am
Level 1 : New Miner
Jetpack Cat
Jetpack Cat avatar
this is a pretty bad tutorial, just saying
2
02/09/2022 2:00 am
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
Okay, thank you for the constructive feedback xD
2
01/11/2021 12:09 pm
Level 7 : Apprentice Miner
WyLL
WyLL avatar
I cannot for the life of me figure out what you want me to put in load.json
2
01/11/2021 12:17 pm
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
Part 2 should have taught you how to write JSON. You can find the JSON format for tag files here. Try and see if you can turn that into a JSON file. It's very important to be able to read these kind of wiki structures as you'll be seeing it a lot. If all else fails, the solution is in the spoiler below, but give it another try first.
load.json

{
"values": [
   "<namespace>:[path/to/function/]load"
]
}


For example:{
"values": [
   "bertiecrafter:my_data_pack/load"
]
}

2
08/06/2020 12:26 pm
Level 1 : New Miner
hardcoremc007
hardcoremc007 avatar
I am confused about what you mean by just saying use this format and putting in a link and how that would work to make a timer, it would make a lot more sense to me in video form but I know how much a hassle making and editing videos is but I would like a more in depth explanation
Planet Minecraft

Website

© 2010 - 2022
www.planetminecraft.com

Welcome