ObjectPools

by Ter13
Create and manage pools of objects for retrieval and reuse later.
ID:1377691
 
atom/movable procs:



pooled()
format: pooled(poolname)
args:
  • poolname: the name of the pool that this object was added to.

default action:
set src.loc to null
usage:
override this to perform cleanup actions on movables that are moving back into the pool, so they can be easily reused later.


unpooled()
format: unpooled(poolname)
args:
  • poolname: the name of the pool that this object was retrieved from.

default action:
nothing
usage:
override this to perform initialization actions on movables that are moving out of the pool, so they can be easily reused later.


global procs:



createPool()
format: createPool(poolname,type)
args:
  • poolname: the name of the pool that will be created.
  • type: the type of object that will inhabit this pool.

default action:
create the pool by name if it does not already exist, and ensure that the type of the pool will be the type supplied.
returns:
a new list with the first element being the type supplied.


pool()
format: pool(poolname,atom/movable)
args:
  • poolname: the name of the pool that will be used.
  • atom/movable: the object we are adding to the pool.

default action:
if the pool doesn't exist, create one with the base type being the object's type, then add the object to the named pool. After object is added, pooled() is called on the object added to the pool.
returns:
nothing


unpool()
format: unpool(poolname,type=null)
args:
  • poolname: the name of the pool that will be used.
  • type: an optional type, that if the named pool doesn't exist, a new pool will be created using this type.

default action:
if the pool doesn't exist, create one with the base type being the supplied type. If no type is supplied, return null. If the pool exists, or was created, check the pool. If the pool does not have any ready-made objects, create a new one, and return that instead. If the pool does have objects, grab the first one, remove it from the pool, then return it, after calling upooled() on the returned object.
returns:
one of: null, a new object of the type referenced by pool, or an object residing in the named pool.
I'm going to assume there was another post there at one point, and you aren't just having semi-argumentative conversations with yourself ;)

Pretty nice, and timely with the particle effect kind of stuff people have been working on. Thanks.
I'm going to assume there was another post there at one point, and you aren't just having semi-argumentative conversations with yourself ;)

I have no idea what you are talking about. ;P

Pretty nice, and timely with the particle effect kind of stuff people have been working on. Thanks.

It's a pretty simple concept, actually, one I'm having some difficulty enhancing. If you have any suggestions, let me know.
Fine, now make it look like I'M the one seeing things... ;)

You have the second group of procs (createpool, pool, unpool) listed as atom/movable procs, when they are actually global.
Thanks, copy/paste gone wrong. Fixed.
Perhaps you could point out what I'm missing here. I'm trying to use ObjectPools to handle the particles for my firework object. It works just fine for the first object, but then I get no particles after that. I'm unsure if I should have to do any "factory resetting" on my particles when I pool them again.
world/New()
..()
createPool("particle_red", /obj/particle/red)

obj/Firework
proc/Effect()
src.alpha=0
for(var/i=0, i<40, i++)
var/obj/particle/red/p = unpool("particle_red")
p.loc = src.loc
animate(p, pixel_x=rand(-64,64), pixel_y=rand(-64,64), alpha=0, time=rand(3,8))
spawn(9) pool("particle_red",p)
del(src)
In response to Davlin
Yeah, you do need to make sure your particles are reset each time. They're staying at alpha = 0 (and starting off with the previous pixel offsets) after they've been pooled and unpooled.
The atom/movable/unpooled() function is for setting things up, and the pooled() function is for preparing things for being added back to the pool.

You can override those to make it a bit easier to remove any lingering effects you made when using the particle in the first place.
Ah yeah, thanks Kaio. Never thought of also pooling the firework object, thanks Ter.
Wait, don't pool the firework, sorry, my misunderstanding. I didn't read that snippet thoroughly. Pool the particles, the firework is fine.
One more from 'unpooled()':
override this to perform initialization actions on movables that are moving back into the pool, so they can be easily reused later.

Shouldn't these be movables that are moving out of the pool?
Good catch, thank you. I wrote three libraries with similar design documentation in one day, so my editing skills were definitely suffering.