How to do Random Animation?

This is a question I’ve been meaning to ask, and since the BGMC starts tomorrow, I decided to finally ask it. So in my game, I want it so that when you press a button, it plays one animation, chosen randomly. Then the next time I press it, it should be so it plays a different animation, also chosen randomly. Is it possible to achieve this effect?

(quick n dirty method, there are better ways but this’ll get you started)


Keyboard Sensor --------- Python Controller ------------ Action 1
                                                         \  \-------------- Action 2
                                                          \--------------- Action 3

In the Python script:


from bge import logic
from random import randint

cont = logic.getCurrentController()

actions = cont.actuators           #list of all actuators noodled to cont

r = randint(0, len(actions)-1)      #random int defines index in actions list

for action in actions:    #make sure we're only playing one action at a time
    cont.deactivate(action)

cont.activate(actions[r])

Thanks a ton! Deffinitely will be of value to me during this BGMC

Thanks, I was getting frustrated for a script to play a random sound. Kept searching for on google and I all I got was random actuator or number rather than getting a script to activate a random actuator.