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 < 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