Referring to Individual Instances of Spawned Objects

this question follows on from an earlier one from me about spawned objects, where i was still struggling to understand what was happening in the context of the game i’m currently working on; however i think the question is sufficiently different to warrant a new thread:

the problem arises when you spawn two or more objects using an “Edit Object/Add Object” actuator, and then do an “Edit Object/End Object”. this will destroy all instances of that object

if you check the objlist() you see that all spawned objects have the same name (OBMySpawnedObject, or whatever)

soooooo… i think the question boils down to:

is there any way to uniquely refer to individual instances of spawned objects?

I’d like to know this too. I’ve come across a couple of methods mentioned on the forums, one of which would be a bit bulky for my needs and one of which I’m not too sure about the implementation of.

First method:

Create a property on the objects “UniqueName” or something and at the time the object is added, set this unique name to something that is not used and add it to a list of created objects. You can then check each object for the value of this property, use it to get the instance that you want to delete and then just delete this object. For large numbers of added objects, this would be a bulky and hard to manage approach.

Second method:

Use the getPhysicsID() method of the game object and use this to identify the objects. As far as I know, the physics ID for each created object is different, but this would need some playing about with because I’m not sure how this would be done or if it is actually a unique ID. Maybe store the list of physics IDs for the created objects?

thanks for the reply fw

i’m glad i’m not the only person struggling with this problem

it seems to me absolutely fundamental to a system like this, that every unique object within the BGE (or indeed within the totality of the blender system itself) should be assigned a unique ID, which can be used to reference it

as you say, there are potential workrounds, but my own feeling is that there will come a point when workrounds really aren’t enough

The functionality of the “Edit Object/End Object” actuator is limited to the object which hosts that particular logic brick. This actuator does not accept a “name” argument, so “destroying all instances” with an “Edit Object/End Object” actuator should not be possible.

If you’re looking for a way to get a reference to a specific instance of some arbitrary object, don’t search for a name in the scene object list (as you said, the names are the same), instead, use the events that affect the object in question (use the getHitObject function found on collision sensors, mouse rays, etc).

That is a pointless method.

You’re essentially carrying a pointer (physics ID) to a reference to some object. Don’t complicate things needlessly; just carry the actual reference, and use it to manipulate the instance in question.

http://blenderartists.org/forum/images/ba-buttons/viewpost.gif the problem arises when you spawn two or more objects using an “Edit Object/Add Object” actuator, and then do an “Edit Object/End Object”. this will destroy all instances of that object
If you mean “Edit Object/End Object” destroys all instances, simply put the Add Object on the object creator, and the End Object on the object the host adds.

Therefore, all instances of the object continue to have their own SCA’s.

Make sure your sensor will only apply to each individual object.
For example:
Keyboard: K will obviously destroy all instances of that object.
Mouse Over + Keyboard K will only destroy the object your mouse is over when you press K.

I haven’t tried this method yet, but it looks promising: lastCreatedObject. Read more here