1) Is there a way to stop execution of chained command blocks after the first failing command, without making all the subsequent blocks conditional?
In the first example, the commands are:
- execute if entity @e[type=minecraft:villager,tag=T,distance=..10] run say 1
- say 2
- say 3
If the first command block does not find a matching entity, the second command block is not executed since it is conditional. But the third one is no matter is the success state of the previous commands. So is there a way to forcibly stop execution of an arbitrary long chain of command after a command failure (if you are programming-language inclined, I'm looking for something like a break or exit statement)?
2) How to implement switch/conditional control structures (if ... else ... statement].
In the example 2, I would like either one branch of the chained commands or the other to be taken. But I have absolutely no idea how to achieve that.
there is no return/break statement when it comes to Minecraft's command system. The way the system works is it goes through every command and executes it. You have to specify which command will be executed and which command will be skipped by adding criteria to it.
the solution to your first question would be to add criteria to all the "say" commands just like you did with the first command.
Answer #2:
/execute has "if" and "unless" subcommands which you can use to achieve this. For example, if you want one branch to activate if there is a villager named "Villager_1" and the other branch to activate if there is a villager named "Villager_2", you would do it like this:
branch #1:
/execute if entity @e[type=villager,name=Villager_1] run <command_to_run_for_branch_1>
branch #2:
/execute if entity @e[type=villager,name=Villager_2] run <command_to_run_for_branch_2>
this will execute the command at the end of the criteria is met. Think of this as separating the "cases"
from the switch statement into separate if statements with the same criteria.
/execute has "if" and "unless" subcommands which you can use to achieve this. For example, if you want one branch to activate if there is a villager named "Villager_1" and the other branch to activate if there is a villager named "Villager_2", you would do it like this:
branch #1:
/execute if entity @e[type=villager,name=Villager_1] run <command_to_run_for_branch_1>
branch #2:
/execute if entity @e[type=villager,name=Villager_2] run <command_to_run_for_branch_2>
this will execute the command at the end of the criteria is met. Think of this as separating the "cases"
from the switch statement into separate if statements with the same criteria.
Yes, I saw the `if` and `unless` statements of the `execute` command. What was boring me is to repeat again and again the same condition for every command in each branch. But from your other answers, I'm afraid this is how it has to be done with the Minecraft's command system per se. I still wonder however if we can emulate more complex control flows by spawning blocks or "passing" redstone power to different command chains. Any thought or ideas about that?
Rollback Post to RevisionRollBack
Please, support the sledgehammer tool!
I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!
I still wonder however if we can emulate more complex control flows by spawning blocks or "passing" redstone power to different command chains. Any thought or ideas about that?
For fun I tried a couple of things, and end up with this design:
It works, but it is not very time efficient since branches execution is triggered on the falling edge of the redstone signal activating the "test" command (the `execute if` or `execute unless` command block on the foreground)
Two somewhat related questions:
1) Is there a way to stop execution of chained command blocks after the first failing command, without making all the subsequent blocks conditional?
In the first example, the commands are:
- execute if entity @e[type=minecraft:villager,tag=T,distance=..10] run say 1
- say 2
- say 3
If the first command block does not find a matching entity, the second command block is not executed since it is conditional. But the third one is no matter is the success state of the previous commands. So is there a way to forcibly stop execution of an arbitrary long chain of command after a command failure (if you are programming-language inclined, I'm looking for something like a break or exit statement)?
2) How to implement switch/conditional control structures (if ... else ... statement].
In the example 2, I would like either one branch of the chained commands or the other to be taken. But I have absolutely no idea how to achieve that.
Please, support the sledgehammer tool!
I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!
Answer #1:
there is no return/break statement when it comes to Minecraft's command system. The way the system works is it goes through every command and executes it. You have to specify which command will be executed and which command will be skipped by adding criteria to it.
the solution to your first question would be to add criteria to all the "say" commands just like you did with the first command.
Answer #2:
/execute has "if" and "unless" subcommands which you can use to achieve this. For example, if you want one branch to activate if there is a villager named "Villager_1" and the other branch to activate if there is a villager named "Villager_2", you would do it like this:
branch #1:
branch #2:
this will execute the command at the end of the criteria is met. Think of this as separating the "cases"
from the switch statement into separate if statements with the same criteria.
Command block engineer // Developer // #TeamTrees
Yes, I saw the `if` and `unless` statements of the `execute` command. What was boring me is to repeat again and again the same condition for every command in each branch. But from your other answers, I'm afraid this is how it has to be done with the Minecraft's command system per se. I still wonder however if we can emulate more complex control flows by spawning blocks or "passing" redstone power to different command chains. Any thought or ideas about that?
Please, support the sledgehammer tool!
I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!
For fun I tried a couple of things, and end up with this design:
It works, but it is not very time efficient since branches execution is triggered on the falling edge of the redstone signal activating the "test" command (the `execute if` or `execute unless` command block on the foreground)
Please, support the sledgehammer tool!
I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!