[SOLVED...or kindoff]How to add objects to the scene using bge script

Hello, I post this in order to ask a question. In another post I asked about how to remove contraints between a Rigid and a SoftBody, and as long as I searched and tried it is not possible. To achieve the behaviour that I want, I’m trying to delete the green object and then add it again to the scene in the same position that it was located. But I’m not quite sure about How to do it. I attach the .blend that I’m using to test this functionality. When you press the spacebar the game tries to delete the green cube and then tries to add it againt with no result at this time. Please could you give me any clue?
BGE-Physics-DynamicallyCreateConstraint.blend (390 KB)


BGE-Physics-DynamicallyCreateConstraint.blend (356 KB)

If you remove an object it is gone. You can’t create a copy of it as there is no source anymore. This is the reason why a source object is usually placed at an inactive layer.

So, have it on another layer,

have a property like “Spawn”

and have a delay initialize it the first time, (add object)

and then an event delete it,

and another event re-add it?

Allright I took notes of your suggestions. Thanks a lot, I tested this and it worked. The only thing is that I need an Empty or Empties objects to set the spawn points on the active layer.Is that right? Thanks a lot four your time. ASAP I code the final solution I’ll post it and mark the post as solved.

u can have one empty, with prelocated coordinates, so u can move the empty to the right spot before you add an object to the empties location

You need at least one game object as “emitter”. This is usually not a problem as you have at least a camera with is a game object too.

For better organization I recommend to have a separate object (empty) as you already do. And yes you can move the empty and and you can move the added object as well.

Also you can adjust your object location after you’ve added it. Useful for particles to get random emission point for example

myobj = scene.addObject("dust", cont.owner, 100)
myobj.worldPosition.z += random.random()

I’ve finnally achieved the functionality of adding and removing objects from the scene. As you suggested I created an emiter object in the main layer so I can translate it whenever I want to spawn a new object.Since the name of the object is not editable I added an attribute to each object to identify them so I can manipulate them later.

But it doesnt solve at all my problem. I’ve chosen this strategy because the principal problem was that it is not possible to remove Constraints betwen a SoftBody and a RigidBody, at least not using removeConstraint from the module constraint. I know this because if I just remove the object and then add it again, the constraint remains.

I attach another test againt. In this case you have the 3 strategies I already tried. When the “game” is running just press spacebar to run the behaviour test

1- Blue Cube (Rigid Body) vs Green Cube ( Rigid Body) constraint.removeConstraint
2- Red Cube (Soft Body) vs Green Cube ( Rigid Body) constraint.removeConstraint
3- Yellow Cube (Soft Body) vs Green Cube ( Rigid Body) removes object and add it again(purple cube)

The behaviour I want to achieve is the case 1 but with a soft body object . Thanks a lot for your help
BGE-Physics-DynamicallyCreateConstraint.blend (522 KB)

Ok, Sorry for the autoreply. I write to close this thread. I’ve got a quite neat solution for this.
The goal was to create a functionality where the user was able to modify the joints between a SB and a RB. I tried several strategies, for example the one asked in this toppic. I found that is not possible to remove a Constraint which implies a soft body. I also learned how to manage objects from inactive layers to add them to the main scene and manipulate an Empty object as a “spawner” but it didnt help to achieve a solution to the problem.

Combining my new bge skills I’ve created a function where the game replaces the SoftBody mesh with a static mesh and creates a reference on the joint position so the user can interact with it and change its position using this static mesh as a reference. When the user finish, he or she can restart the game sending the new position of the joints through the globalDictionary. The game realizes that is not a new game but a restarted game and set the joints before it starts, et voila. I dont know why is not possible to remove the constraints but I think its a good aproximation to the solution I initially wanted. What do you think?

Thanks a lot for your time, I’ve learnt a lot from you guys