'Object Pooling' is essentially recycling but in scripting form. Whenever you spawn/create an Object (in this case an Actor) at any time, it needs to have its own Scripting ID to identify itself from others. If you don't have unique ID's for your spawned Actors...well, that's when things get messy.
The simplest way to do this is by having a Global Variable that you add 1 to each time a new Actor is spawned - so each new Actor will have a new ID. So what's wrong with just doing that? - Well the problem with this is that the number will just keep escalating upwards and if you've got a game where Actors are constantly spawning/deleting then that number is going to get very big, very quickly. Keep in mind that it needs to allocate room in the systems memory in order to keep track of that number - so the larger it gets the more memory it's going to need. Now, this memory increase is rather insignificant, because storing Integers doesn't exactly require all that much space, but it could potentially turn into something quite nasty - if your game is left running it'll just keep adding up and up and up - so it could potentially lead to a compromised system. How do we stop this? Object Pooling! (recycling)
Just like in real life, in scripting it's important to recycle things - rather than just throwing them away. So rather than continuing to add to the Global Variable each time it spawns a new Actor, we instead re-use ID's that were once used by Actors that have since been deleted. So, effectively what we're doing is when an Actor is deleted, we pinch it's unique ID and store it inside a Collection to be used for another spawned Actor. Here's the script I built for my game but it's universal (so it should work with any project)
Custom Event -> 'Spawn ID'
Collection -> 'colPooling'
Global Variable -> 'intObjectID'
Local Variable-> 'intID'
First we check to see if the Collection has any values in it at all - if it doesn't then it needs to add 1 onto our Global Variable and then return that value back to the relating script. When I Create/Spawn an Actor I have something like this in its value:
"obj" & Custom.Spawn_ID
This sets the newly spawned Actor's name to "obj" and then runs our Custom Event in order to collect its numerical identifier (that's what the 'Return Value' event does). So it returns a value of "1" - if it's the first one. So the final name of the Actor is "obj1".
Moving back to the Custom Event...on the opposite node we first sort of the collection in ascending order (this is optional but I like to have the lower values used first - it just makes more sense :P). Next, we set our Local Variable to the first entry of the Collection (this needs to be stored in a Local/Global Variable because we're about to remove the value from the Collection!) - Then we remove that value from the Collection and then return the value, like we did on the opposite node.
When an Actor is deleted we simply have an 'Add to Collection' event before it and then have something like this for its value:
Basically what I'm doing is cutting out the first 3 characters of the Actor's name and then returning the rest. All of my Actor's have 3 characters at the start in order to identify what it is. For example: Actor 1 would be: "obj1" - of course we don't want the "obj" characters to be stored, only the number, so we use the 'Cut Out Left Portion' use value to ignore those first 3 characters and only return the characters afterwards (the numbers). So only "1" is stored in our Collection - ready to be re-used when a new Actor needs to be spawned.
It's a very simple script and, as I mentioned, it's universal (so it can be used for any project). This is only really useful if your game does a lot of spawning/deleting of Objects but a good practice/habit to get into as many people don't make use of object pooling - when you really should! Now you've got no excuse - because I've just showed you how to do it!
There's been a total of 4294 views(s).
There's been 2485 people that have viewed.