GameDevHQ Intensive Training Program — 02 — Spawning and Pooling
The week’s second task was to build out a Spawn Manager to control how the enemy mechs enter the scene, have them appear in ever-increasing waves, and to utilise an Object Pool to minimise the number of new mechs that needed to be created. This seemed pretty simple at first glance, but turned out to have a few unexpected complications.
The first complication was entirely of my own making, and had to do with how I was telling the mechs where to go once they appeared. We’re encouraged to use the Singleton design pattern for our managers, which means that they’re static classes that can be called from anywhere. The caveat to that is that they should only ever talk directly to other singletons, not to regular classes. Communication there should be strictly regular-class-to-singleton. And yeah… I wasn’t doing that. I tied myself in all sorts of programmatic knots for a while trying to make things work properly before I realised that I needed to flip the way the information was flowing. There’s a few hours of my life I won’t get back.
The bigger complication came from figuring out how to make the mechs “walk off” the end of the track. I tried a number of different approaches, and finally settled for an invisible cube that they could collide with. When they collide, they’re taken out of play (and once we have a scoring system in place they’ll penalise the player for letting them escape). Even that took me longer to get working than I feel it should have, because I forgot that when you’re doing collisions in Unity at lease one of the colliding entities needs to have Rigidbody component attached to it or Unity doesn’t see the collision.
Object Pooling was pretty new to me. I’d worked with established pooling scripts such as Quill18’s SimplePool, but we were encouraged for this course to build our own from first principles. Luckily GameDevHQ’s C# Survival Guide has a whole section on this, and I watched it a couple of times to get my head around the concept before diving in. It’s actually not as scary as I’d thought. You basically just start by creating a base number of objects (enemy mechs, in my case) and turning them off so that they don’t appear in your scene. Then when you want one to appear you just turn the first available one on and put it where you want it. When you don’t need it any more, instead of destroying it you turn it off and it’s then available to be reused.
The clip below shows an example from one of the later waves that has more than ten mechs come through. The pool starts with 11 mechs in it, and they activate one by one as they come into view. A few extras need to get created because nothing is killing them off before the end, but once the first one reaches the end of the track it turns off (greys out) and then activates again as the next mech in the wave appears. In total, Unity only has to keep track of 15 mechs instead of the 20+ that every wave after the first calls for.
It’s such a simple concept, yet even at this point I can see how incredibly powerful it is. The waves grow by ten mechs each time. This means it won’t take long for a wave to consist of over 100 mechs. Yet once we’re building towers to shoot them down, there should never be that many on screen at one time (unless you’re doing really poorly!). So why should you need to create 100+ mechs that just sit around in Unity’s memory waiting to be used? As long as the pooling system has the facility to add extras to the pool as needed, you will only ever create as many as are on screen at once. It’s a very clever design pattern for all its simplicity, as is the case with all the best design patterns.
All in all it’s been a very good first week. I’ve had my first coaching call with our course mentor Dan Schatzeder, and he helped me find some places where I could improve and optimise my code. The rest of the class has been awesome and we’re all banding together on our Slack channel to help each other out and encourage each other. I’m feeling really good about the project, and about my skills, and I’m really looking forward to next week.