Commands/function

From Minecraft Wiki
Jump to: navigation, search

Runs commands founds in the corresponding function file.

In Java Edition, runs a function or all functions in a tag from a datapack.
In Bedrock Edition, Runs a function from a behavior pack.

Syntax[edit]

  • Java Edition
function <name>
  • Bedrock Edition
function <name: filepath>

Arguments[edit]

<name> (BE: name: filepath)

In Java Edition:
May be of the format namespace:path/to/function/file, or #namespace:path/to/function/tag.
The format without the # prefix, namespace:path/to/function/file, runs a single function located at [data_pack]/data/namespace/functions/path/to/function/file.mcfunction.
The format with the # prefix, #namespace:path/to/function/tag, runs all functions included in the function tag located at [data_pack]/data/namespace/tags/functions/path/to/function/tag.json.
In Bedrock Edition:
May be of the format path/to/function/file
Runs the function located at [behavior_pack]/functions/path/to/function/file.mcfunction

Result[edit]

Fails if the argument is not specified correctly.

If successful, runs a function through the command source which ran the /function command.

In Java Edition:[1][2]
Success count is the sum of success counts of all commands ran in the function/functions plus 1 (this command itself).
For example,
foo:bar1
function foo:bar2
foo:bar2
function foo:bar3
foo:bar3
say hi
The Success count of /function foo:bar3 is 1 (one command in foo:bar3) + 1 (this command itself) = 2.
The Success count of /function foo:bar2 is 2 (from above) + 1 (this command itself) = 3.
The Success count of /function foo:bar1 is 3 (from above) + 1 (this command itself) = 4.
Output of the command returns the number of commands ran (not the number of commands that succeed) plus the number of embedded functions called by a function.
The output count of /function foo:bar3 is 1(one command in foo:bar3).
The output count of /function foo:bar2 is 1 (one command in foo:bar2) + 1 (one command in foo:bar3) + 1 (one embedded function) = 3.
The output count of /function foo:bar1 is 1 (one command in foo:bar1) + 1 (one command in foo:bar2) + 1 (one command in foo:bar3) + 2 (two embedded function) = 5.
If successful, /execute store success always returns 1. Otherwise, returns 0.
If successful, the return value of /execute store result is the same as output count. Otherwise, returns 0.

Examples[edit]

  • In Java Edition:
    • To run a function located at data/custom/functions/example/test.mcfunction in a loaded data pack: /function custom:example/test
    • To run all functions in a function tag located at data/custom/tags/functions/example/test.json: /function #custom:example/test
    • To run a function if there is a sheep within a radius of 2 blocks: /execute if entity @e[type=sheep,distance=..2] run function custom:example/test
    • To run a function unless the executor is a player: /execute unless entity @s[type=player] run function custom:example/test

History[edit]

Java Edition
1.12pre1Added /function.
1.1317w49b/function now accept function tags as the argument.
Removed [if/unless] arguments in favor of /execute [if/unless].
Bedrock Edition
1.8.0beta 1.8.0.8Added /function.

See also[edit]

  • /tag – To run function in batch, every tick, or on server (re)load.

References[edit]