Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sound playing in area
39Games Offline
Junior Member

Posts: 48
Threads: 14
Joined: Jan 2013
Reputation: 0
#1
sound playing in area

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

(This post was last modified: 06-16-2013, 10:52 AM by 39Games.)
06-16-2013, 10:30 AM
Find
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#2
RE: sound playing in area

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

EDIT:
At the part where the Player loops.

"Veni, vidi, vici."
"I came, I saw, I conquered."
(This post was last modified: 06-16-2013, 10:45 AM by PutraenusAlivius.)
06-16-2013, 10:45 AM
Find
39Games Offline
Junior Member

Posts: 48
Threads: 14
Joined: Jan 2013
Reputation: 0
#3
RE: sound playing in area

(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

06-16-2013, 10:52 AM
Find
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#4
RE: sound playing in area

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);
}
}

"Veni, vidi, vici."
"I came, I saw, I conquered."
06-16-2013, 10:59 AM
Find
39Games Offline
Junior Member

Posts: 48
Threads: 14
Joined: Jan 2013
Reputation: 0
#5
RE: sound playing in area

it crashed at
else if
{

06-16-2013, 11:06 AM
Find
ExpectedIdentifier Offline
Member

Posts: 234
Threads: 10
Joined: Sep 2012
Reputation: 11
#6
RE: sound playing in area

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

Just use else, not else if.
06-16-2013, 11:11 AM
Find
39Games Offline
Junior Member

Posts: 48
Threads: 14
Joined: Jan 2013
Reputation: 0
#7
RE: sound playing in area

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

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

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);
}
}

06-16-2013, 11:26 AM
Find
Adrianis Offline
Senior Member

Posts: 620
Threads: 6
Joined: Feb 2012
Reputation: 27
#8
RE: sound playing in area

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
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

(This post was last modified: 06-18-2013, 06:47 PM by Adrianis.)
06-18-2013, 06:44 PM
Find




Users browsing this thread: 1 Guest(s)