Help to Pool Object in UPBGE

I’ve been publishing some games on UPBGE, but due to feedback from people who have tested my games, people have said that lags occur during the .exe, I believe it’s the simple system I’ve been using to add enemies (scene.addObejct). I found out that the pool exists, but I’m having difficulties implementing it in my code, could anyone give an example?

“”"
Offer a significant performance boost; it is most effective in
situations where the cost of initializing a class instance is high, the
rate of instantiation of a class is high, and the number of
instantiations in use at any one time is low.
“”"

class ReusablePool:
“”"
Manage Reusable objects for use by Client objects.
“”"

def __init__(self, size):
    self._reusables = [Reusable() for _ in range(size)]

def acquire(self):
    return self._reusables.pop()

def release(self, reusable):
    self._reusables.append(reusable)

class Reusable:
“”"
Collaborate with other objects for a limited amount of time, then
they are no longer needed for that collaboration.
“”"

pass

def main():
reusable_pool = ReusablePool(10)
reusable = reusable_pool.acquire()
reusable_pool.release(reusable)

if name == “main”:
main()

Look in this example downloaded for internet, how implement in UPBGE this system pool for instance objects and run?

if 'pool' not in own:
   own['pool']=[]
   for x in range(15):
       own['pool'].append(own.scene.addObject('PoolObjectName',own,0)

this creates the pool

to use it

if len(own.scene.objects['PoolObjectName']['pool'])>0:
    pool_item = own.scene.objects['PoolObjectName']['pool'].pop(0)
else:
   pool_item = own.scene.addObject('PoolObjectName',own,0)
   

and to return it to the pool after its aged out / or collided whatever

own.scene.objects['PoolObjectName']['pool'].append(pool_item)
1 Like

bullet_Pool_3x_revision_5.blend (1.0 MB)