0
$\begingroup$

I am making a third person shooter and I have three different lazer sounds. I want to figure out how to make it so every time I click it plays a different lazer noise.

$\endgroup$

1 Answer 1

0
$\begingroup$

You can use a python module to choose different sounds to play. The advantage of a module over a script is that a module can have variables that are reset only when the game starts while a script they will be reset each time the script is run.

To treat a text block as a python module you need to end its name with .py and enable the register option in the header. Then you add a python controller and use the text block name without the .py and the function name to run.

using a python module

Now in the module you need a function to play the sound. getCurrentController will give you the controller running the script and its actuators property is a list of actuators connected to it. We can then use an index to access each item.

import bge

idx = 0

def playsound():
    global idx
    cont = bge.logic.getCurrentController()
    if idx >= len(cont.actuators): idx = 0
    snd_act = cont.actuators[idx]
    cont.activate(snd_act)
    idx += 1

The actuator list can also be treated as a dictionary, so you can choose the actuator by name -

snd_name = 'Sound' + str(idx)
snd_act = cont.actuators[snd_name]
$\endgroup$

You must log in to answer this question.

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