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
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.