I am implementing a spawner/despawner system in my game.
To improve performance I use kdtree to check whether an Empty is within the radius of the player or not. If so, the empty spawns the object (could be a sound, an effect, ai… whatever). That is working fine so far.
The problem is the despawning.
I need the empty to stop spawning if the player leaves the kdtree radius and be ready to spawn items again if the player re-enters the area.
My code so far looks like this
def start(self, args): self.scene = self.object.scene self.timer = Timer(0) self.spawnerEmpty = [obj for obj in self.scene.objects if "spawner" in obj] self.kd = kdtree.KDTree(len(self.spawnerEmpty)) self.radius = args["Radius"] self.objList =  for id, obj in enumerate(self.objectEmpty): self.kd.insert(obj.worldPosition, id) self.kd.balance() self.active = None def objectAdder(self, obj, add): addObj= obj["Name"] if add == True: if obj["adder"] == 0: obj["adder"] = 1 self.scene.addObject(addObj, obj, 0) self.objList.append(addObj) elif add == False: obj["adder"] -= 1 self.objList.clear() def update(self): if self.timer.getElapsedTime() > 2: nearList = self.kd.find_range(self.object.worldPosition, self.radius) for obj in nearList: self.objectAdder(self.spawnerEmpty[obj], True) if nearList[-1] > self.radius: self.objectAdder(self.spawnerEmpty[obj], False) self.timer.reset()
So the objects that are spawned can have their own code and despawn themselves whenever the player is out of reach.
But I don’t want every spawnerEmpty to run its own code so I do this centralized from the player.
Basically, what I would like to achieve is setting back the obj[“adder”] property in the objectAdder() function back to 0 so it will be able to spawn items again whenever the player is back in the kdtree reach.
Does anybody know how to solve this? I’ve tried so many things but can’t find a solution