random postion

How would you randomly spawn a enemy at a different position every time you replay the game?:eyebrowlift:

Make a list of spawn points, from a list of coordinates OR using a mesh, and take the vertices as coordinates. If you look into my plugins.py for the multiplayer addon, there is are two files you need; plugins.py and wrapper.py
wrapper.py runs the script, and you set it up as follows:

Always (Single Pulse) -> Python Module Controller (wrapper.run)
Property: components
Value: plugins.random_spawn
Type: String

Property: plugins.random_spawn
Value: {“Spawn Mesh”:“Plane”,}
Type: String

The speech marks have to be used.
It should work provided that you include both wrapper.py and plugins.py are in the text window.
Change the word “Plane” to “Name of Spawn Mesh”
Each vertex in that object will act as a spawn point.


import bge

class KX_PythonComponent:
    '''Spoofs the KX_PythonComponent as a blank class

def init(self, args, object):
    '''Provides a custom init function (as there is none)
    and sets the instance.object to the script owner
    self.object = object

def run(cont):
    '''Wrapper for Moguri's components
    Will take all components in property, import them
    and execute their instances whilst providing custom args'''
    own = cont.owner
    name = cont.name
    components = own['components'].split(', ')
    if not "imported_components" in own:
        setattr(bge.types, 'KX_PythonComponent', KX_PythonComponent)
        own['imported_components'] = []
        for component in components:
            source, module = component.split('.')                        
            imported = __import__(source)
            comp = getattr(imported, module)
            comp.__init__ = init
            args = own.get(component, comp.args)
            args = eval(args)
            instance = comp(args, own)
    for component in own["imported_components"]:            


class random_spawn(bge.types.KX_PythonComponent):
    """Send object score"""
    args = {
    "Spawn Mesh":"Plane",
    def start(self, args):
        objects = bge.logic.getCurrentScene().objects
        map = objects[args['Spawn Mesh']]
        mesh= map.meshes[0]
        verts = mesh.getVertexArrayLength(0)
        self.nodes = []
        for v in range(verts):
            vert = mesh.getVertex(0,v)
        spawn_points = self.nodes
        randomkey = random.randint(0,len(spawn_points)-1)
        spawn_point = spawn_points[randomkey]
        self.object.worldPosition = spawn_point
    def update(self):
        own = self.object
        if 'respawn' in own:
            if own['respawn'] == True:
                randomkey = random.randint(0,len(spawn_points)-1)
                spawn_point = spawn_points[randomkey]
                own.worldPosition = spawn_point
                own['respawn'] = False        

Okay thank you.

If the property field is red it means the property is not set up (in GUI).

Unfortunately you cut this part out on your picture. The game properties are defined left of the logic bricks. You might need to expand the property view first (small + at the left border).

Please add an according Game Property (abc) with int or float type.
In actuator Action2 the value should be 3 rather than “position 3” which is a string.

My suggestion:

define an action with several location keys (e.g. in range from 1 to 10)
define a property “randomFrame” as int with value 0 (we will interpret 0 as “not set”)

property sensor “equal” randomFrame 0
–> And
----> RandomActuator (range 1…10)
----> ActionActuator Property mode Property: randomFrame (!!!not frameProp!!!)

this let the object choose and set a random pose each time you write 0 into the game property

I hope it helps

Thank you for your help. I am a beginner, your answer is helpful for me.
The property’s logic became more an more clear.
I do something try again, but I do not know where is the fault? (Plaese see the picture )
(1) define an action with several location keys (e.g. in range from 1 to 10)
---- I define 3 postions in simple_montion when randomFrame value is 1/2/3.
(2) let the object choose and set a random pose each time you write 0 into the game property
---- I want to let the object choose and set a random pose each time my mouse click screen anywhere.
But nothing happen.
And I don’t know what’s the Empty object can do? (in your #3 reply)
I insert 3 keyframes for the Empty at 3 location and hope Cube can place on it…maybe it’s not necessary?

I got it after make Cube parent to Empty,
but use montion is a mistake, the Cube fly away…
How to define an action with several location keys?

Thanks Monster point me in the right direction, I solved all problem of random postion now.
In the pictre, I use a Empty Object (not necessary), and make Cube parents to Empty Object (not necessary).
the key is:
(1) Insert location keyframe of Cube at where you want it postion in 3D View.
(2) Define randomFrame property, each value connect to ActionActuator Play, start and end at each keyframe .
(3) Use mouse or keyboard or others start ActionActuator Random,
ActionActuator Property mode’s Property: randomFrame (!!!not frameProp!!!)
So, when click mouse, randomFrame will chioce in 1~3,
and then 1 or 2 or 3 will play the keyframe from 1-1 or 2-2 or 3-3,
the Cube will display at the postion as same as the keyframe I insert before.
------ Sorry my English is not well… -----