Getting an added object's creator?

Using an actuator to add objects, is it possible with python to find the added object’s creator? Example I have an object that on collision with with these objects executes a script that tells me the name of the object it was hit by, Id like to be able to script something that tells me the objects creator or spawner.

Make a script add the object, using an addobject actuator.
Make a property on the object to be added called “own_ac”
then, when you activate the addobject in the script, make it set the name of the owner, or the reference:

import bge
cont = bge.logic.getCurrentController()
owner = cont.owner

add = cont.actuators[“add_actuator”]
sens = cont.sensors[“addd_sensor”]

if sens.positive:
ob = add.instantAddObject()
ob[“own_ac”] = owner

Had an error but it was because I put () at the end of objectLastCreated.

@ agoose77 - Thank you, this helps me so much.

You could also use the scene object’s addObject() function, which will return a pointer to the object just created:



cont = logic.getCurrentController()
obj = cont.owner
sce = logic.getCurrentScene()

sensor = cont.sensors['AddSensor']
if sensor.positive:
     created = sce.addObject('Object', obj)
     created['own_ac'] = obj

Yes, but i thought i would keep it simple for him.

SolarLune’s script seems a bit simpler than the previous one using an actuator. In every script I use to add objects, I always use the scene’s addObject() function.

So do i, but i presume that most logic brick users are more comfortable with actuators

I started using the scene.addObject() function and that increases logic usage by up to 60% percent, but with the actuator way logic increases by 10% for the first split second but goes back to normal. Here’s the two functions Im doing.


def Shoot(ammo):
    ammo -= 1
    own.parent['ammo'] = ammo
    randXv = random.randrange(0,10)
    randZv = random.randrange(0,10)
    shooter.object = 'Bullet'
    shooter.linearVelocity = [randXv,-75,randZv]
    shooter.time = 50
    shooter.instantAddObject()
    lastObj = shooter.objectLastCreated
    lastObj['owner'] = own.parent

def Fire(ammo):
    ammo -= 1
    own.parent['ammo'] = ammo
    randXv = random.randrange(0,10)
    randZv = random.randrange(0,10)
    created = sce.addObject('Bullet', own)
    created.setLinearVelocity((0,-15,0),True)
    created['owner'] = own.parent

instantAddObject() returns the reference to the newly created KX_GameObject too!

so you can shorten


add.instantAddObject()
ob = add.objectLastCreated()

to


ob = add.instantAddObject()

add.objectLastCreated()
is usefull if you want to to identify the created object after the actuator created the object without python.

I always forget Monster!

The sce.addObject() function shouldn’t increase logic that high. Is it working correctly? Are there any errors in your console? It might be the setLinearVelocity() line - try replacing the ‘True’ with 1.

Yea some other part must have been the problem, I didn’t touch thy part redid another part and it doesn’t hog any logic anymore

Just a note, i revised a bit of your code (to optimize it)

def Shoot(ammo):
    ammo -= 1
    own.parent['ammo'] = ammo
    randXv = random.randrange(0,10)
    randZv = random.randrange(0,10)
    shooter.object = 'Bullet'
    shooter.linearVelocity = [randXv,-75,randZv]
    shooter.time = 50
    lastObj = shooter.instantAddObject()
    lastObj['owner'] = own.parent

def Fire(ammo):
    ammo -= 1
    own.parent['ammo'] = ammo
    randXv = random.randrange(0,10)
    randZv = random.randrange(0,10)
    created = sce.addObject('Bullet', own)
    created.setLinearVelocity((0,-15,0),True)
    created['owner'] = own.parent