# encountering different monsters

Whats the best way of calculating how rare a monster spawn is per area. Currently I’m working on a pokemon clone and each area would have something like 10 monsters. Some of the encounters will be very rare spawns so something like 7 of those monsters always spawn and then 3 would have a low chance to spawn. I want to add a variable to my monster class and then during an encounter I would search for each monster that’s in my current area, but then I want to take all of those monsters and somehow properly calculate which to spawn. Which is the part that confuses me. I could roll a random out of 100 and give each monster beforehand a low and high number, so if that random falls between the monsters number, then that’s the one encountered.

Maybe I’m making this too complex?

I would think you could use weighted probability to achieve this.
You could decide the probability of each monster spawning and just plug those numbers into the probability formula

EDIT:

Also weighted random selection from an array

I was thinking that I don’t even want to assign a specific number. Mostly just a enum with how rare each monster is such as common, uncommon, rare, legendary. Do you think you could explain what you were thinking of a little more?

Spawn is not required to be so complex.
Simply determine the kind of Perfab with its rare monster, and the number of monsters you want to instantiate.

I have the scripts, I just don’t know where I’ve been saving. I have to find the scripts.
Soon i find the project i send you if you like.

From what you said this is not what I’m trying to do. I’m only trying to spawn 1 single monster out of a group of monsters. Each monster has a different percentage to spawn, but only one is chosen. That rare monsters would have a very low chance to be picked out of the group.

There are multiple ways to do this. You could do a weighted probability as stated above or you could use a enum as stated but you’d still need to provide statistics so I believe a weighted probably is what you want. You could simply do:

[Pool of Objects] > [Choose a monster via probability] > [Spawn]

Monster A = 60% chance to spawn.
Monster B = 39 chance to spawn.
Monster C = 1% chance to spawn.

Is what I presume you want?

To go off what login4donald said, when you make an object pool you will tell the pool how many of the object you want to create. If you only create a small amount of the rare monster and a higher amount of less rare one, then just push them all into an array and then just grab a random number out of the list of then you have your rare spawn. But if you don’t need an object pool you can do something as simple as this,

``````public Enemys[] enemyPrefabs;
public RareEnemys[] rareEnemyPrefabs;

void SpawnEnemy()
{

int rareEnemyChance = Random.Range(0, 100);

//1% chance
if (rareEnemyChance == 10)
{
//Spawn rare enemy here
int enemySpawn = Random.Range(0, rareEnemyPrefabs.Length);
Spawn(enemySpawn);
}
else
{
int enemySpawn = Random.Range(0, enemyPrefabs.Length);
//Spawn normal enemies here
Spawn(enemySpawn);
}
}
``````

I guess this way is a lot easier than assigning a percent for each. Making it into two categories and just grabbing a random one out of that array after the random is rolled seems like a good choice. I think I shall go with your method. One last question would be if it’s possible to loop through an array and pool together objects depending on what the variable is set to? Such as this

``````Var region = "region1";
var monsters : Monsters[];
var chosenMonster : Monsters;

//after rolling to see if a rare or common is chosen then loop through and grab those speicic monsters, then spawn just 1.

for(var i=0;i<monsters.Length;i++) {

if(monsters[i].rarity == "rare"  monsters[i].regionLocated == "region1") {

//Add each monster to a Temp List
}

}

//After for loop take that Temp List and grab 1 monster from it at random and place it in chosenMonster variable.
``````

The best way to do this would be to have each enemy have a assigned region, then when you spawn an enemy check all the enemies with the appropriate region. Or you could simply just make different array’s for each region and the check what region your in and only spawn enemies from your array you made for that region.

Similar issue being resolved here: