Change sound actuator sound with python

Simple question.
The following script can change the pitch of the sound actuator on the fly.

cont = logic.getCurrentController()
own = cont.owner
actu = cont.actuators['Sounds']
actu.pitch = 2

Can I similarly change the sound it’self?
Is there a line like:
actu.Sound.name = 'sound_I_want_to_play.wav' ?

Thank you all.

i think so:

if you run this code:

import bge 
cont = bge.logic.getCurrentController()

print(dir(cont.actuators["Sound"]))

it will print you the variables and functions of the actuator:

[‘class’, ‘delattr’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘lt’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘attenuation’, ‘cone_angle_inner’, ‘cone_angle_outer’, ‘cone_volume_outer’, ‘distance_maximum’, ‘distance_reference’, ‘executePriority’, ‘invalid’, ‘is3D’, ‘mode’, ‘name’, ‘owner’, ‘pauseSound’, ‘pitch’, ‘sound’, ‘startSound’, ‘stopSound’, ‘time’, ‘volume’, ‘volume_maximum’, ‘volume_minimum’]

as you can see, there is “sound” in it.

Have not tried it but i guess you could just give the path to “sound”

sound = "the_path_to_your_sound"

EDIT: Sorry, that doesnt work… need more time to figure it out

import bge
def main(self):
  self.actuators["Sounds"].sound = "sound_I_want_to_play.wav"

https://upbge.org/#/documentation/docs/latest/api/bge.types.SCA_SoundActuator.html#bge.types.SCA_SoundActuator.sound

You may need to use the AUD module as well to properly recognize the other sound. Can’t remember if that was optional or mandatory. In any case, feel free to share if it does and I or others can share some more code.

Every time 'def" shows up in python, I get lost…

from bge import logic
cont = logic.getCurrentController()
own = cont.owner

def main(self):
    self.actuators["Sound"].sound = "Door_open_5.mp3"\
    
main(self)
cont.activate("Sound")
cont.deactivate("Sound")

If I try and call the function, with main() it says "required posttransition argument ‘self’ required.
If I try and use main(self) to call the function, it says name ‘self’ is not defined.

If you cannot tell, I lack a fundamental understanding pf python, which I really should have by now, since I’ve been using the code weekly for a decade…

Even when I simply do:

import random
from bge import logic
cont = logic.getCurrentController()
own = cont.owner


cont.actuators["playsound"].sound = "Door_open_5.mp3"
cont.activate("playsound")
cont.deactivate("playsound")

It returns the error “object is not of type factory!”

I have much to learn. :stuck_out_tongue:

This is what I warned you about above. Here’s how to fix that:

UPBGE 0.2

from bge import logic
# UPBGE 0.2
from aud import Factory
def main():
    cont = logic.getCurrentController()
    own = cont.owner
    actu = cont.actuators['Sounds']
    # Sound - Path
    path = logic.expandPath(str("//untitled.wav"))
    # Sound - Load | UPBGE 0.2
    actu.sound = Factory(path)
    # Sound - Pitch
    actu.pitch = 2
    # Sound - Pitch
    actu.startSound()
main()

UPBGE 0.3

from bge import logic
# UPBGE - 0.3
from aud import Sound
def main():
    cont = logic.getCurrentController()
    own = cont.owner
    actu = cont.actuators['Sounds']
    # Sound - Path
    path = logic.expandPath(str("//untitled.wav"))
    # Sound - Load | UPBGE 0.3
    actu.sound = Sound(path)
    # Sound - Pitch
    actu.pitch = 2
    # Sound - Pitch
    actu.startSound()
main()

That plays the sound, but there’s no attenuation. It’s the same volume a mile away, as an inch away, regardless of how I change settings on the actuator.

The sound behaves as a 3d sound, and attenuates with distance when I plug in a normal “and” node connected to a property.

The reason I’m trying to do it this way, as opposed to exclusively in aud (as I used to do) is it that I have never managed to make aud produce a 3d sound. Ever.

No error message as well.

AUD doesn’t use 3D positional sound. Here’s how you can achieve it with AUD though:

So why doesn’t the previous method have any attenuation? It’s using logic brick actuators, not aud, right?

The previous method is using a Logic Brick actuator, yes. But it’s calling an AUD sound, which means the sound is controlled by AUD from then on.