Changing the name of an object in realtime

ok well im working on a blender snake game, the problem is that i can’t get the objects that are added to have a different name, because they carry the same name as their “maker” so i was wondering if there is a way to change an objects name in real-time?

The only thing that is unique about created objects is their id (which using id information through means like python can be tricky).

The object’s name as far as I’m concerned is read-only (meaning it can’t be changed), however you could have them carry a string property which is assigned different values according to a counter that counts up by one with each object created (and to add to a string you would do something like
obj[‘string’] = str(obj.name) + str(GameLogic.counter) or something similar)

Yes, the object name is read only, if I recall. The only possibility for uniquely ‘naming’ objects is to do like Ace said and make a property that each object has, which is unique in its value. Then, loop through all objects (either in the scene or in a single list of added, named objects) and check their properties. It’s too bad that we can’t change objects’ names, but that’s just one of the limitations of the BGE…

I do not really see a reason, why you need to change the name of an game object. In the most cases this wish is a result of misunderstanding how the BGE works (mostly when mixing up strings and GameObjects).

There are a lot of ways to deal with this without the need to change the name. The important question is:
What do you want to achieve?
(and what made you think you need to change the name of a game object)?

Ace -thank you, i might be able to try something kinda like that.
Monster -i am in the proses of making a “snake game” and i was trying to get the segments that get added to track to the segment they were made by. First i tried to to use “lastObjectCreated” But this returned the same name every time (“OBSegment”) and python kept complaining that there is no object named “OBSegment” it seemed that the lastObjectCreated function adds “OB” to the name of the object. so then i thought that i could use a variable to decide which object to follow. Follow = “Segment.” + str(prop). but this didnt work because all of the added objects have the same name as the one before. and so they all track to the first cube.

Actually, the object called by (objectLastCreated) is actually unique, even if it’s an instance. What i mean to say is, if you were to append each added object to a list:

if add == True:
objects.append(add.objectLastCreated)

If you try this with each object using the add object actuator, then use code like this:
number = 4.0
for object in objects:
object.position = [object.position[0],object.position[0],number]
number += -0.1

each of the objects should be higher than the one next to it…
However, if you use the name of the added object, and call it in the list of objects in the scene, it becomes useless. (as you are not calling the unique object, just the name which all of them share.
object.

[Edit]
@kendrick1397: It is a I thought, you mix the object name with the object reference.

ObjectLastCreated() does NOT return a string!
It returns an object reference. You can directly use it.
e.g.:


lastSegment = actuator.objectLastCreated()
lastSegment["follow"] = previousSegment
trackToActuator.object = lastSegment # better use a reference than a string!

As you see a object reference is much better then a simple string. If you want to know the name of the object:


print ("Hello my name is %s" %(lastSegment.name))
print ("My location is %s" %(str(lastSegment.worldPosition))

You can’t do that with a string.

What you can do with a string is to try to find an object reference with that name (usefull for save/load and network messages). If you have multiple copies - you get any copy, not necessarily the one you meant.

Hold on Monster!
What i meant to add was, if you use
add.objectLastCreated().name
that returns the name of the object, and with add_actuator it is not unique (instances!)

@Monster - Hmm. I suppose unique object naming isn’t really necessary - I thought that by using unique object naming, you could find a specific object quickly, but the process would be the same as looping through objects and checking their unique property for a specific value…

Also, yes, object names aren’t the same as the object’s identifier. agoose77 is correct that while you can directly access (and store) add.objectlastCreated(), you can’t do very much with just its name, as it is not unique (the most you could do is reference the scene object list for the name, which would just give you one of the added objects, not a specific one).

I wonder, if after checking the reference for a certain name created with actuator.objectLastCreated, you could append to a custom list of perhaps the last three or four objects with the same reference name, check the length of the list and then check for the object reference, and finally deleting that object from the list, you could theoretically then assign the same type of object to appear in two or more positions as they are created if needed using just one add object actuator (or just through use of the instantAddObject function in the Python API that can do the same thing)?

In theory this could be plausible, since objectLastCreated only stores one reference for an object type while you can hold more than one by moving it to a list.

@Ace - I’m having a bit of trouble understanding your logic, though I want to - what is the goal? You can easily add objects using the scene’s addObject function, so that is easy to use to create objects.

If you want a quick look up of game objects, use a Python dictionary. As the key use the custom name you want and the value can be the object. If you can also store the custom name as a property on the object:


import bge

# Create a dictionary (you should only do this once)
bge.logic.objects = {}

# Add an object
scene = bge.logic.getCurrentScene()
ob = scene.addObject("MyObject", adder)

# Cache the object
ob['name'] =  "MyObject3"
bge.logic.objects[ob['name']] = ob

# Retrieve the object (I don't think this can actually be done in the same frame...)
ob = bge.logic.objects["MyObject3"]

Ah - Ohh, okay, I get it. So you put a unique name variable in the added object, and then store the object in the dictionary and refer to the object by the unique name… That’s really smart - I’ll have to try to implement this sometime. Thanks, Moguri.

@agoose77, your solution is fine. Sorry, I didn’t meant you with my statement. I meant kendrick1397.
I edit my post to make it clear.

Beside that I recommend Moduri’s solution if you want to identify game objects with equal names by string.

I don’t see the point of this . All this might do is to return the object duplicated whatever the name given to search into the dictionnary.

Can i identify an instance with : name of the object + propriety + value inside that propriety ?

Ok , to make it clear. An instance of the object can be recorded in a global variable (or a dictionnary when many instance ) and not in a new game object.

So , for each instance, create a global variable “my_variable” in a “whatever_name.py” file , give him any value, and import it in the python controller by writing “import whatever_name” after “import bge”

   whatever_name.my_variable = scene.addObject(   **3 parameters**  )

whatever_name.my_variable can receive proprieties too

whatever_name.my_variable[‘name’] = “John”
whatever_name.my_variable[‘age’] = 102

dude the post is 8 years old.
you can rename objects in upbge doing obj.name = "name"

thx. Is there a clear page showing all the difference UPBGE and BGE ?

yep, i know but , i dont think google cares abut the age of the first post :slight_smile:

https://doc.upbge.org/api-changes.php