Create too many meshes

Hello all

I made this code to make a model been created constantly (like a particle system)

class Stars(DirectObject):
    def __init__(self):
        self.stars=[]
        taskMgr.add(self.create,"starCreate")
        taskMgr.add(self.starMove,"starMove")
        
        
    def create(self,task):
      self.pos = 
      Vec3(random.uniform(-5,5),random.uniform(99, 100),random.uniform(-2,2))
      self.galaxy=loader.loadModel("star")
      self.galaxy.reparentTo(render)
      self.galaxy.setScale(1)
      self.galaxy.setPos(self.pos)
      self.stars.append(self.galaxy)
      return Task.cont

    def starMove(self,task):
        for i in range(len(self.stars)-1,-1,-1):
            self.stars[i].setY(self.stars[i].getY()-1)
            if self.stars[i].getY()<10:
                self.stars[i].detachNode()
        return Task.cont

Although I detach every node that its Y value less than 10 but the game begin to slow down after a while

I also read about RigidBodyCombiner but the problem is that I have to call rbs.collect every time (self.create) being done and manual doesn’t support that

Is there any idea about this??

Thanks in advance

You could use RigidBodyCombiner if you create a fixed amount of stars and then recycle the ones that you otherwise would have detached. Something like this:

class Stars(DirectObject):
    def __init__(self):
        self.available_stars=[]
        self.used_stars=[]
        # create 100 stars for use later
        for i in range(0, 100):
          galaxy=loader.loadModel("star")
          galaxy.reparentTo(render)
          galaxy.setScale(1)
          galaxy.hide()
          self.available_stars.append(galaxy)
        taskMgr.add(self.create,"starCreate")
        taskMgr.add(self.starMove,"starMove")
       
       
    def create(self,task):
      if not self.available_stars:
        return
      star = self.available_stars.pop(0)
      star.show()
      self.used_stars.append(star)
      pos = Vec3(random.uniform(-5,5),random.uniform(99, 100),random.uniform(-2,2))
      star.setPos(pos)
      return Task.cont

    def starMove(self,task):
        for i in range(len(self.used_stars)-1,-1,-1):
            self.used_stars[i].setY(self.used_stars[i].getY()-1)
            if self.used_stars[i].getY()<10:
                star = self.used_stars.pop(i)
                star.hide()
                self.available_stars.append(star)
        return Task.cont

That’s a great idea…I thought of making a cycle before but I didn’t know how…I think it will work fine now
Thanks