• Hey! Register here to create your account, engage with the community, and talk about what is new!

How to spawn mobs in a random position?

TVTRex

New member
Joined
Dec 27, 2023
Messages
19
Reaction score
0
How do I spawn a mob in a random set position?
 
Solution
Off-topic, but this is my 100th message! 🥳



First, create a list containing all the locations where you want to spawn mobs. We'll temporarily call this list 'SpawnLocations'. At this point, you should have a list with a few (or more) locations. To get a random location from that list, we first need to find the list's length. You can use the `Set Variable: Get List Length` code block for this. In the chest, enter a variable (we'll call it 'ListLength' for this explanation), and then input the 'SpawnLocations' variable as well.

1727934224823.png



Now, we’ll actually get a random location from that list. To do this, place a `Set Variable: Get List Value` code block. In the first slot, enter a variable. Name it...

CoderElias

Notorious member
Overlord
Joined
Jun 15, 2022
Messages
101
Reaction score
17
Off-topic, but this is my 100th message! 🥳



First, create a list containing all the locations where you want to spawn mobs. We'll temporarily call this list 'SpawnLocations'. At this point, you should have a list with a few (or more) locations. To get a random location from that list, we first need to find the list's length. You can use the `Set Variable: Get List Length` code block for this. In the chest, enter a variable (we'll call it 'ListLength' for this explanation), and then input the 'SpawnLocations' variable as well.

1727934224823.png



Now, we’ll actually get a random location from that list. To do this, place a `Set Variable: Get List Value` code block. In the first slot, enter a variable. Name it anything you want (for this explanation, I'll call it 'LocationOutput'). This variable will store the randomly selected location. In the next slot, input the 'SpawnLocations' variable. Then, in the following slot, add a number. However, this can't be just any number. If it were a set number, the location wouldn't be random. Instead, we’ll use this command to get a specific number value:
/num %random(1,%var(ListLength))
By running this command you'll get a number which you must put in the third slot of the `Set Variable: Get List Value`. If you want to know more about how this works, click the following spoiler:
If you use %random() instead of a set number, it will generate a random number within the range specified inside the brackets. For example, %random(1,5) will return a random number between 1 and 5. This helps you avoid adding an extra code block. Since we want to get a random number between 1 and the length of the 'SpawnLocations' list, you can use %var(ListLength) to represent that length. However, if you prefer using a code block for this, it’s called `Set Variable: Set To Random Number`.

1727963511461.png


1727934921405.png



After all this you can spawn a mob, and instead of inserting a set location you can insert the 'LocationOutput' variable.

All this should ensure you get a random location from a list of predefined locations, if you have any further questions please reply!
 
Last edited:
Solution
Top Bottom