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/return: Difference between revisions

From SAS Gaming Wiki
SyncBot (talk | contribs)
Remove broken links to missing pages
SyncBot (talk | contribs)
Fix template calls: add MC/ prefix
 
Line 1: Line 1:
{{exclusive|java}}
{{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 {{cmd|function}} command as its [[Minecraft:Commands/function#Output|output values]], or the {{cmd|execute if function ...}} command to test.
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 {{cmd|execute}} command that has multiple [[Minecraft:Commands/execute#Subcommands and forking|branches]] at the first branch.
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 {{cmd|function}} command, the '''return value''' and '''successfulness''' are returned to the {{cmd|function}} command as its [[Minecraft:Commands/function#Output|output values]], and then can be stored using {{cmd|execute store}}.
* 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 {{cmd|execute if function}} command, the '''return value''' is checked whether it is not <code>0</code>.
* 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 {{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:
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 {{cmd|execute}} command that has multiple [[Minecraft:Commands/execute#Subcommands and forking|branches]], the {{cmd|execute}} command also ends after the first execution of the {{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.
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 {{cmd|execute}} command that has multiple [[Minecraft:Commands/execute#Subcommands and forking|branches]], the {{cmd|execute}} command also ends after the first branch execution.
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 {{cmd|return|link=none}} command always executes, and it returns a failure and a <code>0</code> value when the {{cmd|execute}} command terminates; While the {{cmd|return|link=none}} command in the second instance does not execute when the {{cmd|execute}} command terminates.
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 {{cmd|function}} command that calls multiple functions in a tag, the whole {{cmd|function}} command also ends after the first execution of the {{cmd|return|link=none}} command in any of the functions. See also the {{cmd|function}} article.
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 {{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 {{cmd|function}} or the {{cmd|execute if function ...}} command that is calling the function. However, the {{cmd|return|link=none}} command itself also has its [[#Output|output values]]. Following a {{cmd|execute store ... run}} command, its <code>success</code> value and <code>result</code> value of the {{cmd|return|link=none}} command can be stored while it ends the {{cmd|execute}} command and the function. Directly executed in a command block, the success count of the {{cmd|return|link=none}} command is recorded by the command block.
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 {{cmd|execute}}, only the first branch is executed.
: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 ==
{{argument|value|integer}}
{{MC/Argument|value|integer}}
: An integer value to use as the return value of the {{cd|function}}.
: An integer value to use as the return value of the {{MC/Cd|function}}.
: {{arg desc|je=integer}}
: {{MC/Arg desc|je=integer}}
{{argument|''command''}}
{{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 {{cd|<''command''>}} fails (If the command is an {{cmd|execute}} command, the first branch fails)}}
{{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 {{cd|<''command''>}} is {{cmd|execute}}, and the {{cmd|execute}} command [[Minecraft:Commands/execute#Subcommands and forking|terminates]]}}
{{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 {{cd|<''command''>}} is {{cmd|function}}, and the {{cmd|function}} command is [[Minecraft:Commands/function#Result|void]]}}
{{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 {{cd|<''command''>}} is {{cmd|execute ... run function}}, and the {{cmd|function}} command in the first branch of {{cmd|execute}} command is void}}
{{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 {{cd|<value>}}.}}
{{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 {{cd|<command>}}. (If the command is {{cmd|execute}}, the <code>result</code> value of the first branch)}}
{{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 {{cmd|return|link=none}} command itself. And the successfulness and the return value of the function are also set if the {{cmd|return|link=none}} command is executed in a function. The successfulness of the function equals to the <code>success</code> value of the {{cmd|return|link=none}} command. The return value of the function equals to the <code>result</code> value of the {{cmd|return|link=none}} command.
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 {{cd|return}}.}}
|{{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 {{cmd|link=none|return run}}.}}
|{{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 {{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.
|{{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 {{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.
|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>.
|{{cmd|link=none|return run}} now always returns. If there are no valid results from returned command, function containing {{cmd|link=none|return run}} fails.
|{{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.
|{{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 propagates success value together with results value (previously it always set success to <code>1</code>).
|{{cmd|link=none|return run}} now also allows storing values, which means {{cmd|link=none|execute store ... run return run ''some_command''}} both stores value and returns it outside function.}}
|{{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

  1. REDIRECT Template:Command

Template:Redr command as its output values, or the

  1. REDIRECT Template:Command

Template:Redr command to test.

It can also end a forking

  1. 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
  1. REDIRECT Template:Command

Template:Redr command, the return value and successfulness are returned to the

  1. REDIRECT Template:Command

Template:Redr command as its output values, and then can be stored using

  1. REDIRECT Template:Command

Template:Redr.

  • If the function is called by a
  1. 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

  1. 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">

  1. if (@s[tag=test1]) then return function("test1")

execute if entity @s[tag=test1] run return run function test1

  1. else if (@s[type=player]) then return function("test2")

execute if entity @s[type=player] run return run function test2

  1. else return function("test3")

return run function test3 </syntaxhighlight>

Following a forking

  1. REDIRECT Template:Command

Template:Redr command that has multiple branches, the

  1. REDIRECT Template:Command

Template:Redr command also ends after the first execution of the

  1. 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

  1. REDIRECT Template:Command

Template:Redr command that has multiple branches, the

  1. 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

  1. REDIRECT Template:Command

Template:Redr command always executes, and it returns a failure and a 0 value when the

  1. REDIRECT Template:Command

Template:Redr command terminates; While the

  1. REDIRECT Template:Command

Template:Redr command in the second instance does not execute when the

  1. REDIRECT Template:Command

Template:Redr command terminates.

Followed by a

  1. REDIRECT Template:Command

Template:Redr command that calls multiple functions in a tag, the whole

  1. REDIRECT Template:Command

Template:Redr command also ends after the first execution of the

  1. REDIRECT Template:Command

Template:Redr command in any of the functions. See also the

  1. REDIRECT Template:Command

Template:Redr article.

The

  1. 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

  1. REDIRECT Template:Command

Template:Redr or the

  1. REDIRECT Template:Command

Template:Redr command that is calling the function. However, the

  1. REDIRECT Template:Command

Template:Redr command itself also has its output values. Following a

  1. REDIRECT Template:Command

Template:Redr command, its success value and result value of the

  1. REDIRECT Template:Command

Template:Redr command can be stored while it ends the

  1. REDIRECT Template:Command

Template:Redr command and the function. Directly executed in a command block, the success count of the

  1. 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 the success value and result value of that command. If the command is a forking
  1. 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
  1. REDIRECT Template:Code

Template:Redr.

{{#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

  1. REDIRECT Template:Command

Template:Redr command itself. And the successfulness and the return value of the function are also set if the

  1. REDIRECT Template:Command

Template:Redr command is executed in a function. The successfulness of the function equals to the success value of the

  1. REDIRECT Template:Command

Template:Redr command. The return value of the function equals to the result value of the

  1. REDIRECT Template:Command

Template:Redr command.

History

Template:HistoryTable

References

<references/>

Navigation

All editions
Removed
Template:El only
Debug
Removed
Template:Els only
Template:El only
Removed
Template:El only
Removed
Template:Els only
Removed
Joke
Related

de:Befehl/return ja:コマンド/return lzh:令/return pt:Comandos/return zh:命令/return