Duplicating objects in realtime?

Hello all,

I’m playing arround with the game engine and manage to do what I want exepcted this thing :

duplicating objects.

I plan to organise my scene with layers and my base objects and props will be duplicated from an original (on layer 11 for example)

Here is my code :

import Blender, bpy
from Blender import Mathutils

scene = Blender.Scene.getCurrent()
own = GameLogic.getCurrentController().owner

########################
#Spawn mob script...

try:


    if own["monsters"]<3:
        #localisation au hazard
        posx = Mathutils.Rand(0, 2)
        posy = Mathutils.Rand(0, 2)
    
        #on cree une nouvelle instance de notre objet
        mob = Blender.Object.New("Mesh","cedMob")
        mob.link(bpy.data.meshes["MonsterMesh"])
        
        #On le place dans la scene
        mob.setLocation(posx, posy, 1)    #toujours a la meme hauteur par contre
        
        #attachement a la scene
        scene.link(mob)

        scene.update()
        own["monsters"] = own["monsters"]+1
        print "Nouveau cree"
    else:
        
        print "Stop! Deja trop de monstres ont ete crees."
    
    
except Exception, e:
        
    print "Erreur" + str(e)

Sorry for the comments in french.
This works but the objects doesn’t display in realtime, but after the game ends…

Moreover, it’s a bad method, because it links a mesh to a new object, but in the future, it’ll be caracters and I need everything else, like bones and modifiers…
My second question is, how can I clone an object, not simply a mesh?

Many documentations are saying opposing ideaes and it’s confusing,
I’ve read there is a duplicate method for objects, but the only working example is the one I’m testing…

Can someone redirect me to a good reference? I don’t really get the one on blender.org, I can’t understand how it’s organized… in fact, a reference is good after reading a good guide, that’s what I’m missing.

Sorry to be confusing but it’s late and I spent too much time on a small problem like that and I can’t think really clearly anymore.

Thank you for reading, and even more for answering.

You cannot import Blender and bpy in the game engine. That is a common mistake. Those API are for the blender interface only not for the game engine. The game engine has a different separate API, which you can check here:
http://www.blender.org/documentation/249PythonDoc/GE/index.html

For further documentation about the BGE you can check either the official reference at:
http://wiki.blender.org/index.php/Doc:Manual#Game_Engine
or the tutorialsforblender3d site which is very good and well organized:
http://www.tutorialsforblender3d.com/GameDoc/index_GameDoc.html

For duplicating an object you should simply use the add object actuator and add an object of the same type of your current object and maybe then copy it’s properties later:
http://www.blender.org/documentation/249PythonDoc/GE/GameTypes.KX_SCA_AddObjectActuator-class.html

Hi, thank you for your answer and the links.
I know the references for blender and the game engine, I spent my afternoon to try to make them fit together lol :smiley:

I didn’t think to use an actuator to duplicate my object, I’ll look at it.
Thank you for the link,

It’s great I managed to make it work, the object actuator is really powerfull, I have many ideas :smiley:
I encounter another problem, I put my life bar on an overlay scene.
This way the display is what I want, but the sensors connected to the player character on the main scene doesn’t connect the signals any more…
Any clue on this?
Here is a pic to explain better :
http://img199.imageshack.us/img199/1739/blender2009082611324998.th.png
Thank you for your help.

Ok, I managed to script this in python…
But I have another question (yes another…) :
When a collision occurs, the controller is called twice.
The hitObject (I use it to determine the damages from a collid object property) is empty on the second call.
Does anybody know why?

As all sensors you get the negative pulse when the sensor detects it’s condition is not true anymore. Python scripts have to handle that by themselves by calling sensor.positive for positive pulses.
In you situation the sensor tells the script, it does not receive a collision anymore.

I hope it helps.

PS: Please be aware that this questions have nothing to do with the title of the thread. I think, it would be better to open a new one. That might help others to help you or to identify relevant threads when performing a search.

Hi simonced,

also, if you have other “little” questions, you can use the “Simple questions answered. Got a question?” thread.
(Bonne chance au Japon, pour ma part je retourne y vivre dans les mois qui viennent.)

@Monster : Thank you, the positive property saved the stuff, no double call to handle. It’s becomming easier to do things that seems difficult. The title of my post is not really revelant anymore, I thought to change the title, but we can’t, I’ll have to open a new thread then indeed…

@cray : Thank you for the link, this post looks interresting,
On pourrait se rencontrer quand tu seras de retour, c pas tous les jours que l’on a la chance de rencontrer IRL les gens des communautes numeriques, surtout au japon lol.

You can change the title of your thread by editing your first post (maybe in advanced mode).

If you found the Edit-Add object actuator useful, you can also check the python methods to do it :
Class KX_Scene -> AddObject
instantAddObject can be an interesting option, maybe you can check it.

Je serai sur Tokyo car j’y crée une entreprise. Pour quelques années donc. On aura bien l’occasion :slight_smile: