NokiMo
Megan Fox
Megan Fox

patreon


Spawn Pooling (and cute art)

(this is the September post, not October, forgot to post this last night again)

This is a combo topic! There's code at the top, and cute art at the bottom. Scroll as you feel is appropriate, heh.

This topic comes up because I'm putting hats on birbs now. Also scarves. And glasses.

Original thought was maybe that we'd instantiate these as we loaded each birb into scene, but when you're talking about having a ton of NPC mission givers in the scene, that's like 30 birbs? Each wearing at least 5 articles of clothing, because some designer went a bit happy with the customization? Not to mention our VHS system, which is kinda how we let you play cutscenes, has to create whole scenes instantly when you press the load-tape button, and any delay there won't be well tolerated. Hence, problem, that's gonna stall hardcore.

So, let's talk about Spawn Pooling. It's a thing you do in other engines and is barely worth talking about, BUT sometimes folks don't do in Unity. It's a lot easier to Instantiate() a prefab in Unity, and especially on desktop, usually you can do it fast enough that it barely matters. It still matters if you're on a slow machine, or if you're creating 30,000 bullets or whatever, but mostly - people can ignore it. Unity optimized Instantiate() a while ago, so folks think the problem is sorted, sometimes.

BUT IT IS NOT! Because consoles. Freaking consoles, folks.

If you try and Instantiate() much of anything on an Xbox or especially a Switch, you're gonna see the mother of all stalls. There is no fancy trick to fix it, there is no avoiding it. A lot of indies lately are thinking about console ports, so that means - hey let's party like it's 2014! We literally had to rip the innards out of Spartan Fist to make it work on Xbox, because we'd relied heavily on Instantiate() in building our levels. Don't be like us! Do this NOW.

First up, don't write your own spawn pooler unless you're a masochist or have specific needs. Instead, use PoolManager. They've been the gold standard in Unity since, well, 2014 or so. I used PoolManager 2 way back on Jones On Fire, and they're up to 4 now, I think? Or higher.

Now then. "What's pooling?" you might ask. Well, PoolManager has fancy explainer videos, so I'll give you the short version: your pooling system creates a ton of duplicates of whatever you think you'll need to spawn runtime, and they actually get saved into the scene, but as disabled objects. Then when you want to spawn a thing, instead of Instantiate()'ing them, you pull one out of the disabled pile, enable it, and tada, it's like new. When you get done with it, you despawn, and it disables and puts it back in the pool for later.

Major upsides are twofold: you're avoiding the major Instantiate() stall during gameplay, because, you're shifting any stalls to scene load time. Scenes, unlike everything else, can be thread-loaded, while you've got a nice smooth loading screen up. Huzzah!

Now then, stuff gets a bit fancy. Look at how many hats and other clothing items our lovely concept artist Mike Rayhawk has dreamed up:

Now we're not going to do all of those, but even if we did a fraction, think about what that means for spawn pooling. Do we really want to have 5 or 10 of every single clothing piece up there just sitting around, just in case? Probably not. So remember, you're not just doing spawn pooling, you're also gonna want to leverage what PoolManager calls "Pre-runtime Instances".

Basically, you're gonna dress your NPCs as you edit your scenes, not as a load-time step. That means you don't have to think about how many extra copies of each hat you need in the spawn pool to cover all possible uses. The only things pulling directly from your spawn pool are player-driven, so you probably only need to manually put one or two copies of each hat in there, and then your NPCs are already wearing the extra copies of the hats/etc that they'll need. When they get despawned over the normal course of the level, their clothing goes into the pot, when they get respawned, their clothing comes out the pot, etc. It all works nicely without you needing 30 copies of the joker hat "just in case."

So to sum up:

... and if you're a $3+ patron, go here and get some random components I've made that I use a lot with PoolManager.


Related Creators