Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Akril15 on Sun 04/03/2012 23:45:00

Title: Audio problems when changing rooms [SOLVED!]
Post by: Akril15 on Sun 04/03/2012 23:45:00
I'm working on a game in AGS 3.2.1 that has a lot of rooms (more than 100), many of them with the same music. I've created an on_event function that assigns music to most of the rooms (removing the need to add code to each individual room), but the problem is that if I enter a room with different music than the previous one, the previous track will continue to play along with the current one. Not only that, but if I enter a room with the same track playing, that track will restart from the beginning. Adding Game.StopAudio() right before the music function solves the simultaneous track problem, but doesn't help the restarting track problem.

I've experimented with IsAudioPlaying, adding a boolean variable that determines which exit leads to a room with the same track and a couple other things, but so far, nothing has worked. I thought SSH's Continuous Music module might help, but unfortunately AGS's new sound system has rendered the code in that module non-functional, and I lack the skills to update it. I've gone through the appropriate sections of the manual repeatedly, but I still don't quite have the hang of this new system.

As always, help is greatly appreciated.

Thank you!
Title: Re: Audio problems when changing rooms
Post by: Khris on Mon 05/03/2012 00:20:36
You need to store which track is playing.

AudioClip*room_music[10];
int current_track;

// in game_start
  room_music[0] = aRoomMusicPiano;
  ...

// in on_event
  // determine track number
  if (track_number != current_track) {
    Game.StopAudio();
    room_music[track_number].Play();
    current_track = track_number;
  }
Title: Re: Audio problems when changing rooms
Post by: Akril15 on Mon 05/03/2012 01:06:15
Thank you -- that code should be a lot simpler than what I currently have.


(And pardon me if I come across as dense, but how should I define "track_number"?)
Title: Re: Audio problems when changing rooms
Post by: Khris on Mon 05/03/2012 01:12:51
You said you had code that assigns music to the rooms.
Declare it as int and store the number instead of playing the track.

  int track_number;
  if (player.Room > 3 && player.Room < 7) track_number = 1;
  ...
Title: Re: Audio problems when changing rooms
Post by: Akril15 on Mon 05/03/2012 03:32:24
I'm afraid I'm still having trouble understanding what I need to do. The relevant portions of my script now look like this:


///start of global.asc
AudioClip *room_music[10];
int current_track;
int track_number;


game_start() {   

room_music[0] = aDay;

}


function on_event(EventType event, int data) {
 
  // determine track number
  if (track_number != current_track) {
    Game.StopAudio();
    room_music[track_number].Play();
    current_track = track_number;
  }

if (player.Room==8 || player.Room==14 || player.Room==16 ||player.Room==18 || player.Room==20) {
  current_track=0;
  }
}


The game loads without any problems, but no music plays in the rooms listed above.
Title: Re: Audio problems when changing rooms
Post by: Khris on Mon 05/03/2012 10:36:08
//start of global.asc
AudioClip *room_music[10];
int current_track = -1;              // set to -1 because 0 is already a valid track number
int track_number;

game_start() {   
  room_music[0] = aDay;
}


function on_event(EventType event, int data) {
 
  // set track_number, not current_track, and set it *before* the other code
  if (player.Room==8 || player.Room==14 || player.Room==16 ||player.Room==18 || player.Room==20) {
    track_number = 0;
  }

  if (track_number != current_track) {
    Game.StopAudio();
    room_music[track_number].Play();
    current_track = track_number;
  }
}


current_track is used to store the track that's currently playing. When a new room is entered, you're supposed to set track_number to the track that's supposed to be playing in the new room. In the next step, AGS compares the two to figure out whether it needs to stop the old and start the new audio file.
Since the first track has index 0, we also need to use -1 as initial value for current_track, otherwise AGS thinks that track 0 is already playing.
Title: Re: Audio problems when changing rooms
Post by: Akril15 on Mon 05/03/2012 22:02:41
The music works perfectly now. Thank you very much for being so patient with me.


(Nothing more embarrassing than breezing along through AGS and figuring out how to do so many things with great success, only to be stopped dead in one's tracks by something as basic as playing music.)