As you saw earlier, you can create a Minecraft function using
By specifying only the function's name, it will be created inside the default namespace. However, you can specify it yourself:
Here, your function will be created inside the
One of the goal of Sandstone is to promote reusable block of commands. To make this possible, you have the ability to call other functions.
Your first possibility is to call another Minecraft function, just like you would in a normal Minecraft Datapack. To achieve this, you need to assign your MCFunction to a variable:
This will result in the following functions:
This approach has several advantages:
Commands are not duplicated. This results in a lighter datapack.
The function can be recursive.
It has a meaningful name in Minecraft (here, default:main).
However, it has three drawbacks:
It will create a
.mcfunctionfile for each functions, even if they are never called. Therefore, it makes it hard to share your functions with other peoples: if your library contains 100 helper functions, all datapacks using your library will include those 100 functions - even if they only use one.
It cannot take parameters. If you want to have a generic set of commands, that changes depending on some parameters, this is not possible.
The first drawback can be solved using lazy functions, and the second one using inline functions.
To prevent Sandstone creating functions when not mandatory, you can use lazy functions. A lazy function will be created only if another function calls it. A lazy function that isn't called won't even be present in the datapack:
As you can see, the
useless function has not been created. Let's call it from the
As you can see, the
useless function has been created, because it is called from
main. This feature is very useful to distribute lot of functions, in a library for example.
You can specify different options, other than
lazy, for your Minecraft functions.
|Whether the function should run each tick.|
|Whether the function should run when the datapack is loaded or reloaded.|
|The function tags to apply to this function.|
|If true, then the function will only be created if it is called from another function.|
Let's take a simple example using inline functions:
This results in:
As you can see, the commands from the
giveDiamonds function are directly written inside
main. Inline functions are a very efficient way to group up related commands, which helps writing a clean and logical datapack.
Inline functions can do everything a normal function does: using commands, calling MCFunctions, calling other lazy functions...
Sandstone allows you to wait a fixed amount of time between some commands, without having to manually declare a new function each time. This has several purpose: dialogs, event scheduling, animations etc... Under the hood, this uses the
/schedule command: however, all the complexity is abstracted away.
To wait a specific time between commands, there are two things to do:
Change your function to an asynchronous function, by adding the
await sleep(delay)line between your commands.
delay can be a number of ticks, or a time string like
Here is a minimal syntax:
There are two things to notice. First, our function is now asynchronous. They
async keyword has been added before the parameters list:
Second, we await the
You could simulate a dialog like this:
This example would compile to the following resources: