I would like to count the number of entities of a given type in an area. Preferably by issuing a redstone signal proportional to that number.
I've seen that thread, that Arqade StackExchange answer and a couple of other similar resources, but they all are using the testfor command which was removed in MC1.13. I'm running MC1.14 Java Edition.
So in MC1.14, how to count entities in a given area?
/testfor is now a part of /execute and is used like this:
/execute if entity @e[type=sheep]
if you don't add a command to run at the end of the "if" statement, it will give out a comparator output if a comparator is attached to the command block.
A more reliable way is to use scoreboard objectives.
create an objective to hold the number of entities:
/scoreboard objectives add entity_count dummy
and then run this command to store the amount of sheep around you into your "entity_count" objective:
/execute store result score @s entity_count run execute if entity @e[type=sheep]
"store" is a subcommand of the /entity command which can be used to modify variables such as scores, entity data...
/testfor is now a part of /execute and is used like this:
/execute if entity @e[type=sheep]
if you don't add a command to run at the end of the "if" statement, it will give out a comparator output if a comparator is attached to the command block.
It is puzzling me, since when I run this command the output of an attached comparator doesn't seem to reflect the number of entities found. For example, in the image below, the comparator output power is 1 whereas the command output claims having found 2 entities.
A more reliable way is to use scoreboard objectives.
create an objective to hold the number of entities:
/scoreboard objectives add entity_count dummy
and then run this command to store the amount of sheep around you into your "entity_count" objective:
/execute store result score @s entity_count run execute if entity @e[type=sheep]
"store" is a subcommand of the /entity command which can be used to modify variables such as scores, entity data...
once you run this command, you can do:
/scoreboard players get @s entity_count
to show how many entities were counted.
I didn't look at scoreboards yet. But assuming I am using a scoreboard like you explained it, how can I get back a redstone power proportional to the stored value?
ATTACHMENTS
2019-08-28_19.38.49
Rollback Post to RevisionRollBack
Please, support the sledgehammer tool!
I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!
[...] I noticed the redstone signal strength did not match my entity count either.
Thank you for having taken the time to test that.
On my side, I noticed I can have an output signal strength matching the entity count if I use "execute as" and if the execute statement actually runs a command:
--ok--
execute as @e[type=villager,distance=..20] run me ""
execute as @e[type=villager,distance=..20] run execute if entity @s
--not ok--
execute if @e[type=villager,distance=..20]
execute as @e[type=villager,distance=..20]
execute as @e[type=villager,distance=..20] run
I suspect the output value is the sum of the output values of the commands: if you run for 5 entities a command returning 1, the result is 1+1+1+1+1 and that is what reflects the output signal strength. It is somewhat confirmed by the contrived command below:
[/p]
[p]execute as @e[type=villager,distance=..20] run execute if entity @s[tag=T][/p]
[p]
Here, the nested `execute` command is run once for each villager in the range. And that nested command will return either 0 or 1 depending if the villager is tagged. In my tests, the result value of the outer `execute` command is the number of tagged villagers.
On my side, I noticed I can have an output signal strength matching the entity count if I use "execute as" and if the execute statement actually runs a command:
--ok--
execute as @e[type=villager,distance=..20] run me ""
execute as @e[type=villager,distance=..20] run execute if entity @s
--not ok--
execute if @e[type=villager,distance=..20]
execute as @e[type=villager,distance=..20]
execute as @e[type=villager,distance=..20] run
I suspect the output value is the sum of the output values of the commands: if you run for 5 entities a command returning 1, the result is 1+1+1+1+1 and that is what reflects the output signal strength. It is somewhat confirmed by the contrived command below:
[/p]
[p]execute as @e[type=villager,distance=..20] run execute if entity @s[tag=T][/p]
[p]
Here, the nested `execute` command is run once for each villager in the range. And that nested command will return either 0 or 1 depending if the villager is tagged. In my tests, the result value of the outer `execute` command is the number of tagged villagers.
It's been a while since I've used a redstone signal as a command block output so I didn't know this worked that way. Thanks for the info
I would like to count the number of entities of a given type in an area. Preferably by issuing a redstone signal proportional to that number.
I've seen that thread, that Arqade StackExchange answer and a couple of other similar resources, but they all are using the testfor command which was removed in MC1.13. I'm running MC1.14 Java Edition.
So in MC1.14, how to count entities in a given area?
Please, support the sledgehammer tool!
I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!
/testfor is now a part of /execute and is used like this:
if you don't add a command to run at the end of the "if" statement, it will give out a comparator output if a comparator is attached to the command block.
A more reliable way is to use scoreboard objectives.
create an objective to hold the number of entities:
and then run this command to store the amount of sheep around you into your "entity_count" objective:
"store" is a subcommand of the /entity command which can be used to modify variables such as scores, entity data...
once you run this command, you can do:
to show how many entities were counted.
Command block engineer // Developer // #TeamTrees
Thanks a lot for your detailed reply Megacrafter107.
It is puzzling me, since when I run this command the output of an attached comparator doesn't seem to reflect the number of entities found. For example, in the image below, the comparator output power is 1 whereas the command output claims having found 2 entities.
I didn't look at scoreboards yet. But assuming I am using a scoreboard like you explained it, how can I get back a redstone power proportional to the stored value?
Please, support the sledgehammer tool!
I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!
I don't know the answer to that one as I noticed the redstone signal strength did not match my entity count either.
Command block engineer // Developer // #TeamTrees
Thank you for having taken the time to test that.
On my side, I noticed I can have an output signal strength matching the entity count if I use "execute as" and if the execute statement actually runs a command:
I suspect the output value is the sum of the output values of the commands: if you run for 5 entities a command returning 1, the result is 1+1+1+1+1 and that is what reflects the output signal strength. It is somewhat confirmed by the contrived command below:
Here, the nested `execute` command is run once for each villager in the range. And that nested command will return either 0 or 1 depending if the villager is tagged. In my tests, the result value of the outer `execute` command is the number of tagged villagers.
Please, support the sledgehammer tool!
I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!
It's been a while since I've used a redstone signal as a command block output so I didn't know this worked that way. Thanks for the info
Command block engineer // Developer // #TeamTrees