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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Is pooling worth the hussle for small games?

Discussion in 'Editor & General Support' started by Oshigawa, Sep 22, 2018.

  1. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    Hi guys,

    Before i started working on anything i Unity almost three years ago i checked for some basic things to look out for so i don't get stuck with a big problem during the development that would be hard to fix. Pooling was among them. Considering i am making a shoot 'em up (which means lots of bullets and explosions, but not bullet hell - think something like Steredenn), i simply resorted to pooling almost everything without much further though. For clarity in further text, spawning means creating from pool, squadron is a parent game object, it holds ships which are children game objects.

    Before nested prefabs came, i made squadrons as an empty game object which would spawn the ships as its children (because they also have their own children objects such as guns, jets and so on) and all behaved well - when they were destroyed, they despawned and got back to the pool.

    The performance was good, there were no jitters so i was quite happy.

    But the work on those squadrons was tedious, i could not just drag and drop the ships and save the prefab, but i had to preset their spawn position by manually typing in the coordinates. In that regard, nested prefabs came as a blessing since i could visually put the ships in their places in squadrons and then simply save the complete squadron prefab and spawn it.

    But with that, a huge problem arises. When i spawn the squadron with ships that are nested, ships are not considered spawned, and therefore they can't be despawned. If i understand well what's going on, squadron itself with all containing ships IS pooled, but ships themselves aren't (sounds crazy, but i'm not much of a programmer to explain it properly, i just see what's going on, spawned ships are not from the pool, i use Core Gamekit for pooling).

    So, it looks to me that i have three solutions: get back to the way i worked before, pool and spawn squadrons which spawn ships on predefined positions, which spawn jets, guns and all other elements. It works perfectly but it is time consuming and error prone to do things that way.

    Second solution would be hybrid - pool stuff that appears a lot and is simple (bullets, explosions, asteroids) and instantiate squadrons containing ships. Though it's probably the best of both worlds, the potential downside i see is confusion in the work, since some objects will be spawned/despawned and some will be instantiated/destroyed and being prone to errors of course.

    Third solution is to use destroy/instantiate for everything.

    Of course, i'd really like to be able to pool all the elements of nested prefabs, so first of all, i wanted to ask you if there is a way to spawn the ships that would otherwise be instantiated in the case i wrote about?

    Second, is instantiating and deinstantiating THAT bad? Excluding maybe bullets, would there be stuttering from GC? Maybe timing the garbage collection between levels would do the trick?

    Here's how the game looks, just so you get the number of objects on screen



    Any help is welcome, thanks.
     
    Last edited: Sep 22, 2018
  2. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,571
    As with many things, it all depends on your game. I did a couple of shoot 'em ups with no pooling, I'm doing a puzzle game with almost everything pooled.

    The shoot 'em ups were the first games I programmed (having been a games designer before with no requirement for programming), so part of the reason for not pooling was being unsure of knowing if I could get it to work reliably. The other part was I already had it working at 60FPS on an iPod Touch 1st gen, so there was no need to do anything else. I did do a GC collect between levels to ensure there were no stutters in a level.

    I do plan on doing another shoot 'em up. I am planning on pooling bullets, but not enemies. My first shoot 'em up had 20 kinds of enemy, my second 50. I'll probably have even more for the next one. There are plenty of ways pooling could go wrong with hundreds of enemies pooled (50+ types, up to 30 required of a type), and I'm pretty sure I can improve on my old code to make sure I can get the maximum FPS on the lowest possible hardware.
     
    xVergilx and Oshigawa like this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,419
    Use a Profiler to measure it.

    Here are a few resources that should help you to get started with the Unity Profiler.

    Learn how to diagnose common performance problems and optimize your projects:
    https://unity3d.com/learn/tutorials/topics/performance-optimization



     
    Mauri and Oshigawa like this.
  4. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    Thanks guys.