Minecraft:Commands/function: Difference between revisions
More actions
imported>Kestrer m Add link |
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 = | | others = Cheat only{{only|bedrock|short=1}} | ||
}} | }} | ||
| Line 10: | Line 10: | ||
== Usage == | == Usage == | ||
{{IN|java}}, runs a | {{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 | :: 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 | : 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 | {{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 | {{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 | ** 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 | |{{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}} | ||
de:Befehl/function | |||
es:Comandos/function | |||
ja:コマンド/function | |||
ko:명령어/function | |||
pt:Comandos/function | |||
ru:Команды консоли/function | |||
uk:Команди консолі/function | |||
zh:命令/function | |||
Revision as of 15:44, 9 April 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 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
de:Befehl/function es:Comandos/function ja:コマンド/function ko:명령어/function pt:Comandos/function ru:Команды консоли/function uk:Команди консолі/function zh:命令/function