   Creating MOBs on demand or at game initialization?
  Hi, I'm trying to write a simple MMORPG game with some friends and went into a discussion: should the MOBs (mosters) be created on demand or initialized when the server side load the game?For unique NPC, we can create individual instance for each such character.However, let's say we have 1000 monsters in certain area, should we do something like as below ?for(int i=0;i<1000;++i) { create_monster(moster_type);}Since all thest "monsters" are the same type and have the same name/HP/etc., players don't need to check its "individual status" unless they start "attacking" the beast. So should we create the instance only when the player start attacking? something like:if (player.attack(moster_type)){   newMonster = create_monster(moster_type);   do_fight(player, newMonster)}For sure we need to have some game logic to control how many monsters to "dynamically" create. I just wonder how this is implemented in commercial games, just create all MOBs at server initialization phase, or using the later approach? Thanks.   
  Hello,This depends much on how your game works, but I would suggest that the mobs should be created (instantiated) when they are inserted into your world.  You could certainly wait until a player tries to attack a mob before spawning it, but I think that's generally not the way most would do it.I'm assuming that as mobs die, new ones will replace them.  That implies that you will need to add mobs as the game runs.  Even if you spawn mobs when you start your server, you will need a thread or process in the background to generate more.You could spawn an initial amount at startup, and then respawn new ones as needed during execution.  Or you could just start the server with no mobs and spawn them all at runtime.  You could make an argument for either method.When my world first starts up there are no mobs, and I have a cap for the maximum number of mobs I want in the world.  I have a thread that runs in the background with a delay.  The further the current mob population is from the cap, the more aggressively it spawns new mobs.  When the server starts up it runs almost continuously spawning mobs while the population is far below the cap, and as the population approaches the cap the spawn rate slows.  When the current population exceeds the cap, I don't spawn any new mobs until some area destroyed.Regards   ;  There's a fun trick question in the Blizzard interview process that's vaguely related to this:"Let's say I'm running a successful MMO an we notice that there's a performance problem with AI.  How would you go about solving this problem"The answer: "Just buy more servers to run the AI.  There are more important things to worry about optimizing, like bandwidth and client-side or database problems.  Servers are cheap compared to everything else we do" [smile]-me   ;  I don't think you create/load mobs directly at all. You probably create mob spawners. A mob spawner is responsible for placing new mobs of a certain kind into the map, when some condition is fulfilled. Maybe every 10 minutes, if there isn't one of the right type alive already. Or make sure there's at least one kobold per player in the map. Or make sure that there are always 10 will-o-wisps within some specific area of the map.The configuration and location of these spawners is something that the map designer sets up, so I imagine they are parts of the map just like buildings and trees and whatnot. However, you don't need to load or worry about them on the client side, just when loading on the server.   ;Quote:Original post by likexx...I just wonder how this is implemented in commercial games...Many roads lead to Rome, and you should do it the way YOU can handle/implement it. A sophisticated example for hundreds of MOBs on one zone server follows:A MOB (mobile object) spawner (like hplus0603 described it) places mirrors of an existing MOB master template in the map. Mirror objects just consist of a position attribute and point to their master template for all other stuff, mesh for body, abilities and statistics. The spawner can have a simple AI routine called in the servers loop which lets roam some (not all at the same time!) of these mirror objects. When a player clicks such a mirror, he sees all of the according statistics (hitpoints etc.).Now if the player attacks that mirror, THEN an own instance of the MOB template gets created with his very own attributes and statistics (all things which can be influenced/changed by the fight) and especially an own AI routine. This AI let the monster attack the player, or the whole group of the player, or the one which causes the most harm (depending on AI). In this moment you have a lot of mirrors on the map, controlled by one MOB spawner with his one AI for all mirrors, and one real MOB entity with his own AI.When you cancel a fight (by running away from the attacked MOB) that MOB gets resetted. Full hitpoints, no buffs etc. The MOB floats back to his spawn point, a phase where you can't even select it. In this moment the real MOB entity you attacked got replaced by a new mirror handled by the MOB spawner. And the story starts from the beginning again ;-)   ;Quote:Original post by AnntorQuote:Original post by likexx...I just wonder how this is implemented in commercial games...Many roads lead to Rome, and you should do it the way YOU can handle/implement it. A sophisticated example for hundreds of MOBs on one zone server follows:A MOB (mobile object) spawner (like hplus0603 described it) places mirrors of an existing MOB master template in the map. Mirror objects just consist of a position attribute and point to their master template for all other stuff, mesh for body, abilities and statistics. The spawner can have a simple AI routine called in the servers loop which lets roam some (not all at the same time!) of these mirror objects. When a player clicks such a mirror, he sees all of the according statistics (hitpoints etc.).Now if the player attacks that mirror, THEN an own instance of the MOB template gets created with his very own attributes and statistics (all things which can be influenced/changed by the fight) and especially an own AI routine. This AI let the monster attack the player, or the whole group of the player, or the one which causes the most harm (depending on AI). In this moment you have a lot of mirrors on the map, controlled by one MOB spawner with his one AI for all mirrors, and one real MOB entity with his own AI.When you cancel a fight (by running away from the attacked MOB) that MOB gets resetted. Full hitpoints, no buffs etc. The MOB floats back to his spawn point, a phase where you can't even select it. In this moment the real MOB entity you attacked got replaced by a new mirror handled by the MOB spawner. And the story starts from the beginning again ;-)Thanks, that's something similar to what I'm thinking about right now.generally, I created a MobStub which contains only the ID for its template and a NULL pointer. If we define 1000 mobs in an area, there are 1000 stubs (each contains only an integer). When player starts attacking, create a new Mob and set the pointer in the stub, then every action later to the stub is done via the Mob pointer. And delete the pointer when the fight is done. Or maybe I can add a few more states in the stub (like the current HP, just another integer, so we know if it's dead or whatever).