import bge from Scripts.Common import * # This script is to spawn objects smartly # We will need this scene = bge.logic.getCurrentScene() # We need a dictionary to save objects for reuse if "reuse" not in bge.logic.globalDict: bge.logic.globalDict["reuse"] = {} bge.logic.globalDict["reuse-amounts"] = {} bge.logic.globalDict["self-destruct"] = [] # For easy access reuse = bge.logic.globalDict["reuse"] amounts = bge.logic.globalDict["reuse-amounts"] selfDestruct = bge.logic.globalDict["self-destruct"] # Function to spawn objects def Create(object, selfDestructFrames=0, selfDestructInactive=False, visible=True, declarenew=False, frompoint=None): # Making a list of those objects in reuse dictionary. if object not in reuse: reuse[object] = [] # If the list is empty ( we need more objects ) we make a new one. if not reuse[object]: if not frompoint: frompoint = object object = scene.addObject(object, frompoint, 0, False) new = True if object.name not in amounts: amounts[object.name] = 0 amounts[object.name] += 1 #print(clr["bold"]+clr["tdyl"]+"Reuse New"+clr["norm"]+":", object, amounts[object.name]) else: object = reuse[object].pop(0) object.restorePhysics() object.worldLinearVelocity = [0,0,0] object.worldAngularVelocity = [0,0,0] new = False # If self descructing if selfDestructFrames: SelfDestruct(object, selfDestructFrames, selfDestructInactive) object.setVisible( visible ) if declarenew: return object, new else: return object def Delete(object, inactive=False): # To make this callable from logic bricks the next thing is needed. try: object = object.owner except: pass # Making a list of those objects in reuse dictionary. # Technically if Create() was use to create this object it's # not needed, but incase other objects will be stored like this. if object.name not in reuse: reuse[object.name] = [] # Sometimes just recording that the object is available is enough: if not inactive: # Instead of deleting we are going to store it for later object.worldLinearVelocity = [0,0,0] object.worldAngularVelocity = [0,0,0] object.scaling = [1,1,1] object.suspendPhysics() object.position = [0, 0, -1000] # Putting it outside of the playable area ( in case there are parented parts ) object.visible = False # For some objects if "Motion" in object.actuators: object.actuators["Motion"].dLoc = [0,0,0] # Storing the object for later use if object not in reuse[object.name]: reuse[object.name].append(object) # Making sure it will not self distract again after it is reused object["self-destruct"] = 2 if object in selfDestruct: selfDestruct.remove(object) def SelfDestruct(object, frames, inactive=False): # Function that will make objects ( like particles ) self terminate: # We will add the object into a dict object["self-destruct"] = frames object["self-destruct-inactive"] = inactive selfDestruct.append(object) def SelfDestructDo(): # Happening on update to self destruct objects for object in selfDestruct: object["self-destruct"] -= 1 if object["self-destruct"] <= 0: Delete(object, object["self-destruct-inactive"]) if object in selfDestruct: selfDestruct.remove(object) def EndObject(object): # Actually end object # Reuding the number of the object in amounts if object.name in amounts: amounts[object.name] -= 1 print(clr["bold"]+clr["tdrd"]+"Reuse End Object"+clr["norm"]+":", object, amounts[object.name]) # Removing the object from reuse if it is in reuse if object in reuse.get(object.name): reuse[object.name].remove(object) # Removing all object's children, and the object. for i in object.childrenRecursive: i.endObject() object.endObject()