[BGE] [HELP] Set object name via python

Hi,

I have made a script that spawns the same object multiple times but they all have the same name and that gives me some weird things
I want to give it a unique name

i tried:


obj = scene.addObject(mouse_over.hitObject["item"], "spawner", 0)
obj.name = obj.name + str(random.nextInt(0, 999))

but that gives me the error:


AttributeError: attribute 'name' of 'KX_GameObject' objects is not writable

Any way to fix this?

The “name” attribute of an KX_GameObject is read only, the api says this. You can’t change the name of an object in realtime. Why do you want to do change the name to those objects?

They conflict if i want to remove one specific or teleport to it it just teleports to the first one spawned

Use a property and assign that a unique name for each object.


for i in range(NUM_OBJECTS):
object = scene.addObject(yadda, yadda)
object['ID'] = object.name+str(i)

And when you’re looking for that particular object, look for object[‘ID’] rather than object.name

:confused: Is there no other way (i was already thinking about ids but if i want it hat way i need to rewrite alot)

EDIT: with id’s i cant call


obj.endObject()

Because it will pick the first one spawned again

Hello! I don’t know what exactly you are trying to achieve. Could you explain the context?

Anyway, if you want, each time you add an object in the scene, you can append it to a list:

object = scene.addObject(“Cube”, “Empty”, 500)
bge.logic.myObList.append(object)

Then you can get your object in the list:

bge.logic.myObList[5].endObject()

EDIT: You can also add an entry in a dictionnary myDico[“Cube1”] = object

bge.logic.myObList[-1].endObject()
Removes the wrong one again the first one spawned

And what i want to make is teleportation pads they work but i need to link them and i get as oudput " linked ‘teleportPad’ to ‘teleportPad’ "
And that is where it goes wrong

Edit: also bge.logic.myObList[“teleportPad”].endObject() Gives me the same thing

I will try to rewrite it for id’s now

EDIT: my dict


['cube:0', 'cube:1', 'cube:2', 'teleportPad:3', 'cube:4', 'teleportPad:5']
#OBJ : ID

How can i get the location of a object with specific id?

You need to separate this into separate issues. The only reason you have multiple objects with the same name is if you spawn objects using the add object actuator or addObject.

When you do this, you should keep track of what you added, in order to be able to later reference it. You can access objects (Python objects) by their python id using the scene.objects.from_id function. However, it’s just as easy to store the object references in your own list, and avoid having to maintain this data. Hence, whenever you run the add object code / logic bricks, keep a reference to that added object. In order to differentiate between these objects that you’ve added something has to be unique, else there is no functional difference between them. Hence, whenever you want to access these objects, consider what criteria you use to find them.

If you store your spawned objects in a list as suggested above (which I also suggest), you can iterate through that list and test against the property.


obj_list = 
[list, of, objects]

WE_WANT = 'Teleporter3'

for obj in obj_list:
    if obj['ID'] == WE_WANT:
        obj_list.remove(obj)
        obj.endObject()

Just remember that if you’re going to remove the object from the scene, you want to remove it from your list of items as well.

Can someone help me with this?


locs = [] #location list
items = [Cube:0, teleportPad:1, teleportPad:2, gear:3]

#i want the location of teleportpad:1 and teleportPad:2
#i tried

for x in items:
  X = x.split(":")
  if X[0] == "teleportPad":
    if scene.objects[X[0]]["id"] == X[1]:
        pos = scene.objects[X[0]].worldPosition
       locs.append(pos)

Your script alone doesn’t give any reason as to what you’re trying to achieve. Can you state the problem with a goal in mind, e.g
“I would like to have two teleport pads, and randomly select one to spawn new objects at”, or something along those lines.

It’s best not to get objects by name. If you want to get an object that’s already in the scene give it a useful property as an identifier and then get it from a list:

telepad_1 = [ob for ob in own.scene.objects if ob.get("telepad") == 1][0]

This will find an object in the scene which has the property “telepad” with a value of 1.

You could give telepad_1 a link to another telepad by adding another property to it:

telepad_1[‘linked’] = “2”


telepad_1_partner = [ob for ob in own.scene.objects if ob.get("telepad") == telepad_1['linked']][0]

now;

if telepad_1['active'] = True:
    own.worldPosition = telepad_1_partner.worldPosition.copy()
    telepad_1['active'] = False

That way if you switch on telepad 1, it will teleport you over to telepad_2.

If you want to do something else, I’d still recommend doing the same way, get your objects from own.scene.objects using a list comprehension.

Thanks Smoking_mirror!

It works :smiley:
Only i need to make it dynamic to get more than 2 telePads