How to set the spawned object to child of whoever spawned it?

hi every one !
is there is a script to make an object be parented to the emitter/spawner ? knowing that there are multi users of that spawned object , alright ?

spawner = own.scene.objects['spawner']

added_object = own.scene.addObject('object_to_spawn', None, 0)
added_object.worldPosition = spawner.worldPosition
added_object.setParent(spawner)

Your wasting a extra-line of code there.

spawner.worldPosition added_object.setParent(spawner)

You can simply do –

spawner = own.scene.objects['spawner']

added_object = own.scene.addObject('object_to_spawn', spawner.worldPosition, 0)
added_object.setParent(spawner)

– instead.

Why not make it another line shorter?

added_object = own.scene.addObject('object_to_spawn', own.scene.objects['spawner_object_name'].worldPosition, 0) 
added_object.setParent(own.scene.objects['spawner_object_name'])

Or even more shorter.

added_object = own.scene.addObject('object_to_spawn', own.scene.objects['spawner_object_name'].worldPosition, 0); added_object.setParent(own.scene.objects['spawner_object_name'])

But joking a side, why waste a function’s function if it was originally created to serve that purpose.

for customization, vec+vec can’t be done in that string

@Cotaks @RandomPerson , i don’t understand any thing here , any explanation for a programming noob ? :joy::sweat_smile::sweat_smile:

def spawn_object(cont):

    own = cont.owner
    spawner = own.scene.objects['spawner']

    added_object = own.scene.addObject('object_to_spawn', None, 0)
    added_object.worldPosition = spawner.worldPosition
    added_object.setParent(spawner)

hook this up with a python controller, set it to module, enter the scriptname.spawn_object. Make sure the scriptname in the text editor end with .py

1 Like

Except for the .worldPosition functions that I stated, my script works for me.

Proof.

Script.

import bge

def main(cont):
    
    if bge.logic.keyboard.events[bge.events.SPACEKEY] == 1:
        obj = cont.owner.scene.addObject("Suzanne", cont.owner.scene.objects["Cube"]); obj.setParent(cont.owner.scene.objects["Cube"])

Blend-File.
Vec+Vec.blend (106.5 KB)

1 Like

i tried that with the child object , nothing interesting happens , could you explain more ?

your method is good for single user , but i am searching for multi user , so the spawned object follows who ever spawned it

did you edit the ‘spawner’ and ‘object_to_spawn’?

i did but nothing happen , could you please upload an example ?

spawn cube parent after.blend (505.7 KB)

space to add cube, upbge 2.5

1 Like

Mr.Cotaks , i have downloaded the .blend , duplicated the empty with the script but the cube still spawn at the same postion , why :thinking: ?

change the name in script to the correct name, the spawner is called spawner if you copy it it will be most likely spawner.001

1 Like