How can I target a random entity of any type using @r?

How can I target a random entity of any type using @r? - Goal Lettering Text on Black Background

While @r alone can only target player entities, adding the type parameter allows it to target other entity types:

/say @r[type=ArmorStand]

But because of this requirement, you'd only be able to target all except for a specific entity:

/say @r[type=!Player]

Attempting to use an invalid ID will throw an error, stating "Entity type [name] is invalid", rather than targeting all except that invalid type.

/say @r[type=!InvalidID]

How would I cause @r to target all possible entities without excluding any?



Best Answer

The type parameter grabs a list of string IDs from the same tab-complete list that /summon uses. If the input into the type parameter is not among that list, the selector will throw that error. The type parameter specifically appends "Player" to that list, which is why "Player" does not show up in /summon's tab-complete.

Among that list is "LightningBolt", which is not actually a valid savegame entity ID. Valid IDs are those that appear within the EntityList class mapping, which is what the tab-complete function will grab from before forcibly appending the list with "LightningBolt". It is essentially only appended for usability with /summon, which handles "LightningBolt" directly rather than through an automated process for any other input.

As such, you can safely use type=!LightningBolt. Lightning bolts themselves are not target-able in the first place. They are not added to the internal list of spawned entities, which is what entity selectors obtain targets from. Even if they were added to that list but remained non-savegame entities, "LightningBolt" is still not associated with them and the selector would target them regardless.

To reiterate: lightning bolts are not excluded by the selector because they are not included in the list of possible targets to begin with, nor is the ID "LightningBolt" associated with them in any way other than as a means of creation via /summon. It is simply impossible to target lightning bolts.

But since the type parameter believes that "LightningBolt" is a valid ID due to the /summon tab-complete list, it can be used without throwing an error. The following will target a random entity from all entities possible with selectors:

/say @r[type=!LightningBolt]

It will also target non-savegame entities such as fishing bobbers. While fishing bobbers do not have an ID at all, they do not have the "LightningBolt" ID which satisfies the selector. Fishing bobbers, unlike lightning bolts, are added to the list of spawned entities, which is why they can be targeted by selectors while lightning bolts cannot be.




Pictures about "How can I target a random entity of any type using @r?"

How can I target a random entity of any type using @r? - Unrecognizable person working on laptop at workplace
How can I target a random entity of any type using @r? - Concentrated black man working on laptop at home office
How can I target a random entity of any type using @r? - Crop unrecognizable man cleaning computer system unit



How do you target specific entities in Minecraft?

The @e target selector is used to target all entities in the game. This includes both players and mobs in the world. Notice that in this last example, we have used [type=enderman] to target only enderman. This value is called a target selector argument and allows us to filter the target.

How do you summon random entities in Minecraft?

Bedrock Edition can use @r to target non-player entities via the type selector argument; in Java Edition, to select a random entity, use @e[sort=random] instead. In Bedrock Edition, @r can only target entities who are alive.

How do you target a specific entity in Minecraft bedrock?

The other target selectors are \u201c@a\u201d for all players, \u201c@r\u201d for a random player, and \u201c@e\u201d will target all entities. Entities include everything that isn't a block, like monsters, snowballs, animals, and arrows.



Target Selectors | Minecraft Bedrock | Command Augments | dx, dy, dz, r, rm, type, family, l, lm etc




More answers regarding how can I target a random entity of any type using @r?

Answer 2

I have a solution!

First you have to write ArmorStand like this: armor_stand and to get it random: @e[type=armor_stand,sort=random,limit=1]

The limit = 1 limits it to only 1 entity which is exactly what you want.

Example: /execute as @e[sort=random,type=armor_stand,limit=1] at @s run summon minecraft:lightning_bolt

I hope I could help you :D

Edit: to target all entities just remove the type=armor_stand

Example: /execute as @e[sort=random,limit=1] at @s run summon minecraft:lightning_bolt

This only works in 1.13+!

Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Anna Tarazevich, Ketut Subiyanto, Andres Ayrton, Anete Lusina