0

I'm making a small game for android and currently use a pooling system e.g. for enemies, projectiles etc.

As for now I'm Async loading in levels as you play the game which means no loading scene when venturing into a new zone.

Pool is created when new game is started or old save is loaded. It works fine but leaves one issue! With no loading scenes while playing, there's no time to populate pool other than at the beginning. As player could potentially play all the way though in one sitting, that means all pooled objects needs to be loaded and instantiated.

I realize "Dynamically" filling a pool kinda miss the whole idea of pooling in the first place, but I'm curious what solutions people have come up with. Instantiating enemies at level 1 that won't see use until level 10 seem wasteful.

Cheers :)

Ills
  • 415
  • 1
  • 3
  • 13
  • How about pooling objects of the next level when loading the current level? And perhaps clearing the pooled objects of the previous level? – Immorality Apr 29 '19 at 09:38
  • I was thinking, that but this will need to be done during gameplay as new scenes area "streamed" in. From what I can read unity doesn't do async instantiation of objects, so populating the pool would very likely cause stuttering. – Ills Apr 29 '19 at 10:16
  • You could start a coroutine and spawn in the objects one by one in each frame, thus reducing the stuttering because you are also "streaming" the objects in. – Immorality Apr 29 '19 at 11:04
  • According to a unity dev., the actual instantiation of an object cannot happen async. Instead he refer to using coroutines on the actual instances themselves to lighten their weight upon creation. Its a rather old article from 2012 though, so things might've changed. https://forum.unity.com/threads/how-can-i-instantiate-a-gameobject-async.160737/ – Ills Apr 29 '19 at 11:35
  • And btw., thanks a lot for the replys =) – Ills Apr 29 '19 at 11:36

1 Answers1

0

To extend on my comments, I made an example which you could implement into your code. The enable, disable and OnLevelFinishedLoading should go somewhere into your Level/SceneManager class.

This should stream in the pooled objects whenever you load a new scene.

void OnEnable()
{   
    SceneManager.sceneLoaded += OnLevelFinishedLoading;
}

void OnDisable()
{  
    SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}

void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
{
    StartCoroutine(StartPoolingNextLevel(yourObjectPooler));
}

private IEnumerator StartPoolingNextLevel(YourObjectPoolerClass yourObjectPooler)
{
    for (int i = 0; i < yourObjectPooler.AmountToPool; i++)
    {
        yourObjectPooler.PoolItem(); // psuedo code
        yield return new WaitForEndOfFrame();
    }
}
Immorality
  • 2,164
  • 2
  • 12
  • 24
  • I still thought it would lock up the thread (even in a coroutine), especially once it starts calling the different awakes on materials etc. after instantiation. Will test once I get home and my phone hooked up =). I'll mark it as solved, thanks for the help :) – Ills Apr 29 '19 at 12:37