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]

JE<name>: function
BE: name: filepath: CommandFilePath

In Java Edition, must be a namespaced ID, which refers to a single function, or one prefixed with a #, which refers to a function tag. In Bedrock Edition, must be of the format path/to/function/file, which refers to a function located at [behavior_pack]/functions/path/to/function/file.mcfunction.

Result[edit]

CommandTriggerJava EditionBedrock Edition
anythe arguments are not specified correctly Unparseable Failed
if the specified functions/function tags do not exist Failed
On successRuns the function(s) through the command executor which ran the /function command.

Output[edit]

CommandEditionSituationSuccess Count/execute store succeess .../execute store result ...
anyJava EditionOn fail000
On successthe sum of the number of all commands ran in the function(s) plus 1 (this command itself)1the number of commands ran (whether successful or no) plus the number of embedded functions called by a function.
Bedrock EditionOn fail0N/AN/A
On success1N/AN/A

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 message displayed in chat returns the number of commands ran (not the number of commands that succeed) plus the number of embedded functions called by a function.
The chat output count of /function foo:bar3 is 1(one command in foo:bar3).
The chat 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 chat 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 functions) = 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 chat 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]