Add random object from a list, how?

So, I’m looking for a python script that will randomly add an object from a list. So the python will link to an add object actuator. In the python script will be a list of object names (For example “OBCube1” “OBCube2” “OBCube3” ) then the python will select one of these randomly.

  I was thinking that each object would have an index and the random function would spit out a number (Between 1 and 5 say) and the object with the matching index would then get passed along to the add object actuator.

  I would do this with logic bricks, but as this is going to have to be done multiple time for many different places, it will get very hard to keep track of with logic bricks. I really need help with this because I really don't know python that well. This could have numerous uses and might be good for the resource forum if it works! :)

I just made this script. It’s not quite perfect though.

For some reason the random function chooses two objects. Also, it can’t access objects on other layers.

If anybody could fix those problems, then it would be perfect.

Attachments

add_random_object.blend (140 KB)

THANK YOU! this should work fine for what I need. Hehe, when I first downloaded it it didn’t work. Then I realized that I had accidentally deleted python from my computer when i was cleaning it up >.> Btw, I figured out why it adds two at once. It adds one for the down stroke of the keyboard as well as the upstroke! :eek:

Even though it’s set to tap? I thought it gave only one trigger when it’s set to tap. Hmm…

well, even though you set it to tap, tap still gives out an on and off pulse I guess. I took tap off to test it, that’s what made it so visible to me.

I didn’t know that a negative pulse made the script run. I’ll have to remember that one.

It makes sense if you think about it.


# Assuming there is only one sensor attached to this script controller

if sensor.positive:
    # Do something
else:
    # If the script didn't run on negative pulses, this code would never run

Also, regarding your example: I think it would be better if you used a per object property to differentiate between objects that can be added, and those that cannot.

In that case, you could just do it like this:


objects_to_add = []
for obj in scene.objects:
    if "prop_name" in obj:
        objects_to_add.append(obj)

Or, alternatively, you could use a list comprehension:


objects_to_add = [obj for obj in scene.objects if "prop_name" in obj]

Is there a way to negate the negative tick? It’d be nice if it would only spawn one object. I mean, I could get away with having the 2 spawn, but it would much more effective if only one spawns. Also, is there any way to get it to spawn from a separate layer or scene?

Just put your spawn code under “if sensor.positive:”, and it will only run on the positive pulse.

About spawning from a second layer: scene.addObject() should be able to spawn from any layer, even the current active layer, while the add object actuator can only spawn from inactive layers.

I don’t think there is a way to add an object from a different scene.

sorry to keep being such a noob, but if you could show an example of how to add the scene.addObject thing? I tryed to look it up and do it myself, but I just don’t know how.:o
Thanks for all your help.

Here you go:

Attachments

add_random_soc.blend (139 KB)