Skip to content

Function Definitions

antD97 edited this page May 18, 2022 · 12 revisions

Every function definition no longer requires its own file. By creating a body for a function using {}, multiple functions can be created inside a single .mcfunction file.

Use the format:

[... function ]<namespace>:<functionpath>
{
    ...
}

The lines surrounded by the {} are used to create a .mcfunction file whose location matches the <namespace> and <functionPath> from the definition. If the definition is preceded by the word function, the function will be executed at its definition. If there is no preceding word function, the definition is removed from the file it was written in and only the .mcfunction file will be created.

Define & execute regex: (?<=^|\s)function (\S+):(\S+)(?=\s|$)
Define only regex: ^\s*(\S+):(\S+)(?=\s|$)

This:

# File: data/example/functions/tick.mcfunction

execute as @a[scores={time_alive=0}] run function example:player/on_death {
    say Oh no!
    say That hurt!
}

different_namespace:hostile/say_hello {
    say Hi!
    say I'm a hostile mob!
}

execute as @e[type=zombie] run function different_namespace:hostile/say_hello
execute as @e[type=skeleton] run function different_namespace:hostile/say_hello

Turns into this:

# File: data/example/functions/tick.mcfunction

execute as @a[scores={time_alive=0}] run function example:player/on_death

execute as @e[type=zombie] run function different_namespace:hostile/say_hello
execute as @e[type=skeleton] run function different_namespace:hostile/say_hello
# File: data/example/functions/player/on_death.mcfunction

say Oh no!
say That hurt!
# File: data/different_namespace/hostile/say_hello.mcfunction

say Hi!
say I'm a hostile mob!

Additional Details

  • While it does not affect how the function files are generated, function definitions can be nested inside of other function definitions.

  • .mcfunction files that end up with no executable commands after being converted will not be saved to the final converted datapack. This means that a .mcfunction file can be used solely for defining functions and will not needlessly become an empty function.

Clone this wiki locally