Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Minecraft:Commands/function: Difference between revisions

From SAS Gaming Wiki
imported>Kestrer
m Add link
 
SyncBot (talk | contribs)
Remove broken links to missing pages
Line 4: Line 4:
* 2 {{only|java|short=1}}
* 2 {{only|java|short=1}}
* 1 {{only|bedrock|short=1}}
* 1 {{only|bedrock|short=1}}
| others = [[Minecraft:Cheat]] only{{only|bedrock|short=1}}
| others = Cheat only{{only|bedrock|short=1}}
}}
}}


Line 10: Line 10:


== Usage ==
== Usage ==
{{IN|java}}, runs a [[Minecraft:Function (Java Edition)|function]] or all functions in a [[Minecraft:tags|tag]] from a [[Minecraft:data pack]].
{{IN|java}}, runs a function or all functions in a tag from a data pack.


{{IN|bedrock}}, runs a [[Minecraft:Function (Bedrock Edition)|function]] from a [[Minecraft:behavior pack]].
{{IN|bedrock}}, runs a [[Minecraft:Function (Bedrock Edition)|function]] from a [[Minecraft:behavior pack]].
Line 22: Line 22:
: <code>function <name> <arguments></code>
: <code>function <name> <arguments></code>
: <code>function <name> with (block <sourcePos>|entity <source>|storage <source>) [<path>]</code>
: <code>function <name> with (block <sourcePos>|entity <source>|storage <source>) [<path>]</code>
:: Runs a function or functions in a tag, with arguments for [[Minecraft:Function (Java Edition)#Macros|macros]].
:: Runs a function or functions in a tag, with arguments for macros.


* '''Bedrock Edition'''
* '''Bedrock Edition'''
Line 46: Line 46:


{{el|je|short=1}}: {{Argument|source|resource_location}} (in <code>storage <target></code> mode)
{{el|je|short=1}}: {{Argument|source|resource_location}} (in <code>storage <target></code> mode)
: Specifies a [[Minecraft:Command storage format|storage]] whose NBT is to be used.
: Specifies a storage whose NBT is to be used.
: {{arg desc|je=resource_location}}
: {{arg desc|je=resource_location}}


Line 77: Line 77:
{{Output table|edition=je|On fail|N/A|0|0}}
{{Output table|edition=je|On fail|N/A|0|0}}
{{Output table|On void|N/A|unchanged|unchanged}}
{{Output table|On void|N/A|unchanged|unchanged}}
{{Output table|On success|N/A|1|the return value of the first [[Minecraft:Function_(Java_Edition)#Returning|non-void]] function<br>(Execution stops after first {{cmd|link=none|return}} in any of the specified functions)}}
{{Output table|On success|N/A|1|the return value of the first non-void function<br>(Execution stops after first {{cmd|link=none|return}} in any of the specified functions)}}
{{Output table}}
{{Output table}}
{{IN|java}}, otherwise:
{{IN|java}}, otherwise:
Line 83: Line 83:
{{Output table|edition=je|On fail|0|0|0}}
{{Output table|edition=je|On fail|0|0|0}}
{{Output table|On void|0|unchanged|unchanged}}
{{Output table|On void|0|unchanged|unchanged}}
{{Output table|On success|1|1|the sum of the return values of all the [[Minecraft:Function_(Java_Edition)#Return|non-void]] functions}}
{{Output table|On success|1|1|the sum of the return values of all the non-void functions}}
{{Output table}}
{{Output table}}
{{IN|be}}:
{{IN|be}}:
Line 94: Line 94:
* {{IN|java}}:  
* {{IN|java}}:  
** To run a function located at <code>data/custom/function/example/test.mcfunction</code> in a loaded data pack: {{cmd|function custom:example/test|link=none}}
** To run a function located at <code>data/custom/function/example/test.mcfunction</code> in a loaded data pack: {{cmd|function custom:example/test|link=none}}
** To run all functions in a function [[Minecraft:tags|tag]] located at <code>data/custom/tags/function/example/test.json</code>: {{cmd|function #custom:example/test|link=none}}
** To run all functions in a function tag located at <code>data/custom/tags/function/example/test.json</code>: {{cmd|function #custom:example/test|link=none}}
** To run a function if there is a sheep within a radius of 2 blocks: <code>/execute if entity @e[type=sheep,distance=..2] run function custom:example/test</code>
** To run a function if there is a sheep within a radius of 2 blocks: <code>/execute if entity @e[type=sheep,distance=..2] run function custom:example/test</code>
** To run a function unless the executor is a player: <code>/execute unless entity @s[type=player] run function custom:example/test</code>
** To run a function unless the executor is a player: <code>/execute unless entity @s[type=player] run function custom:example/test</code>
Line 107: Line 107:
|{{HistoryLine|||dev=pre4|Added new arguments to the {{cmd|function|link=none}} command: <code>[if{{!}}unless] [selector]</code>}}
|{{HistoryLine|||dev=pre4|Added new arguments to the {{cmd|function|link=none}} command: <code>[if{{!}}unless] [selector]</code>}}
|{{HistoryLine|||dev=pre6|Skipped functions (when a conditional fails) are now considered failures when used in commands.}}
|{{HistoryLine|||dev=pre6|Skipped functions (when a conditional fails) are now considered failures when used in commands.}}
|{{HistoryLine||1.13|dev=17w49b|{{cmd|function|link=none}} now accept function [[Minecraft:tags]] as the argument.|Removed {{cd|[if{{!}}unless]}} arguments in favor of {{cmd|execute [if{{!}}unless]}}.}}
|{{HistoryLine||1.13|dev=17w49b|{{cmd|function|link=none}} now accept function tags as the argument.|Removed {{cd|[if{{!}}unless]}} arguments in favor of {{cmd|execute [if{{!}}unless]}}.}}
|{{HistoryLine||1.20.2|dev=23w31a|Added function macro arguments.}}
|{{HistoryLine||1.20.2|dev=23w31a|Added function macro arguments.}}
|{{HistoryLine||1.20.3|dev=23w41a|{{cmd|link=none|function}} command no longer returns the number of commands executed to better accommodate new {{cmd|return}} command.}}
|{{HistoryLine||1.20.3|dev=23w41a|{{cmd|link=none|function}} command no longer returns the number of commands executed to better accommodate new {{cmd|return}} command.}}
Line 132: Line 132:
{{Navbox commands}}
{{Navbox commands}}


[[Minecraft:de:Befehl/function]]
de:Befehl/function
[[Minecraft:es:Comandos/function]]
es:Comandos/function
[[Minecraft:ja:コマンド/function]]
ja:コマンド/function
[[Minecraft:ko:명령어/function]]
ko:명령어/function
[[Minecraft:pt:Comandos/function]]
pt:Comandos/function
[[Minecraft:ru:Команды консоли/function]]
ru:Команды консоли/function
[[Minecraft:uk:Команди консолі/function]]
uk:Команди консолі/function
[[Minecraft:zh:命令/function]]
zh:命令/function

Revision as of 15:44, 9 April 2026

Template:Infobox command

Runs commands found in the corresponding function file(s) with command execution context (executor, execution position, angles, etc) of the /function command itself.

Usage

Template:IN, runs a function or all functions in a tag from a data pack.

Template:IN, runs a function from a Minecraft:behavior pack.

Template:IN, the behavior of Template:Cmd (or Template:Cmd) is different with a direct call to the Template:Cmd command. If Template:Cmd runs multiple functions, execution stops after first Template:Cmd in any of the functions. If a direct call to the Template:Cmd runs multiple functions, all of the functions are executed, and the return values are added up as the result value of the Template:Cmd command.

Syntax

  • Java Edition
function <name>
Runs a function or functions in a tag.
function <name> <arguments>
function <name> with (block <sourcePos>|entity <source>|storage <source>) [<path>]
Runs a function or functions in a tag, with arguments for macros.
  • Bedrock Edition
function <name: filepath>
Runs a function.

Arguments

Template:Argument

Specifies the function(s) to execute.
Template:Arg desc

Template:El: Template:Argument

Specifies arguments for macro functions in a compound NBT tag.
Template:Arg desc

Template:El: Template:Argument

The position of the target Minecraft:block entity whose NBT is to be used.
Template:Arg desc

Template:El: Template:Argument (in entity <target> mode)

Specifies an entity whose NBT is to be used.
Template:Arg desc

Template:El: Template:Argument (in storage <target> mode)

Specifies a storage whose NBT is to be used.
Template:Arg desc

Template:El: Template:Argument

Specifies the NBT path. Should be a path of a single compound tag.
Template:Arg desc

Result

Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table

Output

Template:IN, for Template:Cmd: Template:Output table Template:Output table Template:Output table Template:Output table Template:Output table Template:IN, otherwise: Template:Output table Template:Output table Template:Output table Template:Output table Template:Output table Template:IN: Template:Output table Template:Output table Template:Output table Template:Output table

Examples

  • Template:IN:
    • To run a function located at data/custom/function/example/test.mcfunction in a loaded data pack: Template:Cmd
    • To run all functions in a function tag located at data/custom/tags/function/example/test.json: Template:Cmd
    • 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
    • To run a function macro with arguments a=42, b="example": /function custom:example/test {a: 42, b: "example"}
    • To run a function macro with arguments from storage custom:storage: /function custom:example/test with custom:storage

History

Java Edition

Template:HistoryTable

Bedrock Edition

Template:HistoryTable

See also

  • Template:Cmd – To run function in batch, every tick, or on server (re)load.

References

Template:Reflist

External links

Navigation

Template:Navbox commands

de:Befehl/function es:Comandos/function ja:コマンド/function ko:명령어/function pt:Comandos/function ru:Команды консоли/function uk:Команди консолі/function zh:命令/function