0
$\begingroup$

has anybody an idea why this script doesn't work? The object "Sphere" is on a separate layer.

"addOne" is a property who generates random integers from 1–10.

import random
from bge import logic
import bpy


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


if own["addOne"] == 1:

unitll here the script does work

    scene.addObject("Sphere")

or on the other hand, if I run a script that says for example

bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=2, view_align=False, enter_editmode=False, location=(-24.4838, 0.107069, 1.23061e-07), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

the cylinder doesn't apper in the game itself but only in my 3d view (where you actualy work). Can anybody help me?

thank you in advance, Sputi

$\endgroup$

1 Answer 1

1
$\begingroup$

First point is that bpy is not available while the game engine is running, similarly bge is only available when the game engine is running.

When developing in the game engine you might find using the console to be helpful. In the console you will see the following -

Python script error - object 'Cube', controller 'Python':
Traceback (most recent call last):
  File "Text", line 11, in <module>
KeyError: 'value = gameOb[key]: KX_GameObject, key "addOne" does not exist'

To expand on that, you have set own as the owner of the script controller that is running the current script. The syntax of own["xxx"] will give you access to a game property called 'xxx'. I expect you want to access a sensor that you have named 'addOne', for this you want to use cont.sensors['addOne'] and you may want to use it's positive property.

Also scene.adObject takes at least two arguments, the second being an object to use as the initial location when created - empties are good for this.

import random
from bge import logic

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

if cont.sensors['addOne'].positive:
    scene.addObject("Sphere","Empty")
$\endgroup$

You must log in to answer this question.

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