Hello everyone! (Wow, its been a while since i’ve been online…):eek:
I’ve been busy messing around with the blender game engine and recently I started to write a python code to properly loop soundtrack audio into the game engine.
How does it work? Simple: first it plays the intro once, and then it goes on to the main part which it becomes loop-able infinitely.
So far I’ve got the audio to load properly and to work, but the other problem now is to somehow stop the music. I was working on them in different modules so then it can make the music work…:mad:
Here is the code:
import bge from bge import logic import aud # Setting up the controller cont = bge.logic.getCurrentController() obj = cont.owner # Loading up the audio, can be as many as you want device = aud.device() musicFolder = logic.expandPath('//Sound/') intro = musicFolder + '<music_name>_INTRO.ogg' main = musicFolder + '<music_name>.ogg' def bossMusic(): # Using a single module to start playing music # 'Factory' enabled music boss_intro = aud.Factory(intro) boss = aud.Factory(main) # Made the main part Loopable and come after the intro part boss_loop = boss.loop(-1) playlist = boss_intro.join(boss_loop) # This part is used to start playing music while preventing duplicates # if using the pulse mode in the Sensor area, even with 'TAP' enabled. if not 'play' in obj: obj['device'] = aud.device() handle = obj['device'].play(playlist) obj['play'] = 1 def bossMusicStop(): # This module was supposed to stop the music but not working... bossMusic().handle.stop() obj['play'] = 0
the “bossMusicStop” module was supposed to stop the music but I get an error:
NameError: name ‘handle’ is not defined
Is there a way to connect two modules together? or should I re-write the code to make this work?
You can give the code above a try to see how this code works. Use a “keyboard” sensor or an “Always” sensor to activate the code and then type: "<nameofcodeabove>.bossMusic in the “Module” of the Python Script Type in the “Python” Controller. Do the same thing over again for the bossMusicStop module to see the error.:yes: