Transfer Python code from Blender Render to Blender Game Engine I have a script that randomly spawns objects in blender render.
Now is there an easy way to transfer that code so that it works as a "game" in blender game engine? Or do I need to start from scratch anyway?

(In the end I want to have a "game that plays itself and has a different outcome each time it runs through" but looks like its just an animation. So I figure I need to have the script working in blender game engine.)

Here's the code in case it may help:

import bpy

# this is for the random position, imports the random() function from random library
from random import random

# this holds how many cubes were created and what the cap is
spawned_cubes = 0
max_cubes = 20

def spawn_object(scene):
    """This function will be run everytime before frame changes because it is added in the callback list"""

    global spawned_cubes
    global max_cubes

    # read the current frame number
    # if it is 10 and number of spawned cubes is less than max_cubes place a cube
    # if the frame number is not 10 just end and do nothing
    if scene.frame_current == 100 and spawned_cubes &lt; max_cubes:
        loc = (20-40*random(), 18-0*random(), 20-20*random())
        bpy.ops.mesh.primitive_cube_add(enter_editmode=False, location=loc)
        spawned_cubes += 1
    return

# add spawn_object function if there isnt one in bpy.app.handlers.frame_change_pre callback list
# the function needs to be added there only once
# everything thats in that list will be executed before any frame change
if spawn_object not in bpy.app.handlers.frame_change_pre:
    bpy.app.handlers.frame_change_pre.append(spawn_object)


Thanks,
Sputi
In the logic bricks you can add python scripts under "Controllers".  You would need to set a sensor "Property" or two that holds something to indicate the start/end of the game.  Likewise you will need to set that "Property" at specific times to achieve what you want.  "Restsrt Game" is under Actuators.

You may have to modify your code but you can do a majority with Logic bricks and sensing "Properties".  You can do something similar with changing cameras at certain times.

For detecting frames, just have your script set a Logic Brick property each frame change, you will need to keep an eye on your FPS as large scripts can slow you down.