Commands/function

From Minecraft Wiki
Jump to: navigation, search
function
Restrictions

Operator

First introduced

1.12 (1.12-pre1)

Runs a function.

Syntax
  • Java Edition
function <function> [if|unless] [selector]
Arguments
function
Must be of the format namespace:path/to/function/file.
if
Will only make the function run when an entity was found matching the selector.
unless
Will only make the function run when no entity was found matching the selector.
selector
When used with if or unless, this determines the entity to find or not find for the function to actually run.
Result
Fails if the argument is not specified correctly.
Fails if the selector was not found using if.
Fails if the selector was found using unless.
If successful, runs a function through the entity or commandblock which ran the /function command.
Examples
To run a function located at 'data/functions/custom/example/test.mcfunction': /function custom:example/test
To run a function if there is a sheep within a radius of 2 blocks: /function custom:example/test if @e[type=sheep,r=2]
To run a function unless the executor is a player: /function custom:example/test unless @s[type=player]
See also
gamerule gameLoopFunction – Runs a function every tick.