Minecraft:Commands/function: Difference between revisions
More actions
Fix template calls: add MC/ prefix |
Sync: updated from Minecraft |
||
| 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 = Cheat only{{only|bedrock|short=1}} | | others = [[Minecraft: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|bedrock}}, runs a [[Minecraft:Function (Bedrock Edition)|function]] from a [[Minecraft:behavior pack]]. | ||
{{ | {{IN|java}}, the behavior of {{cmd|return run function ...}} (or {{cmd|return run execute ... run function ...}}) is different with a direct call to the {{cmd|link=none|function}} command. If {{cmd|return run function ...}} runs multiple functions, execution stops after first {{cmd|link=none|return}} in any of the functions. If a direct call to the {{cmd|function ...|link=none}} runs multiple functions, all of the functions are executed, and the return values are added up as the <code>result</code> value of the {{cmd|function ...|link=none}} command. | ||
== Syntax == | == Syntax == | ||
| 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 macros. | :: Runs a function or functions in a tag, with arguments for [[Minecraft:Function (Java Edition)#Macros|macros]]. | ||
* '''Bedrock Edition''' | * '''Bedrock Edition''' | ||
| Line 29: | Line 29: | ||
== Arguments == | == Arguments == | ||
{{ | {{argument|name|function|name: filepath|CommandFilePath}} | ||
: Specifies the function(s) to execute. | : Specifies the function(s) to execute. | ||
: {{ | : {{arg desc|je=function|be=commandfilepath}} | ||
{{ | {{el|je|short=1}}: {{Argument|arguments|nbt_compound_tag}} | ||
: Specifies arguments for macro functions in a compound NBT tag. | : Specifies arguments for macro functions in a compound NBT tag. | ||
: {{ | : {{Arg desc|je=nbt_compound_tag}} | ||
{{ | {{el|je|short=1}}: {{Argument|sourcePos|block_pos}} | ||
: The position of the target [[Minecraft:block entity]] whose NBT is to be used. | : The position of the target [[Minecraft:block entity]] whose NBT is to be used. | ||
: {{ | : {{arg desc|je=block_pos}} | ||
{{ | {{el|je|short=1}}: {{Argument|source|entity}} (in <code>entity <target></code> mode) | ||
: Specifies an entity whose NBT is to be used. | : Specifies an entity whose NBT is to be used. | ||
: {{ | : {{arg desc|je=entity|amount=single|type=entitie}} | ||
{{ | {{el|je|short=1}}: {{Argument|source|resource_location}} (in <code>storage <target></code> mode) | ||
: Specifies a storage whose NBT is to be used. | : Specifies a [[Minecraft:Command storage format|storage]] whose NBT is to be used. | ||
: {{ | : {{arg desc|je=resource_location}} | ||
{{ | {{el|je|short=1}}: {{Argument|path|nbt_path}} | ||
: Specifies the NBT path. Should be a path of a single compound tag. | : Specifies the NBT path. Should be a path of a single compound tag. | ||
: {{ | : {{arg desc|je=nbt_path}} | ||
== Result == | == Result == | ||
| Line 66: | Line 66: | ||
{{Result table|The tag at <code><path></code> is not a compound tag.}} | {{Result table|The tag at <code><path></code> is not a compound tag.}} | ||
{{Result table|cmd=/... <arguments><br>/... with ..|With the provided arguments, any macro line in the function(s) becomes an unparseable command.}} | {{Result table|cmd=/... <arguments><br>/... with ..|With the provided arguments, any macro line in the function(s) becomes an unparseable command.}} | ||
{{Result table|cmd=|Only one function is specified, and in it there's no {{ | {{Result table|cmd=|Only one function is specified, and in it there's no {{cmd|return}} command being executed.|[[Minecraft:Commands#Result|Void]]}} | ||
{{Result table|Only one function is specified, and in it there's a {{ | {{Result table|Only one function is specified, and in it there's a {{cmd|return}} command being executed and returning a failure.|fail}} | ||
{{Result table|Multiple functions are specified, and in all of the functions, there is no {{ | {{Result table|Multiple functions are specified, and in all of the functions, there is no {{cmd|return}} command being executed.|Void}} | ||
{{Result table|succeed}} | {{Result table|succeed}} | ||
{{Result table}} | {{Result table}} | ||
== Output == | == Output == | ||
{{ | {{IN|java}}, for {{cmd|return run execute store ... run function ...}}: | ||
{{ | {{Output table}} | ||
{{ | {{Output table|edition=je|On fail|N/A|0|0}} | ||
{{ | {{Output table|On void|N/A|unchanged|unchanged}} | ||
{{Output table|On success|N/A|1|the return value of the first non-void function<br>(Execution stops after first {{ | {{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}} | ||
{{ | {{IN|java}}, otherwise: | ||
{{ | {{Output table}} | ||
{{ | {{Output table|edition=je|On fail|0|0|0}} | ||
{{ | {{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}} | ||
{{ | {{IN|be}}: | ||
{{ | {{Output table|onlybe=1}} | ||
{{ | {{Output table|edition=be|On fail|0}} | ||
{{ | {{Output table|On success|1}} | ||
{{ | {{Output table}} | ||
== Examples == | == Examples == | ||
* {{ | * {{IN|java}}: | ||
** To run a function located at <code>data/custom/function/example/test.mcfunction</code> in a loaded data pack: {{ | ** 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 tag located at <code>data/custom/tags/function/example/test.json</code>: {{ | ** 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 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 103: | Line 103: | ||
=== ''Java Edition'' === | === ''Java Edition'' === | ||
{{HistoryTable | {{HistoryTable | ||
|{{ | |{{HistoryLine|java}} | ||
|{{HistoryLine||1.12|dev=pre1|Added {{ | |{{HistoryLine||1.12|dev=pre1|Added {{cmd|function|link=none}}.}} | ||
|{{HistoryLine|||dev=pre4|Added new arguments to the {{ | |{{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||1.13|dev=17w49b|{{ | |{{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.20.2|dev=23w31a|Added function macro arguments.}} | ||
|{{HistoryLine||1.20.3|dev=23w41a|{{ | |{{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|||dev=23w44a|If {{ | |{{HistoryLine|||dev=23w44a|If {{cmd|link=none|function <''function tag''>}} runs multiple functions in combination with {{cmd|link=none|return run}}, execution stops after first {{cmd|link=none|return}} in any of the functions.|A single call to the {{cmd|link=none|function}} command always returns when run with {{cmd|link=none|return run}}. For example, {{cmd|link=none|return run execute ''[split context]'' run function ''<some function with conditional return>''}} always returns after processing the first context.}} | ||
|{{HistoryLine||1.20.5|dev=23w51a|Fixed a bug that <code>/function</code> executing a function with <code>/return fail</code> has a <code>success</code> value of 1.<ref>{{bug|MC-267193|||fixed}}</ref><ref>{{bug|MC-268160}}</ref>}} | |{{HistoryLine||1.20.5|dev=23w51a|Fixed a bug that <code>/function</code> executing a function with <code>/return fail</code> has a <code>success</code> value of 1.<ref>{{bug|MC-267193|||fixed}}</ref><ref>{{bug|MC-268160}}</ref>}} | ||
}} | }} | ||
| Line 116: | Line 116: | ||
=== ''Bedrock Edition'' === | === ''Bedrock Edition'' === | ||
{{HistoryTable | {{HistoryTable | ||
|{{ | |{{HistoryLine|bedrock}} | ||
|{{HistoryLine||1.8.0|dev=beta 1.8.0.8|Added {{ | |{{HistoryLine||1.8.0|dev=beta 1.8.0.8|Added {{cmd|function|link=none}}.}} | ||
}} | }} | ||
== See also == | == See also == | ||
* {{ | * {{cmd|tag}} – To run function in batch, every tick, or on server (re)load. | ||
== References == | == References == | ||
{{ | {{Reflist}} | ||
== External links == | == External links == | ||
| Line 130: | Line 130: | ||
== Navigation == | == Navigation == | ||
{{ | {{Navbox commands}} | ||
de:Befehl/function | [[Minecraft:de:Befehl/function]] | ||
es:Comandos/function | [[Minecraft:es:Comandos/function]] | ||
ja:コマンド/function | [[Minecraft:fr:Commandes/function]] | ||
ko:명령어/function | [[Minecraft:ja:コマンド/function]] | ||
pt:Comandos/function | [[Minecraft:ko:명령어/function]] | ||
ru:Команды консоли/function | [[Minecraft:pt:Comandos/function]] | ||
uk:Команди консолі/function | [[Minecraft:ru:Команды консоли/function]] | ||
zh:命令/function | [[Minecraft:uk:Команди консолі/function]] | ||
[[Minecraft:zh:命令/function]] | |||
Latest revision as of 11:03, 17 May 2026
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 Minecraft: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
- 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.mcfunctionin 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
- To run a function located at
History
Java Edition
Bedrock Edition
See also
- Template:Cmd – To run function in batch, every tick, or on server (re)load.
References
External links
Minecraft:de:Befehl/function Minecraft:es:Comandos/function Minecraft:fr:Commandes/function Minecraft:ja:コマンド/function Minecraft:ko:명령어/function Minecraft:pt:Comandos/function Minecraft:ru:Команды консоли/function Minecraft:uk:Команди консолі/function Minecraft:zh:命令/function