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

  • 20
  • 8
  • 14
avatar Bertiecrafter
Level 58 : Grandmaster Cyborg
228
This series of tutorials will teach you how to create your very own datapacks, assuming you know absolutely nothing about commands. Datapacks can contain a number of things, but only functions and tags will be covered in these posts. 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.

Tags

1
01/11/2021 12:09 pm
Level 5 : Apprentice Miner
WyLL
I cannot for the life of me figure out what you want me to put in load.json
1
01/11/2021 12:17 pm
Level 58 : Grandmaster Cyborg
Bertiecrafter
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
1
10/18/2020 1:44 pmhistory
Level 1 : New Explorer
robbieradiant
When I added the tick.mcfunction and load.mcfunction and then reloaded and did /function looked for the autocomplete but, nothing appeared. They're just /tellraw "Hello from tick!" and /tellraw "Hello from Load!" but only helloworld.mcfunction will appear from the last tutorial. Please help.
1
10/19/2020 3:04 am
Level 58 : Grandmaster Cyborg
Bertiecrafter
In the last tutorial you learned how to enable the live log screen, do the logs say anything? Looking at the commands, I think you forgot to specify the recipient. (/tellraw <player> <message>)

If you don't like to specify player names, you can peek ahead at the next tutorial about target selectors ;)
2
08/06/2020 12:26 pm
Level 1 : New Miner
hardcoremc007
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
1
08/06/2020 3:39 pmhistory
Level 58 : Grandmaster Cyborg
Bertiecrafter
I'm assuming you're talking about the first section, about hooking into Minecraft. You're not "creating" a new timer by creating a new file (tick.json) in data/minecraft/tags/functions, but rather telling minecraft to call a function when an already existing timer fires.

If you're uncertain about how to write a tick.json, refer to an example below:
Example tick.json


NOTE: I expanded the first section to be a little more clear about how to create a tick.json file.
2
08/06/2020 3:48 pm
Level 1 : New Miner
hardcoremc007
thanks for clearing that up
1
06/19/2020 12:10 pm
Level 1 : New Miner
Rhaast77
main works perfectly but reload doesn't seem like, they are both called in their respectives load and tick, do you know why can this be happening? thank you for the tutorials! :)
2
06/19/2020 12:53 pm
Level 58 : Grandmaster Cyborg
Bertiecrafter
I don't think I understand your question.... If you got the tick function working, perhaps you can figure out why the load function is not? Otherwise, try explaining a little more :P
1
06/19/2020 8:49 pm
Level 1 : New Miner
Rhaast77
When I enable my datapack, the commands on tick seems to work, but when i use "/reload" nothing appears, sorry for my bad english :P
1
06/20/2020 12:28 am
Level 58 : Grandmaster Cyborg
Bertiecrafter
If you type "/function" in chat, does auto complete show the load function? Does running it output a message to chat? If not, there is something wrong with your load function code. On the other hand, if it does run just fine, you didn't register the function in the tag correctly. Compare with the minecraft:tick JSON file with the minecraft:load JSON file to figure out what went wrong in that case.
Good luck!
1
06/19/2020 9:03 pm
Level 1 : New Miner
Rhaast77
both are called correctly in both tick and load, but i can't understand whats failing
3
04/08/2020 3:59 pm
Level 40 : Master Procrastinator
Amrith Erilaz
Never thought to add a cleanup function. I think it'll be something I do moving forward.



Excellent work.
4
01/15/2020 12:11 amhistory
Level 58 : Grandmaster Cyborg
Bertiecrafter
Thank you Cyprezz, PMC, MasterKiloRen99, Luracasmus, daitallica, Wundercroft, Amrith Erilaz, FeedFrex, Chimerabot, Rune_Bloodstone, HydroByte, Rhaast77, sanonasu, Diamondpuppy808, TofuChild36, Vellaris and TheBigPug for the diamonds! =)
Planet Minecraft Logo

Website

© 2010 - 2021
www.planetminecraft.com

Welcome