Post tutorial RSS Level Editing Scripting by Prospero. Part. 9

Things a bit quiet? Sounds and Music add alot of atmos to a map.

Posted by on - Basic Client Side Coding

Tut 9. Sounds.

Things a bit quiet? Sounds and Music add alot of atmos to a map.

In mapping terms, Sounds and Music are different things. Music is not 'heard' in the map as such. It is like the music in a movie. Sounds are localised and have a position in the map and are louder the nearer the Player is to them.

Sounds need to be created in a similar way to objects,etc. If you intend using lots of them, make a seperate file. Name it or (to keep it in its native language. ) Yes, there is a source file .
Exec the file in as before.

This is the code to create sounds:

python code:
import Bladex

The filepath to the sound file in the Sounds folder is included in the code. There are a lot of sound files and most of them have Spanish names, but you soon figure it out. (golpe = hit for example).

What happens after you have created the sound? Nothing . You have to play it with a function call:

python code:
snd_bang1.Play(49000, 6000, -58000,0)

This code is usually put in a function to correspond with an event.
The first three arguments are the map coords for the focus of the sound. The last 0 is the number of times the file will be played, in this case once. (put 1 and you get 2 bangs. ). Some sounds are looped so that they can play continuously. For a looped sound (wind, waterfall, fire, etc) put -1.

Sometimes you want a sound to play at the Player position. You may not know where exactly the Player may be. You can 'get' the current Player position (and anything else for that matter) with this:

python code:

You might be wondering why all the[]s have appeared. Why not just put charpos. Well, the variable charpos has three values stored. You have to unpack them. So charpos[0] acesses the first value (the X coord), etc. You could do it like this:

python code:

Which is probably neater. There is usually more than one of doing things in Python.

Sounds have many Attributes. Some I am not sure exactly the purpose of. The useful ones are:

python code:

Which are self explanatary. (I am not sure whether sounds get any louder if you set the Volume greater than 1.0)

There is also Pitch, Which can be useful to vary sounds slightly.

There is a function that allows you to play sounds at random intervals.

python code:


It includes two extra args, 20,8. The sound will play at a random interval somewhere between 20 and 8 seconds. Set these values to your own preferences.


The backgroud music is all defined in a file ''. This is a key file and must have that name for savegame integrity. Exec it as usual in

python code:
import Bladex

# for .wav files

# for mp3s

Bladex.AddMusicEventMP3( is the same

There are 6 args follwing the filepath. In practice you only need pay attention to the last one. Like Sounds it controls the number of times the file is played. 0 for once, -1 continuous.

You start the music with this:

python code:

this call is usually contained in a function.

To stop whatever music is playing:

python code:

You usually have to 'kill' whatever music is playing before starting a new track.


You can assign music to enemies that starts to play when they go into Combat mode. Use the same file but:

python code:
import  DMusic


You can assign it to individual enemies or a whole Kind.

I haven't quite figured all the args yet, but the first is the priority. If you have lots of enemies in a group, set this value slightly lower for each one so that they don't all try to play their music at once.

If there was a piece of background music playing when you started fighting the Ork, it will not resume after he is dead. His combat music will stop and there will be silence. I made a little routine to restore the music (if any).

In DefFuncs:

python code:
CurrentMusic=""  # declare a variable and assign and empty string ""

def ChangeMusic(music):
    global CurrentMusic

#then, start all music with:


A word on global variables. If you need to access a variable within the body of a function, you must declare it as a global or it will not be recognised. With this func you can change the music at any time and the identity of the music is stored in the CurrentMusic var.

Then if you happen to have to inerrupt the music for any reason, you can restore it with:

python code:
def ResumeMusic():
    global CurrentMusic
    if CurrentMusic != "":

# to clear the var if necessary

def StopMusic():
    global CurrentMusic

This func is most likely to be called after the death of an enemy. Then, whatever music was playing when you started the fight will carry on after enemy is dead. For this you have to tap into his ImDead function. I'll come to that later.

You can either use the Sounds library, or you can make your own. To use your own stuff, just alter the filepath to your own file:

big truck. Glad you like them, but don't spirit them away too soon as they sometimes need de-bugging.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.