Ever thought we can activate and change actuator's value using python. Let's dive.
We'll be activating a sound a changing it's pitch with python. The sound will be played using logic bricks and the pitch will be changed and reset with python.
We import the module, define all those controllers, owners, whatever needs to be done. Now, read carefully, let's define the actuator and sensors. Our object's name will be playa. Add a keyboard sensor and add a python controller with our script selected. Add a sound actuator and select a audio.
We are getting our keyboard sensor renamed as start from the list actuators that belongs to our playa object. This is how we get that sensor like so:
start = own.sensors['start']
And we get our sound actuator renamed as da_sound which is in the list actuators that belongs to the object playa.
da_sound = own.actuators['da_sound']
So the full code looks like this.
import Range
cont = Range.logic.getCurrentController()
own = cont.owner
#sensors
start = own.sensors['start']
#actuators
da_sound = own.actuators['da_sound']
Now we add a keyboard sensors for increasing and decreasing the pitch and volume. Oh we also have to reset out pitch and volume. So we add another five keyboard sensor and connect them with the python controller. So the setup looks like this.
Values I've set for the keyboard sensors are PgUp and PgUn for changing the pitch. Numpad + and Numpad - for changing the volume. Left Shift for resetting the pitch and volume.
We define the sensors and actuators like this...
#sensors
start = own.sensors['start']
pitch_up = own.sensors['pitch+']
pitch_down = own.sensors['pitch-']
vol_up = own.sensors['vol+']
vol_down = own.sensors['vol-']
reset = own.sensors['reset']
#actuators
da_sound = own.actuators['da_sound']
...and we write the logic.
If the user pressed the keyboard to increase the pitch which is PgUp, then we increase the pitch of our sound actuator da_sound. We access that actuator's sound by using the function sound that belongs to KX_SoundActuator class. KX_SoundActuator is nothing but name of our sound actuator.
If we're using it's function which looks like KX_SoundActuator.volume then when we're using our sound actuator, it looks like this.
#logic here
if pitch_up.positive: #If user pressed that keyboard sensor
da_sound.pitch += 1 #increase the pitch of our sound actuator
Lets see the logic part fully.
if pitch_up.positive:
da_sound.pitch += 1 #increase the pitch
if pitch_down.positive:
da_sound.pitch -= 1 #decrease the pitch
if vol_up.positive:
da_sound.volume += 0.25 #increase the volume
if vol_down.positive:
da_sound.volume -= 0.25 #decrease the volume
if reset.positive:
da_sound.volume = 1 #reset the volume
da_sound.pitch = 1 #reset the pitch
Oh also, we should check if the actuator is playing. Each actuators have their own way of checking their state. So for KX_SoundActuator we have:
If you want to check the if the sound actuator is playing, you do something like this.
if da_sound == >Range.logic.KX_SOUNDACT_PLAYSTOP: #if bge then use bge.logic.KX_SOUNDACT_PLAYSTOP
# do something useful
(or)
if da_sound == 1:
# do something useful
So for checking if is playing whether in a loop or just once, we do:
if da_sound == 1 or 2 or 3 or 4:
if pitch_up.positive:
da_sound.pitch += 1
if pitch_down.positive:
da_sound.pitch -= 1
if vol_up.positive:
da_sound.volume += 0.25
if vol_down.positive:
da_sound.volume -= 0.25
if reset.positive:
da_sound.volume = 1
da_sound.pitch = 1
And then do all those pitch and volume changing stuff. This can improve performance by at least 5%. Every single stuff that can be optimized are to be optimized. You can even make this run on a shitty machine too. Some times optimizing your game can get it to next level (if you're lucky lol).
Back to our stuff, we now write the logic for all keys so the full code looks like this.
import Range
cont = Range.logic.getCurrentController()
own = cont.owner
#sensors
start = own.sensors['start']
pitch_up = own.sensors['pitch+']
pitch_down = own.sensors['pitch-']
vol_up = own.sensors['vol+']
vol_down = own.sensors['vol-']
reset = own.sensors['reset']
#actuators
da_sound = own.actuators['da_sound']
#logic here
if start.positive:
cont.activate(da_sound)
if da_sound == 1 or 2 or 3 or 4:
if pitch_up.positive:
da_sound.pitch += 1
if pitch_down.positive:
da_sound.pitch -= 1
if vol_up.positive:
da_sound.volume += 0.25
if vol_down.positive:
da_sound.volume -= 0.25
if reset.positive:
da_sound.volume = 1
da_sound.pitch = 1
Add an always sensor turn on true level triggering for it and make sure to turn on Tap on all keyboard sensors, or adding 1 everysingle can make the game nonsense. Like increasing the pitch or the volume to a very high values within 0.2 milliseconds doesn't make sense at all.
And btw here is the project file. Have a nice day!