Minecraft:Commands/return: Difference between revisions
More actions
Remove broken links to missing pages |
Fix template calls: add MC/ prefix |
||
| Line 1: | Line 1: | ||
{{ | {{MC/Exclusive|java}} | ||
{{Infobox command | {{Infobox command | ||
|name=return | |name=return | ||
| Line 6: | Line 6: | ||
A command that can be embedded inside a function to control its execution. | A command that can be embedded inside a function to control its execution. | ||
It ends function execution and sets the successfulness and the return value of the function, which is returned to the {{ | It ends function execution and sets the successfulness and the return value of the function, which is returned to the {{MC/Cmd|function}} command as its [[Minecraft:Commands/function#Output|output values]], or the {{MC/Cmd|execute if function ...}} command to test. | ||
It can also end a forking {{ | It can also end a forking {{MC/Cmd|execute}} command that has multiple [[Minecraft:Commands/execute#Subcommands and forking|branches]] at the first branch. | ||
== Usage == | == Usage == | ||
By placing this command in a function, the function can be stopped at a certain line, and the return value and the successfulness of the function are set. | By placing this command in a function, the function can be stopped at a certain line, and the return value and the successfulness of the function are set. | ||
* If the function is called by a {{ | * If the function is called by a {{MC/Cmd|function}} command, the '''return value''' and '''successfulness''' are returned to the {{MC/Cmd|function}} command as its [[Minecraft:Commands/function#Output|output values]], and then can be stored using {{MC/Cmd|execute store}}. | ||
* If the function is called by a {{ | * If the function is called by a {{MC/Cmd|execute if function}} command, the '''return value''' is checked whether it is not <code>0</code>. | ||
Like other commands, following a {{cmd|execute (if{{!}}unless) ... run}} or a forking {{ | Like other commands, following a {{cmd|execute (if{{!}}unless) ... run}} or a forking {{MC/Cmd|execute}} command that may [[Minecraft:Commands/execute#Subcommands and forking|terminate]], this command can be restricted to only execute under certain conditions. With this, under different conditions a function can end at different lines, thus achieving more complex behaviors. For example, the following function simulates an if-else statement: | ||
<syntaxhighlight lang="mcfunction"> | <syntaxhighlight lang="mcfunction"> | ||
# if (@s[tag=test1]) then return function("test1") | # if (@s[tag=test1]) then return function("test1") | ||
| Line 25: | Line 25: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Following a forking {{ | Following a forking {{MC/Cmd|execute}} command that has multiple [[Minecraft:Commands/execute#Subcommands and forking|branches]], the {{MC/Cmd|execute}} command also ends after the first execution of the {{MC/Cmd|return|link=none}} command. So, only the first branch is executed. This can be used to ensure a command is executed only once. For example: <code>/execute as @e[type=zombie,sort=nearest] at @s if block ~ ~-1 ~ grass_block run '''return run''' tp @s Steve</code> teleports the nearest one zombie that is on a grass block to Steve, if such a zombie exists. Note this command in a function also ends the function. | ||
Followed by a forking {{ | Followed by a forking {{MC/Cmd|execute}} command that has multiple [[Minecraft:Commands/execute#Subcommands and forking|branches]], the {{MC/Cmd|execute}} command also ends after the first branch execution. | ||
The only difference between <code>return run execute ... run <''command''></code> and <code>execute ... run return run <''command''></code> is that in the first instance, the {{ | The only difference between <code>return run execute ... run <''command''></code> and <code>execute ... run return run <''command''></code> is that in the first instance, the {{MC/Cmd|return|link=none}} command always executes, and it returns a failure and a <code>0</code> value when the {{MC/Cmd|execute}} command terminates; While the {{MC/Cmd|return|link=none}} command in the second instance does not execute when the {{MC/Cmd|execute}} command terminates. | ||
Followed by a {{ | Followed by a {{MC/Cmd|function}} command that calls multiple functions in a tag, the whole {{MC/Cmd|function}} command also ends after the first execution of the {{MC/Cmd|return|link=none}} command in any of the functions. See also the {{MC/Cmd|function}} article. | ||
The {{ | The {{MC/Cmd|return|link=none}} command sets the return value and the successfulness of a function if it is executed in the function. They are returned to the {{MC/Cmd|function}} or the {{MC/Cmd|execute if function ...}} command that is calling the function. However, the {{MC/Cmd|return|link=none}} command itself also has its [[#Output|output values]]. Following a {{MC/Cmd|execute store ... run}} command, its <code>success</code> value and <code>result</code> value of the {{MC/Cmd|return|link=none}} command can be stored while it ends the {{MC/Cmd|execute}} command and the function. Directly executed in a command block, the success count of the {{MC/Cmd|return|link=none}} command is recorded by the command block. | ||
== Syntax == | == Syntax == | ||
| Line 41: | Line 41: | ||
:Ends the function with a failure and a return value of <code>0</code>. | :Ends the function with a failure and a return value of <code>0</code>. | ||
<code>return run <''command''></code> | <code>return run <''command''></code> | ||
:Executes the command specified in <code><''command''></code>, ends the function to which it belongs, and sets the '''successfulness''' and '''return value''' of the function to the '''<code>success</code> value''' and '''<code>result</code> value''' of that command. If the command is a forking {{ | :Executes the command specified in <code><''command''></code>, ends the function to which it belongs, and sets the '''successfulness''' and '''return value''' of the function to the '''<code>success</code> value''' and '''<code>result</code> value''' of that command. If the command is a forking {{MC/Cmd|execute}}, only the first branch is executed. | ||
== Arguments == | == Arguments == | ||
{{ | {{MC/Argument|value|integer}} | ||
: An integer value to use as the return value of the {{ | : An integer value to use as the return value of the {{MC/Cd|function}}. | ||
: {{ | : {{MC/Arg desc|je=integer}} | ||
{{ | {{MC/Argument|''command''}} | ||
: A command to execute. | : A command to execute. | ||
| Line 54: | Line 54: | ||
{{Result table|The command is incomplete, or any argument is not specified correctly.|unparseable}} | {{Result table|The command is incomplete, or any argument is not specified correctly.|unparseable}} | ||
{{Result table|cmd=/return fail|Always.|fail}} | {{Result table|cmd=/return fail|Always.|fail}} | ||
{{Result table|cmd=/return run <''command''>|the {{ | {{Result table|cmd=/return run <''command''>|the {{MC/Cd|<''command''>}} fails (If the command is an {{MC/Cmd|execute}} command, the first branch fails)}} | ||
{{Result table|the {{ | {{Result table|the {{MC/Cd|<''command''>}} is {{MC/Cmd|execute}}, and the {{MC/Cmd|execute}} command [[Minecraft:Commands/execute#Subcommands and forking|terminates]]}} | ||
{{Result table|the {{ | {{Result table|the {{MC/Cd|<''command''>}} is {{MC/Cmd|function}}, and the {{MC/Cmd|function}} command is [[Minecraft:Commands/function#Result|void]]}} | ||
{{Result table|the {{ | {{Result table|the {{MC/Cd|<''command''>}} is {{MC/Cmd|execute ... run function}}, and the {{MC/Cmd|function}} command in the first branch of {{MC/Cmd|execute}} command is void}} | ||
{{Result table|cmd=|succeed}} | {{Result table|cmd=|succeed}} | ||
{{Result table}} | {{Result table}} | ||
== Output == | == Output == | ||
{{Output table|onlyje=1}} | {{MC/Output table|onlyje=1}} | ||
{{Output table|edition=je|cmd=/return <value>|On success|1|1|the value of {{ | {{Output table|edition=je|cmd=/return <value>|On success|1|1|the value of {{MC/Cd|<value>}}.}} | ||
{{Output table|cmd=/return fail|On fail|0|0|0}} | {{MC/Output table|cmd=/return fail|On fail|0|0|0}} | ||
{{Output table|cmd=/return run <''command''>|On fail|0|0|0}} | {{MC/Output table|cmd=/return run <''command''>|On fail|0|0|0}} | ||
{{Output table|On success|1|1|the <code>result</code> value of {{ | {{Output table|On success|1|1|the <code>result</code> value of {{MC/Cd|<command>}}. (If the command is {{MC/Cmd|execute}}, the <code>result</code> value of the first branch)}} | ||
{{Output table}} | {{MC/Output table}} | ||
Above are the outputs of the {{ | Above are the outputs of the {{MC/Cmd|return|link=none}} command itself. And the successfulness and the return value of the function are also set if the {{MC/Cmd|return|link=none}} command is executed in a function. The successfulness of the function equals to the <code>success</code> value of the {{MC/Cmd|return|link=none}} command. The return value of the function equals to the <code>result</code> value of the {{MC/Cmd|return|link=none}} command. | ||
== History == | == History == | ||
{{HistoryTable | {{HistoryTable | ||
|{{HistoryLine|java}} | |{{MC/HistoryLine|java}} | ||
|{{HistoryLine||1.20|dev=23w16a|Added {{ | |{{HistoryLine||1.20|dev=23w16a|Added {{MC/Cd|return}}.}} | ||
|{{HistoryLine||1.20.2|dev=23w31a|Added a new subcommand <code>run</code>.}} | |{{MC/HistoryLine||1.20.2|dev=23w31a|Added a new subcommand <code>run</code>.}} | ||
|{{HistoryLine|||dev=1.20.2 Pre-release 1|Temporarily removed syntax {{ | |{{HistoryLine|||dev=1.20.2 Pre-release 1|Temporarily removed syntax {{MC/Cmd|link=none|return run}}.}} | ||
|{{HistoryLine||1.20.3|dev=23w41a|Reintroduced subcommand <code>run</code>.}} | |{{MC/HistoryLine||1.20.3|dev=23w41a|Reintroduced subcommand <code>run</code>.}} | ||
|{{HistoryLine|||dev=23w44a|If {{ | |{{HistoryLine|||dev=23w44a|If {{MC/Cmd|link=none|function <''function tag''>}} runs multiple functions in combination with {{MC/Cmd|link=none|return run}}, execution stops after first {{MC/Cmd|link=none|return}} in any of the functions. | ||
|A single call to the {{ | |A single call to the {{MC/Cmd|link=none|function}} command always returns when run with {{MC/Cmd|link=none|return run}}. For example, {{MC/Cmd|link=none|return run execute ''[split context]'' run function ''<some function with conditional return>''}} always returns after processing the first context. | ||
|Added subcommand <code>fail</code>. | |Added subcommand <code>fail</code>. | ||
|{{ | |{{MC/Cmd|link=none|return run}} now always returns. If there are no valid results from returned command, function containing {{MC/Cmd|link=none|return run}} fails. | ||
|{{ | |{{MC/Cmd|link=none|return run}} now propagates success value together with results value (previously it always set success to <code>1</code>). | ||
|{{ | |{{MC/Cmd|link=none|return run}} now also allows storing values, which means {{MC/Cmd|link=none|execute store ... run return run ''some_command''}} both stores value and returns it outside function.}} | ||
|{{HistoryLine||1.20.5|dev=23w51a|Fixed a bug that <code>/return run function <function></code> does not return under some conditions.<ref>{{bug|MC-267194|||fixed}}</ref>}} | |{{HistoryLine||1.20.5|dev=23w51a|Fixed a bug that <code>/return run function <function></code> does not return under some conditions.<ref>{{bug|MC-267194|||fixed}}</ref>}} | ||
}} | }} | ||
| Line 91: | Line 91: | ||
== Navigation == | == Navigation == | ||
{{Navbox commands}} | {{MC/Navbox commands}} | ||
de:Befehl/return | de:Befehl/return | ||
Latest revision as of 20:13, 9 April 2026
Script error: No such module "Exclusive". Template:Infobox command A command that can be embedded inside a function to control its execution.
It ends function execution and sets the successfulness and the return value of the function, which is returned to the
- REDIRECT Template:Command
Template:Redr command as its output values, or the
- REDIRECT Template:Command
Template:Redr command to test.
It can also end a forking
- REDIRECT Template:Command
Template:Redr command that has multiple branches at the first branch.
Usage
By placing this command in a function, the function can be stopped at a certain line, and the return value and the successfulness of the function are set.
- If the function is called by a
- REDIRECT Template:Command
Template:Redr command, the return value and successfulness are returned to the
- REDIRECT Template:Command
Template:Redr command as its output values, and then can be stored using
- REDIRECT Template:Command
- If the function is called by a
- REDIRECT Template:Command
Template:Redr command, the return value is checked whether it is not 0.
Like other commands, following a Template:Cmd or a forking
- REDIRECT Template:Command
Template:Redr command that may terminate, this command can be restricted to only execute under certain conditions. With this, under different conditions a function can end at different lines, thus achieving more complex behaviors. For example, the following function simulates an if-else statement: <syntaxhighlight lang="mcfunction">
- if (@s[tag=test1]) then return function("test1")
execute if entity @s[tag=test1] run return run function test1
- else if (@s[type=player]) then return function("test2")
execute if entity @s[type=player] run return run function test2
- else return function("test3")
return run function test3 </syntaxhighlight>
Following a forking
- REDIRECT Template:Command
Template:Redr command that has multiple branches, the
- REDIRECT Template:Command
Template:Redr command also ends after the first execution of the
- REDIRECT Template:Command
Template:Redr command. So, only the first branch is executed. This can be used to ensure a command is executed only once. For example: /execute as @e[type=zombie,sort=nearest] at @s if block ~ ~-1 ~ grass_block run return run tp @s Steve teleports the nearest one zombie that is on a grass block to Steve, if such a zombie exists. Note this command in a function also ends the function.
Followed by a forking
- REDIRECT Template:Command
Template:Redr command that has multiple branches, the
- REDIRECT Template:Command
Template:Redr command also ends after the first branch execution.
The only difference between return run execute ... run <command> and execute ... run return run <command> is that in the first instance, the
- REDIRECT Template:Command
Template:Redr command always executes, and it returns a failure and a 0 value when the
- REDIRECT Template:Command
Template:Redr command terminates; While the
- REDIRECT Template:Command
Template:Redr command in the second instance does not execute when the
- REDIRECT Template:Command
Template:Redr command terminates.
Followed by a
- REDIRECT Template:Command
Template:Redr command that calls multiple functions in a tag, the whole
- REDIRECT Template:Command
Template:Redr command also ends after the first execution of the
- REDIRECT Template:Command
Template:Redr command in any of the functions. See also the
- REDIRECT Template:Command
Template:Redr article.
The
- REDIRECT Template:Command
Template:Redr command sets the return value and the successfulness of a function if it is executed in the function. They are returned to the
- REDIRECT Template:Command
Template:Redr or the
- REDIRECT Template:Command
Template:Redr command that is calling the function. However, the
- REDIRECT Template:Command
Template:Redr command itself also has its output values. Following a
- REDIRECT Template:Command
Template:Redr command, its success value and result value of the
- REDIRECT Template:Command
Template:Redr command can be stored while it ends the
- REDIRECT Template:Command
Template:Redr command and the function. Directly executed in a command block, the success count of the
- REDIRECT Template:Command
Template:Redr command is recorded by the command block.
Syntax
return <value>
- Ends the function to which it belongs with a success, and sets the return value of the function to the specified integer value.
return fail
- Ends the function with a failure and a return value of
0.
return run <command>
- Executes the command specified in
<command>, ends the function to which it belongs, and sets the successfulness and return value of the function to thesuccessvalue andresultvalue of that command. If the command is a forking
- REDIRECT Template:Command
Template:Redr, only the first branch is executed.
Arguments
<value>: integer
- An integer value to use as the return value of the
- REDIRECT Template:Code
- {{#vardefine:onlyje|}}{{#vardefine:onlybe| Template:IN,}}{{#vardefine:je-arg|integer}}{{#vardefine:be-arg|}}Template:Error{{#vardefine:onlyje|}}{{#vardefine:onlybe|}}{{#vardefine:je-arg|}}{{#vardefine:be-arg|}}
<command>
- A command to execute.
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
Output
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-table-lines|-2}}{{#vardefine:output-table-lines-now|}}{{#vardefine:command-num-now|}}{{#vardefine:command-line-now|}}{{#vardefine:commands-num|}}{{#vardefine:output-edition-num-now|}}{{#vardefine:output-edition-line-now|}}{{#vardefine:output-editions-num|}}{{#vardefine:je-outputs-dis|}}
Template:Output table
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On fail}}{{#vardefine:output1-{{#var:output-table-lines}}|0}}{{#vardefine:output2-{{#var:output-table-lines}}|0}}{{#vardefine:output3-{{#var:output-table-lines}}|0}}{{#vardefine:commands-num|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:command-{{#var:commands-num}}|/return fail}}{{#vardefine:command-row-{{#var:commands-num}}|1}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|Expression error: Unrecognized punctuation character "{".}}
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On fail}}{{#vardefine:output1-{{#var:output-table-lines}}|0}}{{#vardefine:output2-{{#var:output-table-lines}}|0}}{{#vardefine:output3-{{#var:output-table-lines}}|0}}{{#vardefine:commands-num|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:command-{{#var:commands-num}}|/return run <command>}}{{#vardefine:command-row-{{#var:commands-num}}|1}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|Expression error: Unrecognized punctuation character "{".}}
Template:Output table
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-table-lines|-2}}{{#vardefine:output-table-lines-now|}}{{#vardefine:command-num-now|}}{{#vardefine:command-line-now|}}{{#vardefine:commands-num|}}{{#vardefine:output-edition-num-now|}}{{#vardefine:output-edition-line-now|}}{{#vardefine:output-editions-num|}}{{#vardefine:je-outputs-dis|}}
Above are the outputs of the
- REDIRECT Template:Command
Template:Redr command itself. And the successfulness and the return value of the function are also set if the
- REDIRECT Template:Command
Template:Redr command is executed in a function. The successfulness of the function equals to the success value of the
- REDIRECT Template:Command
Template:Redr command. The return value of the function equals to the result value of the
- REDIRECT Template:Command
Template:Redr command.
History
References
<references/>
de:Befehl/return ja:コマンド/return lzh:令/return pt:Comandos/return zh:命令/return