Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlwaysSomething · Feb 03, 2014 at 06:24 AM · instantiateoptimizationmmorpg

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSomething · Feb 04, 2014 at 02:05 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges