Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Best practice / correct way of handling objects from different lists back and forth

Discussion in 'Scripting' started by TheOnlyTruth, Jun 29, 2017.

  1. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    Hello everyone. I have a small problem that i can't seem to wrap my head around without making a super complex and probably stupid script. And im sure im missing something here - sorry, i am still pretty new :(

    I was making a system for an endless runner game. Ive ran into a lil bit of a tricky problem, and i'd like to ask for advice on handling objects from different lists.

    General situation (example):

    Endless runner game.

    5 Folders containing prefabs for X amount of GO's - the platforms to generate.
    Each folder represent different "levels" that change in difficulty.

    On start, i load arrays of GO's from each of the five folders : Array1, Array2 ... Array5.

    I create 3 clones of each prefab and put it in a list, each list contains 3x clones of each array element. So, List1, List2 ... List5.

    Now, i load the game.

    On start, a new list is created and populated with 7 platforms from List1. The list is called "PlatformsInUse" or whatever :) Those GO's are:
    Removed from list1.
    Added to "PlatformsInUse" list.
    Activated and placed in world space.

    Once they are no longer used, they are deactivated and removed from "Platformsinuse" and put back into the pool of objects that is currently used (in this case - List1).


    An update is called once every, lets say 1 second, checking the players score. Lets say player reached a milestone, 100 points.

    Now i want to start loading objects from a list of harder platforms. So i load them from List2.

    The problem is - there is still 6 platforms from List1 that are used, and as soon as i change the list of objects that i activate game objects from, the "deactivation" method will put last unused platform back from "PlatformsInUse" to list that i am currently taking paltforms from - in this case its going to be List2. Which should not contain lists from List1.

    What would be the best practice to identify the platforms original List without putting any scripts onto the platform prefabs themselves? And if i have to put scripts on them - maybe there is an easier way to manage these platforms?

    Maybe my logic is very flawed and i should find a different solution for this?

    Any help is greatly appreciated! :)
     
  2. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    Here is something I have done in the past. Don't know if it the nest way but it works great. I too create pool objects to pull from and have 3 different pools containing different objects. I make a GetObject function on each of the pools that will return an object to me that is not already active in the hierarchy. I have an pool controller which controls what gets pulled. It is the smart part of the operation. Because I pull directly from the pool I never move the list around. I just know what to work with by using the GameObject.activeInHierarchy call on the pooled object. I just keep looping till I hit one that is inactive and return it. So when it is done an inactive it goes back to the pool to be used again. Hope that makes sense and helps a little.

    -Alexander
     
    TheOnlyTruth likes this.
  3. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    Hello and thank you for your answer. Maybe you could explain steps and logic behind the system? I am still very new to this thing... Is my method isn't considered pooling? I'd greatly appreciate if you could explain your method to me as if I were an 8 year old :D
     
  4. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    For me this is a basic mob spawn pooler from a game I am working on.

    Code (CSharp):
    1. public class SpawnPooler : MonoBehaviour
    2.     {
    3.         public GameObject spawnMobs;
    4.         public int spawnDefaultAmount = 15;
    5.         public bool willGrow;
    6.         public int spawnPoolMax;
    7.  
    8.         public List<GameObject> spawnPool = new List<GameObject>();
    9.  
    10.         private void Start()
    11.         {
    12.             for (int cnt = 0; cnt <= spawnDefaultAmount; cnt++)
    13.             {
    14.                 GameObject o = Instantiate(spawnMobs);
    15.                 o.SetActive(false);
    16.                 spawnPool.Add(o);
    17.             }
    18.  
    19.             SpawnPoolController.current.AddSpawner(this);
    20.         }
    21.  
    22.         public virtual GameObject GetSpawnedMob()
    23.         {
    24.             for (int cnt = 0; cnt < spawnDefaultAmount; cnt++)
    25.             {
    26.                 if(!spawnPool[cnt].activeInHierarchy)
    27.                 {
    28.                     return spawnPool[cnt];
    29.                 }
    30.             }
    31.  
    32.             if(willGrow && spawnPool.Count <= spawnPoolMax)
    33.             {
    34.                 GameObject o = Instantiate(spawnMobs);
    35.                 o.SetActive(false);
    36.                 spawnPool.Add(o);
    37.                 return o;
    38.             }
    39.  
    40.             return null;
    41.         }
    42.     }
    Now you can see in my start function I first make all the objects I want to pool and then when that is done I register it with my spawn pool controller script which does all the work of picking which objects need to be pulled from which pools. Now my controller is simple cause all it does is pick a mob from a random pool that is registered with it then a random spawn point. The controller can be more complicated though to do things like what you are wanting.

    For instance you can have you poolers store more than one type of object then randomly pull items for whatever level of difficulty it is. Something like this maybe:

    Code (CSharp):
    1. if (score < 100)
    2. {
    3.   GameObject obj = spawnPoolers[0].GetRandomObject();
    4. }
    5. else if(score > 100 && score < 1000)
    6. {
    7.   GameObject obj = spawnPoolers[1].GetRandomObject();
    8. }
    9. else
    10. {
    11.     GameObject obj = spawnPoolers[2].GetRandomObject();
    12. }
    Again I'm no expert this is just the way with my current knowledge I would approach this.

    -Alexander