Just wondering, because when you switch characters in a game, the character you were playing as before you switched becomes inactive, i.e it goes to a different layer, and is swapped out with another one.
In the game engine you use
addObject('object_to_add', 'spawn_location', 'the time it needs to stay: 0 forever')
this copies an object from an inactive layer to the active layer so you can kill the other one
for example you play with a, then remove object a, spawn object b
How then do I cap the amount of said object to just one instance? It adds multiple, which is not good for singleplayer.
run the script in single pulse mode, or add a property to keep track of the count.
This belongs to your game’s logic. Every time you run an “add” you get a new object. When you want to restrict your game to have none or one object only. you either prevent adding when such an object already exist, or you remove the existing object before your add the new one.
You could work with assumptions. E.g. you assume the object is always removed before you add such object again.
Or you check. Due to it’s dynamic character you need to do that via python:
import bge controller = bge.logic.getCurrentController() if all(sensor.positive for sensor in controller.sensors): scene = bge.logic.getCurrentScene() if not "object_name_to_add" in scene.objects: addObject('object_name_to_add', 'spawn_location', 'the time it needs to stay: 0 forever') else: print("Object already exist. Do not add")
import bge controller = bge.logic.getCurrentController() if all(sensor.positive for sensor in controller.sensors): scene = bge.logic.getCurrentScene() existingObject = scene.objects.get("object_name_to_be_replaced") if existingObject: existingObject.endObject() newObject = scene.addObject("object_name_to_add")
endObject() does not remove the object immediatly, but
addObject() adds an object immediatly.
This means after
addObject() until the next frame there will be both objects in the scene. If that causes unwanted sideefects you can add the new object by activating an AddObjectActuator.