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
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?
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
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.
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.