3
$\begingroup$

I'm programming a game, and I have an empty that I want to be dropping enemies randomly throughout the map. I have the following logic bricks and Python script right now:

import bge


def main():

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

    sens = cont.sensors['mySensor']

    if sens.positive:
        own.worldPosition = [own['xPos'], own['yPos'], 30]

main()

logic bricks

My empty is staying in the same place, but it is at least adding enemies every second. How can I make the empty go to the random positions that are being generated by random actuator (I would have used the random module but it wasn't working last time I tried it).

$\endgroup$
1
$\begingroup$

The problem is that your script controller is on the Enemy Dropper object and attempts to reference mySensor which does not exist on that object. If you check the console, accessed by Window > Toggle System Console (only on windows systems), you will see an error.

Traceback (most recent call last): File "Setting Position.py", line 9, in KeyError: 'value = gameOb[key]: KX_GameObject, key "mySensor" does not exist' Python script error - object 'Enemy Dropper', controller 'Python':

To make it operable, add the mySensor sensor to the Enemy Dropper object's logic bricks.

$\endgroup$
1
$\begingroup$

using only the python script (delete the properties and the edit object actuator ) :

import random

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner
    sense = cont.sensors["mySensor"]
    scene = bge.logic.getCurrentScene()

    if sense.positive :
        own.worldPosition=[random.uniform(-10.0,10.0),random.uniform(-10.0,10.0),0]
        scene.addObject("Cube", "Empty", 0)

main()
$\endgroup$
0
$\begingroup$

I would define a navmesh, and then loop through vertices and pick one on the nav mesh, and then do ray checks , so you know the actor will fit.

import bge
cont = bge.logic.getCurrentConroller()
own = cont.owner

mesh = own.meshes[0]

this will get you started down the path, check the python api and let me know if you need more help then this.

own.castRay(To,From,Distance,'property',NormalReturnType,Xray,PolyData) bge.logic.getCurrentScene().addObject('object',object_to_add_@,time)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.