Functions

Introduction

Sourced By Bedrock Commands Community Discord

Functions are .mcfunction files which contain multiple lines of commands. They are run with the /function command in-game.

Functions are created in a Behavior Pack, nested within the functions folder. A function pack creates a system using solely function files.

Functions are useful in many ways to reduce the time spent going from command block to command block debugging a system. They also help with packaging systems for use in multiple worlds and provide many functions that can change how everything works.

Function Pack Folder Structure

๐Ÿ“BP
๐Ÿ“functions
๐Ÿ“this_code.mcfunction
๐Ÿ“more_of_this_code.mcfunction
๐Ÿ“tick.json
๐Ÿ“nested
๐Ÿ“this_code_is_nested.mcfunction

Notes For Beginners

mcfunctionCopy
yaml
#Spawn Effects
effect @a [tag=atSpawn] regeneration 12 255 true
effect @a [tag=atSpawn] saturation 12 255 true
effect @a [tag=atSpawn] weakness 12 255 true
1
2
3
4
  • Each new line in a function file represents a new command. You may start a line with # to add comments. Commands in a function do not need to begin with a slash /, however doing so will not cause any errors.

  • All commands in a function are run in the same tick. Because of this, a function which causes large changes may cause a sudden lag spike and it is helpful to delegate some commands across multiple ticks, if possible. Commands in a function are still run in the same order, however.

  • Minecraft can not run more than 10,000 lines of commands in one function file. This includes any other function files that are executed inside of the original file.

  • It is not possible to run conditional commands. Those will still need to utilize command blocks in some way, or could utilize the 1.19.50 execute syntax.

  • Running commands with a specified delay in a function would involve using scoreboard timers to incrementally count up every tick (to a certain point), and executing at certain scores along the file. You may refer to Scoreboard Timers system to learn how to set it up.

Creating a Function

  1. Locate the ๐Ÿ“ com.mojang folder and navigate to ๐Ÿ“ development_behavior_packs

    • The development folders are used for quick reloading of packs, as the packs aren't cached to the world files.
  2. Create a folder (of any name) for the function pack. This will be referred to as Behavior Pack or BP.

  3. Create a ๐Ÿ“„ manifest.json file and a ๐Ÿ–ผ pack_icon.png file (optional) within the BP folder.

    • A manifest file contains all the information needed to register a pack, while a pack icon displays visually in the pack menu. A pack icon is typically a 128x128 or a 256x256 image, though any power-of-2 resolution will do, they will be upscaled and downscaled accordingly.
Sample ๐Ÿ“„ manifest.json
BP/manifest.jsonCopy
json
{
    "format_version": 2,
    "header": {
        "description": "Write Your Pack Description Here",
        "name": "Write Your Pack Name Here",
        "uuid": "00000000-0000-0000-0000-000000000000",
        "version": [ 1, 0, 0 ],
        "min_engine_version": [ 1, 19, 73 ]
    },
    "modules": [
        {
            "description": "ยงr",
            "type": "data",
            "uuid": "00000000-0000-0000-0000-000000000000",
            "version": [1, 0, 0 ]
        }
    ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Note that the uuid field needs to be replaced with an actual uuid, and the two generated must be different from one another. You can generate a uuid at https://uuidgenerator.net/

Sample ๐Ÿ–ผ pack_icon.png

Sample A:

pack_icon.png

Sample B:

pack_icon.png

  1. Create a ๐Ÿ“ functions folder. Any file within this folder that ends with .mcfunction will be registered as a function in-game, which can be run with /function <function_name>.

    • Nested functions are allowed, simply list the file path in relation to the functions folder as shown in the function pack folder structure.
  2. Apply the behavior pack in-game and try out the functions. Function file changes can be reflected in the world by running /reload or by simply relogging.

NOTE

Functions are versioned; therefore, they will run in the version listed in the ๐Ÿ“„ manifest.json, such as:

  • min_engine_version 1.19.50 or above will adopt the new execute syntax.
  • min_engine_version 1.19.70 or above will require aux values be replaced with block states.

Execution

Functions can be executed in-game by typing /function name_of_function. This will execute all the commands in the function file, all in a single tick.

Nested functions, for example BP/functions/lobby/items/1.mcfunction can be run using the nested folder path, in this case /function lobby/items/1

tick.json

The final file within a function is the tick.json file. This specifies functions to run server-side on every game tick, (similar to a repeating command block.) It is located in the BP/functions folder. By default, functions running in this file execute at origin 0, 0, 0 in the overworld.

BP/functions/tick.jsonCopy
json
{
  "values": [
    "function_1",
    "function_2"
  ]
}
1
2
3
4
5
6

Note: functions in this file are run as soon as the world is initialized, regardless of whether or not the player has been loaded. This may cause unintended behavior if used incorrectly.

Sample Function Pack

Troubleshooting Functions

Your functions may not appear within the command suggestions when using /function. This is normally due to an error with one or more commands in the function.

Enabling the Content Log in creator settings will allow you to see if there are any errors in your function pack, in which function the error is in, at which line and exactly what the syntax error for that command is.

The list of errors will be generated every time you load a world or run /reload to reflect changes after editing files. The list can be viewed on-screen for a few seconds as well as in the content log history in settings.

contentLogToggles

contentLogHistory

Contributors