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. Dismiss Notice

Question I need an Object Pool, but...

Discussion in 'Scripting' started by XJonOneX, Jun 5, 2023.

  1. XJonOneX

    XJonOneX

    Joined:
    Dec 19, 2017
    Posts:
    69
    I'm making a Match 3 game using the 3D system, and I've got 7 different colored cubes that I made Prefabs.

    I'm at the point where I want to implement an object pool, but I now realize that it won't work with my Prefabs. I think I would need a single block prefab, that I can programmatically change the color of, somehow. I also want to add in different shapes later on, and I'm kind of lost at how to implement it all... gracefully. :)

    Any ideas out there?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    orionsyndrome and icauroboros like this.
  3. XJonOneX

    XJonOneX

    Joined:
    Dec 19, 2017
    Posts:
    69
    Insightful. Thank you! :)
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Honestly, for that you wouldn't need an object pooling system 25 years ago, in Flash 5's original Actionscript, not even a proper language, entirely interpreted live while embedded as an ActiveX plugin in MS Internet Explorer on Windows 98 SE. I don't know what's going on with the internet, why everyone is putting so much focus on coroutines and object pooling.
     
    XJonOneX likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    I think there's probably some old youtube tutorials out there still at the top of the algorithm search pile. Probably the same reason why we see people coming here trying to use the binary formatter.
     
  6. XJonOneX

    XJonOneX

    Joined:
    Dec 19, 2017
    Posts:
    69
    I learned of object pooling from a tutorial here on Unity's site a few years ago. It made logical sense to me. Keep garbage collection to a minimum by keeping objects recycled and improve performance by not allocating and reallocating memory. o_O I think it was the space shooter tutorial.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Logical sense is one thing.

    Engineering sense is quite another.

    Everything you create that you become forced to use is simply one more thing to maintain in your engineering space.

    When in doubt, do not write the software.
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Obviously I had to look into this, and sure, there is a Unity Learn tutorial on object pooling. You're informed about the intention of this approach right in the introductory paragraph.
    The project that is on the display to teach us how to use object pooling, is appropriately a project in which you get to fire bullets. Definitely not a walking simulator, not a puzzle, not a point & click adventure, and not match-3.

    Object pooling makes sense when you're producing too many graphical objects that are sure to flood the internal system with frequent instantiation and deletion.

    It makes sense to do this because the economy of scale kicks in, but also because the memory fragmentation tends to become a real burden when you have hundreds of objects that you're abandoning / losing references to.

    Garbage collection in C# wasn't actually made with games in mind, but also its very existence is a realm of arcane knowledge to many small developers, so what actually happens is that a lot of frequently accumulated garbage leads to regular GC sweeps that manifest as annoying hitches.

    Players having bad experiences with badly formulated projects, tend to bad mouth Unity as a whole, and then UT tries to put out the flames by educating newcomers i.e. propagandizing.

    Welcome to the real world.

    You don't actually have to use any object pooling system if you only understand what the goal is, and use the memory responsibly. Make sure you don't allocate 1) too frequently, 2) needlessly. There are plenty of opportunities to recycle existing allocations and even existing game objects. You can write to the same static array over and over. You can toggle your game objects on / off. You can make your own internal conveyor belt, you can set a rate of spawning, make conservative managers, nonallocating classes and functions, set up frame budgets, have instantiation caps soft and hard, manage things in chunks, force and even configure garbage collection ...

    At some point it makes more sense to invest in learning how C# operates, try to learn how strings actually work and use a StringBuilder instead of constantly concatenating strings, and I promise it'll do more for your garbage problem than object pooling ever will in a match-3 game.

    This is all about volume! Object pooling is not a great general advice at all.
     
    XJonOneX likes this.
  9. XJonOneX

    XJonOneX

    Joined:
    Dec 19, 2017
    Posts:
    69
    Thanks for the insight. I appreciate it. :)