Random Spawning Objects

I want to be able to randomly choose one of three objects in one of three colors to come down a conveyer line. How would I get these objects to randomly spawn at the beginning of the conveyer?


You’d have to use a python script using the random module.

import random as rand
r = rand.choice([1, 2, 3])
print r

Good luck,

Could you show me how to implement that? I am a bit of a beginner when it comes to the game engine.


On the object that adds the objects, apply these logic bricks:

Always(f:50) -> Python(Text) -> EditObject[AddObject]

“Text” needs to match the name of the Python script (it says at the bottom of the text window (Shift+F11), default is Text). In the script, write this:

import GameLogic as g
cont = g.getCurrentController()
add = cont.getActuator("act") #or whatever the adding actuator is
x = 3
r = int(g.getRandomFloat()*x)+1
if r == 1:
elif r == 2:
elif r == 3:

The script first generates a random number. Depending on that number, it changes the object to be added and then fires the actuator. Because the script is run every 50 frames (the “f” in the Always sensor), an object is added every 50 frames. This can be changed. Also, you can change “x” in the script above: x is the number of objects to choose from.

seems logical