scene.addObject

Every object made from the same ‘template’ with .add object has the same name. In some situations this is undesirable.

Is there some other kind of identifier other than name that I could use and avoid this entirely? Or some way to edit a name through a python script?

When you call addObject() it returns a reference to the new object which you can save for later use.

So I could append the reference to new object onto the end of a big list and store for later, or something. I see. Thanks!

You can’t change the name of duplicates, but you can identify either by KX_GameObject.id or by a property. But since id’s are never the same this would cause problems with saving and loading the game, so better use properties.

Here’s an example. Add an empty and attach this code to it with an always sensor (no pulse mode). On an inactive layer add a range of objects to spawn. Start the game and you’ll see.

from random import choice, sample, seed
seed(0)

size = 10
number = 100
variation = 100
splitter = '.'

def spawn(cont):
    spawner = cont.owner
    scene = spawner.scene
    objects_names = [o.name for o in scene.objectsInactive]
    for object_name in objects_names:
        spawner[object_name] = []
    population = [i * size / variation / 2 for i in range(-variation, variation)]
    for i in range(number):
        object_name = choice(objects_names)
        object = scene.addObject(object_name, spawner)
        object.worldPosition = sample(population, 3)
        object['id'] = object_name + splitter + str(len(spawner[object_name]))
        spawner[object_name].append(object)
    
    # test
    for object_name in objects_names:
        for ob in spawner[object_name]:
            id = ob['id']
            print(id, id.split(splitter))

Edit: spawner now holds the lists of objects.

Depends on what and how you are adding to scene.

For NPCs I’d use a spawn script that either counts the amount of identically named objects in the scene and appends a property with that number to the object.


#untested
cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()

def Spawn("objname"):
   listidentical = [obj for obj in scene.objects if obj.name == "objname"]
   newobj = scene.addObject("objname", cont.owner, 0)
   newobj["id"] = len(listidentical)

For particles maybe rather store a number of spawned particles in emitter than gameobject references to them. Particles often have a lifetime and are removed sooner or later which makes the reference cause an error.

Remember that when you spawn IDENTICAL objects, it doesn’t make actually sense to be able to differentiate between them in this arbitrary way. I recommend you to question why do you need to able to ID between them. An example:

For 3 of my NPC characters used in a cutscene I actually just cloned one character I had available at this phase until I get the other 2 done. But then I wanted to give them separate waypoints, dialogue etc so of course I had to differentiate between them. I was first annoyed at how troublesome it was but then I realized it was all because of my shortcut I was taking. I wanted some instances of the exact copies to behave differently and that is actually bad design.

Often you can go around this problem by altering the objects in some meaningful way. Maybe even visual so the player can expect them to behave differently. For example if some cubes can be pushed and others not, you better also set a different object.color after adding them for example. You can then check for object.color later in the code for if the object is pushable.