In any game you may be making, more than likely you will run into times where you will need to spawn objects (Enemies, bullets, players, pick-up items, etc.). In this article we go over spawn points and object pooling and go over some tips that can help you in your game development!
CONCEPT OF SPAWN POINTS
So what exactly is a spawn point? Simply put it is a location in space (2D or 3D), which you define by placing an empty GameObject in that location, and then you would MOVE or PLACE an object at that location via instantiation or pooling (which you will learn about below). Some examples of common objects that people place at spawn points are a pick-up item like health for your game, characters (Such as in an FPS game after a player dies you would respawn them in a certain location) or enemy objects ready to attack the player. (I’m also placing a link to the Wikipedia definition of Object Pooling as well => https://en.wikipedia.org/wiki/Object_pool_pattern)
Strategically placing spawn points in games for various object types can help to add more variety and challenge to the overall game and bring in some much needed game play elements.
This is the most basic way to spawn an object however it is not recommended as it is very memory intensive and can cause your game to crash over time, especially if it is being used on lower end devices with limited memories.
Basically what you’re doing is you are asking the computer to give you a dedicated amount of memory to create an object in your game. Once you are done, you want to destroy the object and give the memory back to the computer to use again elsewhere.
The challenge comes in when you are trying to instantiate many objects (as is the normal use case in games). Take for example a weapon which has bullets. If you instantiate each bullet in memory and then destroy it, over time you may not have the memory available and thus the program crashes or you just dont get a bullet created as the computer comes back to you with an error. Not only that, if you are instantiating many objects (especially if they are more complex and dynamic, such as enemies to attack the player and they all have animations and effects and more on them), then you can see how this could easily slow down your computer and thus lead to lower frame rates and quality.
The solution however is using a technique in game development known as object pooling…But first lets go over how we do a basic instantiation of objects!
// Instantiates 10 copies of Prefab each 2 units apart from each other
public class ExampleClass : MonoBehaviour
public Transform prefab;
for (int i = 0; i < 10; i++)
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
Once you have instantiated objects a lot of developers wonder how we get rid of them (Ex. an enemy that has been killed or a bullet that has been shot). The INEFFICIENT way would be to make sure we remove them from our scenes via a destroy command. This however does not help us with the issue of memory as it does return the memory to the computer to reuse, however over time if the computer decides to give that memory to another process, we wont have it available for our game to instantiate another object…This leads us to OBJECT POOLING!
CONCEPT OF OBJECT POOLING
So now that you understand a bit more about the concept of spawning and instantiation, you may be wondering what object pooling is and why it is so important?
As you read earlier we reach limits with memory and thus we end up getting program crashes and other issues along the way with instantiation and destroying objects. This is not efficient and does not help our frame rates nor our game being smooth and fast.
Enter Object Pooling! It is literally the concept of REUSING the same objects that have been loaded into memory rather than destroying them.
The easiest way to see this concept in action is to think of a weapon with projectiles (Bullets). Lets say you wanted to simulate many bullets in your game, you could easily instantiate them each time the user presses the fire button and then destroy them but that returns the memory back to the computer (And imagine if the user presses the fire button continuously which happens all the time in FPS games, that could be hundreds if not thousands of instantiation calls).
The concept of object pooling instead is to take those same bullets and simply deactivate them and move them somewhere else (Into a POOL of objects) that you can easily call again to the same starting point of the weapon and reuse the object.
So now with that concept in mind, instead of having to have unlimited bullets being instantiated and destroyed, you could have a limited amount of bullets in total instantiated, and when the player fires a bullet instead of having the object destroyed after use, we would simply deactivate and replace it into the pool again ready to be reused again (NOTE By Uroš Zečević in a comment below, ‘You need to reset their properties which can be tricky for more complex objects sometimes. Also, you need the way to track the number of spawned objects and adjust the pool size when needed.’) With that you can still simulate an infinite amount simply by using this wonderful concept and not have to exhaust your computer memory or slow down your frame rates.
Keep in mind as I said that there are many ways to do this and many times they are dependent on the game type and genre. Try out different ones and see which specific pooling system matches for you and your game type. When done right you can save memory and increase your fps and thus give your players an incredible gameplay experience!
SPAWN POINT TIPS AND IDEAS
TELEPORTATION This technique basically uses two spawn points, one as a start point and one as an end point. You would use an OnTriggerEnter function to detect when the player or item to teleport enters the object. Once there you would simply move the object (you can use a tween effect if you wish) to the other spawn point location. This gives the illusion of teleportation and is used in many games.
PARTICLE EFFECTS Many times you may need to spawn up particle effects in certain locations (such as when you want to indicate a player has hit an enemy or object at a certain point in space). Rather than instantiating, try pooling your particle effects as well and reusing them instead of destroying them. This could give you a bit of a boost in fps (I’ve used this several times in my own games).
AUDIO AND SOUNDS This one is an interesting one, but if you are instantiating any sounds, try and put your sound and audio into pools. This would work well for things like sound effects that are commonly reused (Especially in 3D space if you are trying to get realistic sounds in specific locations. Think of VR style games for example).
So as you can see, spawning objects and pooling can be very useful and helpful concepts of the game dev world! Use some of these concepts today in your own games and until next time, HAVE A BLAST MAKING YOUR AWESOME GAMES!
Feel free to come watch me live on twitch where I teach game development and these same training’s using the top games such as League of Legends, Starcraft II, Hearthstone, Overwatch, Fortnite and many other top games! https://www.twitch.tv/academyofgames0
Until next time, keep on grinding at the code 🙂 You’re a super star!! Keep up the great work and NEVER GIVE UP! You Got This!! BE A CHAMPION!
Nav is a Unity Live Expert and Mobile Game and App Developer