Minecraft:Commands/return: Difference between revisions
More actions
imported>Chixvv m →Usage |
Remove broken links to missing pages |
||
| Line 4: | Line 4: | ||
|oplevel=2 | |oplevel=2 | ||
}} | }} | ||
A command that can be embedded inside a | 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 {{cmd|function}} command as its [[Minecraft:Commands/function#Output|output values]], or the {{cmd|execute if function ...}} command to test. | ||
| Line 93: | Line 93: | ||
{{Navbox commands}} | {{Navbox commands}} | ||
de:Befehl/return | |||
ja:コマンド/return | |||
lzh:令/return | |||
pt:Comandos/return | |||
zh:命令/return | |||
Revision as of 15:45, 9 April 2026
Template: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 Template:Cmd command as its output values, or the Template:Cmd command to test.
It can also end a forking Template:Cmd 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 Template:Cmd command, the return value and successfulness are returned to the Template:Cmd command as its output values, and then can be stored using Template:Cmd.
- If the function is called by a Template:Cmd command, the return value is checked whether it is not
0.
Like other commands, following a Template:Cmd or a forking Template:Cmd 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 Template:Cmd command that has multiple branches, the Template:Cmd command also ends after the first execution of the Template:Cmd 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 Template:Cmd command that has multiple branches, the Template:Cmd 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 Template:Cmd command always executes, and it returns a failure and a 0 value when the Template:Cmd command terminates; While the Template:Cmd command in the second instance does not execute when the Template:Cmd command terminates.
Followed by a Template:Cmd command that calls multiple functions in a tag, the whole Template:Cmd command also ends after the first execution of the Template:Cmd command in any of the functions. See also the Template:Cmd article.
The Template:Cmd command sets the return value and the successfulness of a function if it is executed in the function. They are returned to the Template:Cmd or the Template:Cmd command that is calling the function. However, the Template:Cmd command itself also has its output values. Following a Template:Cmd command, its success value and result value of the Template:Cmd command can be stored while it ends the Template:Cmd command and the function. Directly executed in a command block, the success count of the Template:Cmd 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 Template:Cmd, only the first branch is executed.
Arguments
- An integer value to use as the return value of the Template:Cd.
- Template:Arg desc
- 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
Template:Output table Template:Output table Template:Output table Template:Output table Template:Output table Template:Output table
Above are the outputs of the Template:Cmd command itself. And the successfulness and the return value of the function are also set if the Template:Cmd command is executed in a function. The successfulness of the function equals to the success value of the Template:Cmd command. The return value of the function equals to the result value of the Template:Cmd command.
History
References
<references/>
de:Befehl/return ja:コマンド/return lzh:令/return pt:Comandos/return zh:命令/return