How to handle multiple "monsters" in an RPG Game?

How do you handle monsters in an rpg?

Let’s say we are talking about a game like minecraft or wow, there are multiple monsters, sometimes even multiple of the same monster… like skeletal archers e.t.c

How do you know which instance of the monster class the player is attacking? I’m making an rpg, and i don’t know how you can tell which instance of the monster class the player is currently attacking… how would this work? Do these games get the x and y (and z) coordinates of the current object and then somehow determine which instance or object of a monster class the player is attacking?

How do you determine this? Any ideas?

All the entities in the game have the same problem, it’s easier for the player entity as you can leave a lot of the thinking to the player :slight_smile:

What I do it for each entity I get the collection of all entities that can be seen by that entity, and if any are enemies it may go over to its attack behavior. It then selects the most attackable enemy (nearest/weakest/most outnumbered) and moves into attack range.

The attack behavior holds a reference to the selected target so that I don’t have to recalculate the looking/selecting logic each frame. Every second or so (depending on how smart the creature is) it will reevaluate its behavior and possibly select a new target to attack. Or possibly run away!

Usually the game world does store monsters according to location, e.g. some tile-based games have a big “checkerboard” Tile[], where each Tile has a List.
So then when the player attacks a location:


Tile tileAttacked = tiles[locationToIndex(x, y)]; // get the tile we clicked on or whatever
for(Entity e : tileAttacked.entities) {               // inflict damage on entities that reside there
    e.damage(player.getStrength());
}

Or, less complicated, using no tiles (and more naive, but works fine for reasonable entity count):


List<Entity> entities; // all game monsters/entities

...

// on attack:
Point attackPoint; // the point we clicked (for 2D game)
for(Entity e : entities) {
    if(e.collides(attackPoint)) {
        e.damage(player.getStrength());
    }
}

These were extremely helpful thank you :slight_smile:

I realize now that monsters are stored by location… I think i can come up with something for this particular project to determine which monster is being attacked… that was my first thought… but i had to be sure… thank you guys.