Set objects with a loop

Hi Community,

i’m trying to create a little game with Blender Game Engine and despair with the python script.

The script should place 10 objects in a line. It has a while loop and every time it should activate the “edit object” actuator. After that it moves the empty a step forward and repeats.
My problem is that if i run the script, it moves the empty 10 times and after that it places one object at the end of the way. But I want that it places a object the whole way. Some ideas?

import bge
import GameLogic

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

message = cont.sensors['Message']

editblockA = cont.actuators['Edit0']
editblockB = cont.actuators['Edit1']
editblockC = cont.actuators['Edit2']

def floor():
    
    cont.activate(editblockA)
    cont.activate(editblockB)
    cont.activate(editblockC)
    
    own.position.y += 2.0 

if message.positive:
   x = 0 
   while x < 10:
      x += 1
      floor()

Always difficult to debug a BGE script without the scene, but just from looking i think a problem is in your floor() method. You activate all three controllers in that method, so when you call floor() in your loop, you activate all three controllers every loop.
If you want to activate one controller, then the next, then next, etc, you should make a list of controllers and iterate through that:


MyCtrlList = []
MyCtrlList.append(cont.actuators['Edit0'])
MyCtrlList.append(cont.actuators['Edit1'])
MyCtrlList.append(cont.actuators['Edit2'])

while x < 10 :if x < len(MyCtrlList) :
[INDENT=2]cont.activate(MyCtrlList[x])[/INDENT]
x+=1

Hm, this doesn’t realy solves my problem.:frowning:
In my case i want to create a longer street of floor with my script for a little game. Later there would be more objects to add. But for the moment the script should add a longer way if I execute it. But everytime I start it, the loop works, but then it only places one “street part” at the end of the way and not the whole way.

This is how it looks if i start it some times.


The problem must be something with the loop because if I set the loop to 1 it works fine.
If you need more information, than tell it to me.

Does nobody know what I can do? It would be nice if I can use this script in this or a likewise way.