Frictional Games Forum (read-only)
sound playing in area - 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: sound playing in area (/thread-21846.html)



sound playing in area - 39Games - 06-16-2013

I have a little water setup and when a player is inside an area at the bottom i want the game to play a sound that loops, and when the player leaves, the sound stops.
Whats the code for that?
Cheers


RE: sound playing in area - PutraenusAlivius - 06-16-2013

I don't get it. What you're meaning.

EDIT:
At the part where the Player loops.


RE: sound playing in area - 39Games - 06-16-2013

(06-16-2013, 10:45 AM)JustAnotherPlayer Wrote: I don't get it. What you're meaning.

EDIT:
At the part where the Player loops.
Oops, fixed


RE: sound playing in area - PutraenusAlivius - 06-16-2013

Code:
void OnStart()
{
AddEntityCollideCallback("Player", "ScriptAreaName", "PlayerCollideSound", false, 0);
}

void PlayerCollideSound(string &in asParent, string &in asChild, int alState)
{
if(alState == 1)
{
PlaySoundAtEntity("PlaySound", "SoundFile.snt", "Player", 0.1f, false); //Make sure the extension is .snt at the second argument and you should use PlayMusic if it's a music.
}

else if
{
StopSound("PlaySound", 0.1f);
}
}



RE: sound playing in area - 39Games - 06-16-2013

it crashed at
Code:
else if
{



RE: sound playing in area - ExpectedIdentifier - 06-16-2013

(06-16-2013, 11:06 AM)39Gamer Wrote: it crashed at
Code:
else if
{

Just use else, not else if.


RE: sound playing in area - 39Games - 06-16-2013

(06-16-2013, 11:11 AM)sonataarctica Wrote:
(06-16-2013, 11:06 AM)39Gamer Wrote: it crashed at
Code:
else if
{

Just use else, not else if.
i experimented and this code appears to work, thanks for the rest of the code Smile

Code:
void OnStart()
{     
    AddEntityCollideCallback("Player", "scriptareaname", "PlayerCollideSound", false, 0);    
}

void PlayerCollideSound(string &in asParent, string &in asChild, int alState)
{
if(alState == 1)
{
PlaySoundAtEntity("PlaySound", "Sound.snt", "Player", 0.0f, true);
}

if(alState == -1)
{
StopSound("PlaySound", 0.0f);
}
}



RE: sound playing in area - Adrianis - 06-18-2013

Just wanted to offer a quick explanation - when you use if...else statements, it ensures that only one of the pieces of code will run (i.e. the code in the 'if' or the code in the 'else'). That applies to if... else if statements as well (the 'else if' is for checking another condition)

However, when you use 'if' and then another 'if' instead of an 'else', it is possible for the code for both if's to run. This can create some nasty bugs if both of your statements turn out to be true (impossible in this example, but very possible elsewhere)

It is therefore much more secure to use this method in the future
Code:
void PlayerCollideSound(string &in asParent, string &in asChild, int alState)
{
   if(alState == 1)
   {  
       PlaySoundAtEntity("PlaySound", "Sound.snt", "Player", 0.0f, true);
   }

   else if(alState == -1)
   {  
       StopSound("PlaySound", 0.0f);
   }
}

This way, you can always be absolutely certain that ONLY PlaySoundAtEntity OR StopSound will be executed, never both.

Again, not so important right now but I thought it might help you out in the future Tongue