Frictional Games Forum (read-only)
Ambient Music/Sound Fix - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: Ambient Music/Sound Fix (/thread-24552.html)



Ambient Music/Sound Fix - RaideX - 02-05-2014

So, why hello there dear Amnesia Community. Up ahead will probably be a somewhat long post for a probably not so huge problem. Though i want to be sure let you guys know what i'm doing and how i'm doing it.

So basically i'm using an ambient music technique where i have different script areas that play different types of music depending on the surrounding architecture. The code looks, in short, something likes this:

PHP Code:
//AMBIENCE COLLIDE CALLBACKS
AddEntityCollideCallback("Player""AmbientScriptArea""AmbienceHandler"false0)

//HANDLER
void AmbienceHandler (string &in asParentstring &in asChildint alState) {
   if (
asChild == "AmbientScriptArea") {
      if (
alState == 1) {
         
PlaySoundAtEntity("Ambience01""amb_xx_xx.snt""Player"1.0ftrue) }
      if (
alState == 1) {
         
StopSound("Ambience01"1.0f) }
   }


With this method the ambient sound can easily be changed with different script areas. Though i ran into problems when it comes to shaped rooms that are not quadrangular. The way I've done it was that i just set one script area up so that it covers the blank areas (because the music would stop otherwise) and let it play the same sound/music again. I've put together a little picture to demonstrate it a little better.

The point marked with an X is the problem zone. If the player switches or passes this point the ambient music changes and starts over from the start again. It's either a whole other setup of code or i'm just being too fussy about this because it is easily overheard. Though i'd like to discuss it here and see if someone is willing to help me out here Smile. Thanks in advance.


RE: Ambient Music/Sound Fix - DreamScripters - 02-06-2014

Ok, so what I got from your post (correct me if i'm wrong) is that there are two script areas that will play the same ambience but when you enter said one of them while the same music is playing it will start over? If this is the case all I beleive you would have to do is set up a conditional statement that will check if the same ambeince is already playing. Sinse there is no way to be able to get the music playing you will have to get creative and use intigers.

So, how we could accomplish this is that we would take all of the script areas that have your problem and make it so it adds 1 to a intiger (considering it isn't already 1) that you would have already defined. And then when walking into an area that plays the same ambeince that is already playing you could make it so that if your intiger is equal to one it won't start the ambeince. And then just make it so when you go into any other script area it will set it back to 0.

Sorry if my reply looks like gibberish to you. Tongue I am very tired. But if this doesn't make sense I can explain it more with the actual script as a reference. Good luck!


RE: Ambient Music/Sound Fix - RaideX - 02-06-2014

(02-06-2014, 05:27 AM)DreamScripters Wrote: Ok, so what I got from your post (correct me if i'm wrong) is that there are two script areas that will play the same ambience but when you enter said one of them while the same music is playing it will start over? If this is the case all I beleive you would have to do is set up a conditional statement that will check if the same ambeince is already playing. Sinse there is no way to be able to get the music playing you will have to get creative and use intigers.

So, how we could accomplish this is that we would take all of the script areas that have your problem and make it so it adds 1 to a intiger (considering it isn't already 1) that you would have already defined. And then when walking into an area that plays the same ambeince that is already playing you could make it so that if your intiger is equal to one it won't start the ambeince. And then just make it so when you go into any other script area it will set it back to 0.

Sorry if my reply looks like gibberish to you. Tongue I am very tired. But if this doesn't make sense I can explain it more with the actual script as a reference. Good luck!

alright, you got my point.

I'll try to work on it when i have some time and get back to you when i run into any problems or when i finish it. Thanks for the answer Smile


RE: Ambient Music/Sound Fix - Daemian - 02-06-2014

If you call a song that it's currently playing, it ignores the call and keeps playing the song.
So in your case of two areas that the player may go from one to the other, the song wouldn't stop cause he entered one of the areas, it finds it's the same and keeps playing.

This only applies to music, so you should use PlayMusic function instead of PlaySound:
PlayMusic( "my_theme_001.ogg", false, 0.1f, 1.0f, 0, true );
Spoiler below!
PlayMusic( file, loop?, volume, fade, priority, resume? );

If a given set of areas play a different theme each, when the player steps into one, it's gonna stop the current song and start another.

In your function AmbienceHandler you can have this:
Spoiler below!

PHP Code:
void AmbienceHandler string &in pstring &in asAreaint s )
{
 
string myTheme "";

     if ( 
asArea == "AmbientScriptKitchen" ) { myTheme == "theme_kitchen.ogg"; }
     if ( 
asArea == "AmbientScriptOutside" ) { myTheme == "theme_outside.ogg"; }
     if ( 
asArea == "AmbientScriptGallery" ) { myTheme == "theme_gallery.ogg"; }

PlayMusicmyThemefalse0.1f1.0f0true ); 



This way you can point all your ambient callbacks to the same function AmbienceHandler and just add a condition to change the name of the file.


RE: Ambient Music/Sound Fix - RaideX - 02-06-2014

(02-06-2014, 08:16 PM)Amn Wrote: If you call a song that it's currently playing, it ignores the call and keeps playing the song.
So in your case of two areas that the player may go from one to the other, the song wouldn't stop cause he entered one of the areas, it finds it's the same and keeps playing.

This only applies to music, so you should use PlayMusic function instead of PlaySound:
PlayMusic( "my_theme_001.ogg", false, 0.1f, 1.0f, 0, true );
Spoiler below!
PlayMusic( file, loop?, volume, fade, priority, resume? );

If a given set of areas play a different theme each, when the player steps into one, it's gonna stop the current song and start another.

In your function AmbienceHandler you can have this:
Spoiler below!

PHP Code:
void AmbienceHandler string &in pstring &in asAreaint s )
{
 
string myTheme "";

     if ( 
asArea == "AmbientScriptKitchen" ) { myTheme == "theme_kitchen.ogg"; }
     if ( 
asArea == "AmbientScriptOutside" ) { myTheme == "theme_outside.ogg"; }
     if ( 
asArea == "AmbientScriptGallery" ) { myTheme == "theme_gallery.ogg"; }

PlayMusicmyThemefalse0.1f1.0f0true ); 



This way you can point all your ambient callbacks to the same function AmbienceHandler and just add a condition to change the name of the file.

Very nice explanation. Though this solution implies that i don't use anything else Music wise. I'll see what i'm gonna use in the final script file. Thanks anyway Smile Appreciate it.


RE: Ambient Music/Sound Fix - MyRedNeptune - 02-06-2014

(02-05-2014, 06:08 PM)RaideX Wrote:
PHP Code:
if (alState == 1) {
         
StopSound("Ambience01"1.0f) }
   } 

I'm sure this is supposed to be:

PHP Code:
if (alState == -1) {
         
StopSound("Ambience01"1.0f) }
   } 

?


Anyway, if I understood correctly every script area calls the same AmbienceHandler function?

If yes, you can try giving a number value to each area depending on what music it needs to trigger. It'll be assigned to a variable when an area is entered. The sound functions will not be called if the value of the int matches the value assigned to the area. The two areas that play the same ambience will have the same value.

PHP Code:
int ambienceType 0;

AddEntityCollideCallback("Player""AmbientScriptAreaBlue1""AmbienceHandler"false0);

void AmbienceHandler (string &in asParentstring &in asChildint alState) {
   if (
asChild == "AmbientScriptAreaBlue1") {
      if (
ambienceType == 1) {
         return;
      }
      else {
         if (
alState == 1) {
            
ambienceType 1;
            
PlaySoundAtEntity("Ambience01""amb_xx_xx.snt""Player"1.0ftrue
         }
         if (
alState == -1) {
            
StopSound("Ambience01"1.0f
         }
      }
   }




RE: Ambient Music/Sound Fix - RaideX - 02-07-2014

(02-06-2014, 09:59 PM)MyRedNeptune Wrote:
(02-05-2014, 06:08 PM)RaideX Wrote:
PHP Code:
if (alState == 1) {
         
StopSound("Ambience01"1.0f) }
   } 

I'm sure this is supposed to be:

PHP Code:
if (alState == -1) {
         
StopSound("Ambience01"1.0f) }
   } 

?


Anyway, if I understood correctly every script area calls the same AmbienceHandler function?

If yes, you can try giving a number value to each area depending on what music it needs to trigger. It'll be assigned to a variable when an area is entered. The sound functions will not be called if the value of the int matches the value assigned to the area. The two areas that play the same ambience will have the same value.

PHP Code:
int ambienceType 0;

AddEntityCollideCallback("Player""AmbientScriptAreaBlue1""AmbienceHandler"false0);

void AmbienceHandler (string &in asParentstring &in asChildint alState) {
   if (
asChild == "AmbientScriptAreaBlue1") {
      if (
ambienceType == 1) {
         return;
      }
      else {
         if (
alState == 1) {
            
ambienceType 1;
            
PlaySoundAtEntity("Ambience01""amb_xx_xx.snt""Player"1.0ftrue
         }
         if (
alState == -1) {
            
StopSound("Ambience01"1.0f
         }
      }
   }


Yes it's suppost to be -1, my bad. I've implemented the version you described, works pretty good. I still need to test it out a bit, will get back to it once i'm concerned that it doesnt work anymore Tongue Thanks.