3
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ I am not sure if it can be done, but I know you can do this with logic bricks fairly easily. $\endgroup$
    – J Sargent
    Commented Jan 12, 2015 at 15:33
  • $\begingroup$ camera change does work with logic bricks? Do you have a hint for me? $\endgroup$
    – Sputi
    Commented Jan 13, 2015 at 8:17

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ it works quite well, thanks, however what doesnt work is to add an object on a random position like in the script above. If I add an object with the add actuator the object spawns just at the position of the object who owns the actuator. Any ideas? $\endgroup$
    – Sputi
    Commented Jan 28, 2015 at 14:36

You must log in to answer this question.

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