Level up your code with game programming design patterns: Object pool | Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] instantiating and destroying objects involves a lot of processing instead of going through this expensive cycle of creating and discarding objects why not recycle them that's what object pooling is all about just think of object pooling as like reusing arrows instead of crafting a new Arrow every time you want to shoot you can simply pick up an arrow off the floor or stuck in a zombie head and then put it back in your quiver for future use object pooling pre-re a pool of instances of a game object like arrows and a quiver and keeps them deactivated when you need to use an object simply activate it and place it where you need it starting from Unity 2021 Unity provides its own object pooling API that will be the focus of this video using the gun turret example from the design patterns ebook linked below before we go step-by-step through things let's first take a look at what our code will eventually do in the gun class called revised gun in the ebook example if fire one is press rest in fixed update after a time delay instead of using instantiate we use get to retrieve an object from the pool and in the projectile class after an amount of time of being alive instead of using destroy the projectile returns itself to the object pool with release so in this example the projectile has reference to the object pool which is also convenient for returning itself on collision all right let's rewind and start from the top with the variables in the gun script at the top of the script include Unity Eng . poool the namespace that allows us to use unity's object pooling API the ebook example script has tool tips for the serialized fields for easy understanding projectile prefab is the prefab with the projectile script we'll look at that again shortly the muzzle velocity field allows you to control the speed of the projectile directly from the gun which is nice to have centralized here rather than on the projectiles muzzle position is the starting point for the bullets cool down window is the time duration between consecutive shot shots this way the bullets don't fire too rapidly and next we Define an interface I object pool parameterized with projectile this sets up the object pool specifically for storing and managing projectile instances collection check is a safety feature of the pool when enabled it checks if you're trying to add an object back to the pool that's already there and throws an exception if that happens the check is only performed in the unity editor for debugging purposes if you're not running into any issues you can turn it off the default capacity and Max size Fields help you Define the initial and maximum size of the object pool it's computationally expensive to frequently resize the pool so set your default capacity such that it doesn't need to be expanded too often Max size acts as a hard cap ensuring that the pool doesn't grow uncontrollably and next time to shoot is a private field used to manage the cool down between shots now in awake we create a new object pool and here we have the Constructor which has several parameters as we walk through this specific implementation let's also look at the object pools Constructor in the API that way we can look at each method and context we've already looked at the collection check default capacity and Max size so let's focus on our first parameter create Funk which is of type Funk of T this represents a delegate or function pointer which returns a value in simpler terms create Funk is like the set of instructions for crafting a new object when the pool is empty think of it as the recipe for making a new Arrow if the quiver is empty and it also returns that new Arrow to you in our example the function is create projectile which returns a projectile object the example sets an object pool property on the projectile instance this way the projectile can have access to the pool this is useful because the projectile can release itself back to the pool after a certain time or on collision that creates a separation of concerns since the projectiles know about their collisions and how long they've been active that's not stuff the gun script should have to be concerned with next we have action on get this action is triggered when an instance is taken from the pool so you can use it to activate the instance the method in the example is on get from pool and there we make the game object active action on release is called when you return an instance back to the pool typically you deactivate the instance here so here we have on relase to pool where the projectile is deactivated then we have action on Destroy if the pool reaches its maximum size and you try to add another item this gets called usually you'll use this to safely destroy the object with the pool created and ready to go all we need to do to get a projectile is object pool get and just like we saw with a bow and arrow we put the bullet in the position it should shoot from and add a force to the rigid body at the bottom we set the cool down delay for the next time to shoot and we can see here that we're invoking deactivate on the projectile so let's take a look at the projectile script next the projectile class has a public property to have reference to the object pool when the gun invokes deactivate on the projectile it starts a timer so that after the timeout delay the projectile stops itself and returns itself back to the pool with object pool release and there you have it simple straightforward object pooling if you want to try creating your own version of the object pooling pattern or learn the other design patterns check out the free ebook at the link below
Info
Channel: Unity
Views: 27,544
Rating: undefined out of 5
Keywords: Unity3d, Unity, Unity Technologies, Game Development, Game Dev
Id: U08ScgT3RVM
Channel Id: undefined
Length: 5min 32sec (332 seconds)
Published: Wed Jan 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.