- Home /
MMORPG Nearby Mobs - Instantiate?
I have a server set up outside of Unity. It sends the unique ID, location, type, etc of mobs nearby the player, which is read into a list of NPCs on the client. All of this is working with code. I'm wondering what the most efficient / best practice is for Unity when it comes to graphically displaying the nearby mobs (while keeping each referenced to its unique stats such as HP for health bars, etc). Then, for removing/deleting them (graphically) when they are no longer nearby (or when they are no longer part of the nearbyNPC list). Given the quantity of mobs which may be present (and being killed) at any time in an MMORPG, optimization is quite important.
For a brief overview of the code:
Global.cs
public static class Global
{
//...
public static Data data
//...
}
Data.cs
public class Data
{
//...
public System.Collections.ArrayList nearby_npcs
//...
}
NPCInfo.cs
public class NPCInfo
{
public uint uniqueID;
public uint NPCType;
public int hp;
public float x, y, z;
//etc
}
((NPCInfo)Global.data.nearby_npcs[i]).hp; //gets the hp of nearbymob [i], for an example use.
Hopefully that helps with understanding what I'm doing.
Your assistance and tips would be much appreciated.
Answer by Narv · Feb 03, 2014 at 05:46 PM
you could instantiate the mob and add it to the nearby npc list and set the active / inactive state of it which will in essence enable / disable the mesh renderer (since you kept saying graphically). this will disable the scripts associated with the npcs so if you need to access that you can just make the npc invisible by disabling just the mesh renderer.
I assume you have a method of garbage collection on the npc list to keep it from getting out of hand and removing destroyed NPCs from the list.
Not sure if this is what you were looking for as the question wasn't 100% clear to me.
Essentially I'm doing it backwards compared to what seems to be the most common practice here; I have the npc with its stats already created and in the nearby_npcs list, /then/ I want to instantiate it as a GameObject, retaining a reference to the unique stats it should be using such as current HP. To reiterate, all of this is working 100% with code only. I'm porting it over to Unity now for something more visual.
I could run a for-loop in a background thread* to iterate through every npc in the list, instantiating a prefab of type "((NPCInfo)Global.data.nearby_npcs[i]).NPCType" at the appropriate position and rotation, then flag (and check) those NPCs as being already instantiated. I would then have to have a script on the prefab to store the mob's uniqueID, and any actions related to that NPC would have to iterate through the entire nearby_npcs list to match the uniqueID. To update a mob's position, for example, a script on the prefab could contain:
public bool isNearby;
public uint uID = 1234567; //set by another script after Instantiate(), then this script is enabled
void Update
{
isNearby = false;
for(int i = 0; i < Global.data.nearby_npcs.Count; i++)
{
if (uID == ((NPCInfo)Global.data.nearby_npcs[i]).uniqueID)
{
transform.position = new Vector3( ((NPCInfo)Global.data.nearby_npcs[i]).x, ((NPCInfo)Global.data.nearby_npcs[i]).y, ((NPCInfo)Global.data.nearby_npcs[i]).z );
isNearby = true;
}
}
if (!isNearby)
Destroy(gameObject);
}
Alternatively I could have one gamemanager script do the same task, but for all NPCs using one loop ins$$anonymous$$d of one loop per NPC, reducing excess computation. I'm looking for the most efficient way to handle the task of displaying and updating NPCs based on their stats stored in the nearby_npcs list.
*It seems instantiating in a background thread is not possible: Instantiate() can only be called from the main thread.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132409im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unlimited Blood on Floor 1 Answer
Photon Instantiate 2 Answers
Code Optimization Question 1 Answer
[UNET] Optimizing Commands 0 Answers
Checking if object intersects? 1 Answer