Frictional Games Forum (read-only)

Full Version: sound playing in area
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
I don't get it. What you're meaning.

EDIT:
At the part where the Player loops.
(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
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);
}
}
it crashed at
Code:
else if
{
(06-16-2013, 11:06 AM)39Gamer Wrote: [ -> ]it crashed at
Code:
else if
{

Just use else, not else if.
(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);
}
}
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