Thread: Changing Music
View Single Post
Old 09-30-2005, 10:49 AM   #2
Darth333's Avatar
Status: Administrator
Join Date: Feb 2004
Location: In my secret dungeon...
Posts: 8,295
10 year veteran! 
Changing music in an area or during a certain event

( this post summarizes the discussion of this thread: )

1. Adding your custom track to the game:

It is as simple as adding a line to ambientmusic.2da and dropping the new track in .mp3 format in the streammusic folder. It is also possible to replace an existing track. (more details to come)

2. Changing the ambient music and battle music tracks in an area:

It is possible to edit the .git file of an area as described above by Fred Tetra with K-GFF (make sure you are using this gff editor made especially for Kotor and TSL as other editors will corrupt the file) and repack the module with Kotor tool's ERF builder. However, this method is considered overkill when not creating a custom area but simply editing an existing area. When editing the music of an existing area, we recommend using scripts.

(example by stoffe -mbk- )
void main() {
    object oArea = GetArea(OBJECT_SELF);
    // Index in ambientmusic.2da
    int nAmbientMusic = 3;
    int nBattleMusic = 47;
    MusicBattleChange(oArea, nBattleMusic);

    MusicBackgroundChangeDay(oArea, nAmbientMusic);
    MusicBackgroundChangeNight(oArea, nAmbientMusic);
The above script changes the ambient (non-combat) music in the current area to Darth Sion's theme, and the battle music to the Dxun Jungle battle track. Use MusicBackgroundPlay() to play the background new music or MusicBattlePlay() to play the battle music again.

The numbers you specify are indexes in the ambientmusic.2da file (for both ambient and combat music).

You can use those functions to set the ambient and battle music to play the same track in the AreaEnter script or somewhere else suitable. Note that using the AreaEnter event will only change the music until the first battle finishes: after the battle, the music will revert back to the original area tracks.

To avoid reverting back to the old tracks, a script like this one could be inserted in the OnHeartbeat event of the area:
(example by Darth333)
void main()
int nTrack= MusicBackgroundGetDayTrack(oArea);
if nTrack!= 3
	MusicBackgroundChangeDay(oArea, 3, FALSE));
The Heartbeat is an event that usually triggers once every round (every 6 seconds) for some types of objects. The Module, areas, placeables, creatures, triggers, doors etc all have Heartbeat events, while some objects, like items, waypoints and stores/merchants, do not. Heartbeat scripts are useful for things that must be done (or checked for) continually. The creature AI uses them for playing ambient animations (looking around, scratching head etc) and for following you around, for example.

3. Changing the music temporarily during a cutscene or other event:

The music played during a cinematic can be specified in the AmbientTrack field of the .dlg or using a script. If you modify the sound file, the music should change in the cinematics as well.

Here is a sample script that will change the music after 3 seconds and revert back to the original track after 45 seconds:
(by Darth333)
void main()
//Check what's currently playing - lazy method but it's usable in any module 
// and there is no need to manuallt check what's playing
    int nTrack = MusicBackgroundGetDayTrack(oArea);

//Play the desired track - replace number 4 by the ambientmusic.2da row corresponding the the track:: 
    DelayCommand(3.0,MusicBackgroundChangeDay(oArea, 4, FALSE));

// Back to the old track:
    DelayCommand(45.0,MusicBackgroundChangeDay(oArea, nTrack)); 

Want to go fancy and add some fade in/out effect during your cutscene?
Here is an example provided by stoffe -mbk-
void main() {
    SetGlobalFadeOut(0.0, 5.0);
    DelayCommand(5.0, MusicBackgroundStop(GetArea(OBJECT_SELF)));
    SetGlobalFadeIn(6.0, 0.5);  
That should fade to black for 5 seconds. When it reaches black after 5 seconds, it stops the ambient music and fades back in.

You'd have to tweak the timing for when the music stops manually, since there is no way of checking in a script if the music has stopped yet.
Darth333 is offline   you may: quote & reply,