How to instanciate a mesh in a python script?

Hi,
I would like to code a puzzle game using the Blender Game Engine.
At start of each level, I have to generate the it through a python script.
The size of each level will be approximatively 20x20 tiles.
Each tile might be a wall, a floor or something else.

In blender, I will then create an object ‘wall’, an object ‘floor’, etc…

My question is how can I instanciate those objects using python code?
For instance, what will be the Python code to create a matrix of 2x2 ‘wall’ objects?

try this:
make an empty, and give it the properties of “x”,“ontile” and “y” and set them all to one.
then make a wall and a floor tile, name them “wall” and “floor”
set an always on a python script, and give it an edit object add object with a start of “wall” and name it “gen”
take a duplicate of your floor tile and put it at 1,1.
and use this code:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

generator = cont.getActuator("gen")

if own.ontile <100:
       tiletype = int(GameLogic.getRandomFloat()*2)
       if tiletype == 1:
              generator.setObject("wall")
              GameLogic.addActiveActuator(generator,1)
              own.ontile +=1
       if tiletype == 0:
              generator.setObject("floor")
              GameLogic.addActiveActuator(generator,1)
              own.ontile +=1

       if own.x == 10:
              own.x = 1
              own.y += 1 #change this to + 2 for 2x2 tiles
       elif own.x<10:
              own.x += 1 #change to + 2 for 2x2 tiles
       own.setPosition(own.x,own.y,0)

your tiles need to be 1x1 for this code.

Thanks, It works like a charm :slight_smile:

I got another question: How would you handle the switch between levels?
I there any way to delete all previously generated instances of the wall/floor?

OK, I finally manage to find how to delete the object afterwards, by creating a set of the objects and calling the member function endObject on them.