Delaney King

Delaney King

20-11-2022

15:01

A big mistakes in #gamedev is spawning entirely new objects rather than using object pooling.

Spawning, or instantiation is making a new copy of an object into memory. Let's take the example of bullet casings. If every time you shoot, you create a new object, you can rapidly end up with thousands of the buggers on screen, in memory and chugging can occur.

With object pooling, however, you say... okay there are only ever N casings in memory. They are invisible, turned off but exist when you start the level. Let's say there are ten for this example.

When a gun first fires, it doesn't create a new bullet casing, it grabs the first casing from the pool, moves this to the guns position, makes it visible and throws it out into the world.

It then records the next free casing is number 2. Another gun fires and casing 2 teleports to the gun, has its physics reset, becomes visible and flies off into the world to plink satisfyingly against the floor. Next free casing is 3.

This continues until the pool runs out. When casing 10 is used, the pool returns to 1, (or 0) and the original casing is made invisible, teleports to the gun, is reset and made visible. This means there is only ever 10 casings in the game. Ever. Ever.

You can set up object pooling for pretty much everything in your game, including enemies, hit effects, spell effects, blood splats, sound effects... you name it. Object pools give you clean control over memory allocation, frame rate and hiccups when you instantiate.

Object pools can be passed enemy types, different artwork, different stats and abilities, but enemy01 is always enemy01 regardless of if it is playing a goblin, a zombie or a chainsaw knight riding on a chicken. If you have a pool of 8 enemies, you won't ever get more...

Than 8 enemies on screen. Until you kill some of them, or they unload and clean the pool.



Follow us on Twitter

to be informed of the latest developments and updates!


You can easily use to @tivitikothread bot for create more readable thread!
Donate 💲

You can keep this app free of charge by supporting 😊

for server charges...