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 mysounds.py or sonidos.py (to keep it in its native language. ) Yes, there is a source file Sounds.py .
Exec the file in cfg.py as before.
This is the code to create sounds:
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:
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:
You might be wondering why all thes have appeared. Why not just put charpos. Well, the variable charpos has three values stored. You have to unpack them. So charpos acesses the first value (the X coord), etc. You could do it like this:
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:
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.
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 'MusicEvents.py'. This is a key file and must have that name for savegame integrity. Exec it as usual in cfg.py.
# for .wav files
# for mp3s
Bladex.AddMusicEventMP3(....rest 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:
this call is usually contained in a function.
To stop whatever music is playing:
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 MusicEvents.py file but:
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).
#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:
if CurrentMusic != "":
# to clear the var if necessary
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.