Entity Pool Package

This package includes a script for using a global entity pool as well as a locator that already has the script attached to it so you can easily add it to your world.

How to install:
Search and install “Entity Pool Package” into your game.
Place one instance of the “GlobalPoolTemplate” anywhere in your world.
To use the entity pool in a script, use the following snippet self.entityPoolScript = GetWorld():FindScript("EntityPoolScript") to access it easily throughout your script.

How to use:
Once you have the entity pool installed, using it is simple. To get an entity (the script will decide to spawn or retrieve from the pool depending on availability) call self.entityPoolScript:Get(TEMPLATE_ASSET, LOCATION_VECTOR, ROTATION_VECTOR), this will return an entity created from the template at the passed in location with the passed in rotation.
To return an item to the pool, simply call self.entityPoolScript:Return(ENTITY_TO_RETURN).

Note: a returned entity simply gets added back into the pool and does not have its visibility or anything changed, in order to handle this, please see below.

Some advanced options for cleaning up and initializing an entity upon return and retrieval can be set by calling self.entityPoolScript:RegisterTemplate(TEMPLATE_ASSET, GET_CALLBACK, RETURN_CALLBACK).
An example of setting a car entity to be visible with collision enabled upon taking one from the pool and invisible, collision disabled, and in location <0, 0, 0> on returning to the pool would be:

self.entityPoolScript:RegisterTemplate(self.properties.carTemplate,
  -- Get callback
  function(carEntity)
    carEntity.visible = true
    carEntity.collisionEnabled = true
  end,
  -- Return callback
  function(carEntity)
    carEntity.visible = false
    carEntity.collisionEnabled = false
    carEntity:SetPosition(Vector.Zero)
  end
)

Please let me know if you have any questions or comments about the script.

2 Likes

Object pooling if definitely my next optimization work.
Thank you for this help

Hey,
I started to use your package.
Well done !
After 10 minutes, it was functionnal for bombs and flames :slight_smile:
But, I don’t know why, when I stop/restart preview, object pooling seems to keep reference to old entities.
I need to start a fresh server again.
Is that normal ?

Ah yeah, I have this issue too. I added a check for invalid entities just because of the preview problem. I can push up the change to the package later. Glad it worked for you!

Just updated the package. Let me know if that fixes the issue for you :slightly_smiling_face:

1 Like

Nice, no more pb on multiple simulations.
Great package :+1: