2
$\begingroup$

this is my scritp that spawns random objects and I want to add a counter to it:

def spawnObjects():

    scene.addObject(randomObject1, "empty1", 90)
    scene.addObject(randomObject2, "empty2", 90)

spawnObjects()

print ("Spawned Objects are:", randomObject1, randomObject2)

So the script adds the objects and prints:

Spawned Objects are:, NameObject1, NameObject2

etc.

So far so good. But how can I get python to print:

Spawned Objects 0001 are:, NameObject1, NameObject2

Spawned Objects 0002 are:, NameObject1, NameObject2

Spawned Objects 0003 are:, NameObject1, NameObject2

Spawned Objects 0004 are:, NameObject1, NameObject2

etc.

etc.

Till infinity?

I suppose one needs a counter using "while" loop but I'm still not able to accomplish it the way I described it here. Thanks for any help!

$\endgroup$

1 Answer 1

3
$\begingroup$

Add integer property to the object running this script and keep incrementing this property :

cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()

def spawnObjects():

    scene.addObject(randomObject1, "empty1", 90)
    scene.addObject(randomObject2, "empty2", 90)

spawnObjects()
own['counter'] += 1 # this is the property that keeps count
print ("Spawned Objects %d are:"%own['counter'], randomObject1, randomObject2)

you can add a loop to spawn multiple objects at the same time, you don't use a local variable ( defined inside the script ) because it get reset every execution of the script so you'll lose the count ,unless that is what you want.

$\endgroup$
1
  • $\begingroup$ This works great! Thanks for the explenation an the hint with the properties. Thats what I needed. $\endgroup$
    – Sputi
    May 8, 2015 at 7:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .