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
Looping a sound til player exit level
Hauken Offline
Member

Posts: 62
Threads: 19
Joined: Jul 2012
Reputation: 1
#1
Looping a sound til player exit level

Hi, im trying to create some athmosphere regarding sound, this is what i want:

The player walks into an area trigger and activates the sound, it plays at once, then 10 seconds after it plays again and again til the player leaves the level.

From my perspective:

Onstart calls a function that tells void func sound that we're gonna loop this but the function says - not until the player walks in my area but when he do i will call the next function which plays a sound, the onstart code says we're gonna play the sound after 10 seconds again.

---

As it is now the game doesnt crash but it doesnt work either, I've followed the "For" Loop tutorial in the wiki.

What's wrong here?

Also I want to mention this is the first time I'm using loop commands.

PHP Code: (Select All)
void OnStart()
{
    
//Looping athmosperhic sound
    
{      
        for(
int i 0<4i++)
        
AddTimer("S1" i10 i"func_sound");   
    }
}

void func_sound (string &in asParentstring &in asChildint alState)

{
AddEntityCollideCallback("Player""sound_scare""func_sound"true1); 
}

void func_sound(string &in asTimer)

{
string x asTimer;
    if (
== "S1")
    {
        
PlayGuiSound("door_level_cistern_open.snt"0.5);
    } 

(This post was last modified: 07-19-2013, 08:01 PM by Hauken.)
07-19-2013, 07:59 PM
Find
ExpectedIdentifier Offline
Member

Posts: 234
Threads: 10
Joined: Sep 2012
Reputation: 11
#2
RE: Looping a sound til player exit level

(07-19-2013, 07:59 PM)Hauken Wrote: Hi, im trying to create some athmosphere regarding sound, this is what i want:

The player walks into an area trigger and activates the sound, it plays at once, then 10 seconds after it plays again and again til the player leaves the level.

From my perspective:

Onstart calls a function that tells void func sound that we're gonna loop this but the function says - not until the player walks in my area but when he do i will call the next function which plays a sound, the onstart code says we're gonna play the sound after 10 seconds again.

---

As it is now the game doesnt crash but it doesnt work either, I've followed the "For" Loop tutorial in the wiki.

What's wrong here?

Also I want to mention this is the first time I'm using loop commands.

PHP Code: (Select All)
void OnStart()
{
    
//Looping athmosperhic sound
    
{      
        for(
int i 0<4i++)
        
AddTimer("S1" i10 i"func_sound");   
    }
}

void func_sound (string &in asParentstring &in asChildint alState)

{
AddEntityCollideCallback("Player""sound_scare""func_sound"true1); 
}

void func_sound(string &in asTimer)

{
string x asTimer;
    if (
== "S1")
    {
        
PlayGuiSound("door_level_cistern_open.snt"0.5);
    } 


Your code seems far too complex for what you're trying to achieve... wouldn't something like this do the trick?

PHP Code: (Select All)
void OnStart()
{
AddEntityCollideCallback("Player""AreaTrigger""PlaySoundLoop"true1);
}

void PlaySoundLoop(string &in asParentstring &in asChildint alState)
{
AddTimer("SoundLoopTimer",0,"SoundLoopTimer");


void SoundLoopTimer(string &in asTimer)
{
PlayGuiSound("door_level_cistern_open.snt"0.5);
AddTimer("SoundLoopTimer",10,"SoundLoopTimer");


Maybe not the best/shortest coding for the problem but it'll work.

Closure ModDB page:

[Image: 16LO8Sx]
07-19-2013, 08:41 PM
Find
The chaser Offline
Posting Freak

Posts: 2,486
Threads: 76
Joined: Jun 2012
Reputation: 113
#3
RE: Looping a sound til player exit level

(07-19-2013, 07:59 PM)Hauken Wrote: Hi, im trying to create some athmosphere regarding sound, this is what i want:

The player walks into an area trigger and activates the sound, it plays at once, then 10 seconds after it plays again and again til the player leaves the level.

From my perspective:

Onstart calls a function that tells void func sound that we're gonna loop this but the function says - not until the player walks in my area but when he do i will call the next function which plays a sound, the onstart code says we're gonna play the sound after 10 seconds again.

---

As it is now the game doesnt crash but it doesnt work either, I've followed the "For" Loop tutorial in the wiki.

What's wrong here?

Also I want to mention this is the first time I'm using loop commands.

PHP Code: (Select All)
void OnStart()
{
    
//Looping athmosperhic sound
    
{      
        for(
int i 0<4i++)
        
AddTimer("S1" i10 i"func_sound");   
    }
}

void func_sound (string &in asParentstring &in asChildint alState)

{
AddEntityCollideCallback("Player""sound_scare""func_sound"true1); 
}

void func_sound(string &in asTimer)

{
string x asTimer;
    if (
== "S1")
    {
        
PlayGuiSound("door_level_cistern_open.snt"0.5);
    } 


You have to voids with the same names. I think this should work:



PHP Code: (Select All)
void OnStart()
{
    
//Looping athmosperhic sound
    
{      
        for(
int i 0<4i++)
        
AddTimer("S1" i10 i"func_sound_x");   
    }
}

void func_sound (string &in asParentstring &in asChildint alState)

{
AddEntityCollideCallback("Player""sound_scare""func_sound"true1); 
}

void func_sound_x(string &in asTimer)

{
string x asTimer;
    if (
== "S1")
    {
        
PlayGuiSound("door_level_cistern_open.snt"0.5);
    } 


I can't really say it surely, as I'm in my vacations in a computer without amnesia.

THE OTHERWORLD (WIP)
[Image: k6vbdhu]

Aculy iz dolan.
07-19-2013, 08:45 PM
Find
Hauken Offline
Member

Posts: 62
Threads: 19
Joined: Jul 2012
Reputation: 1
#4
RE: Looping a sound til player exit level

@Sonata-
Yeah you nailed it, it works. Smile

As I said I never worked with loops before so I thought it had to be this complex, the looping tutorial at the wiki should be updated with this.

Thanks man.

@The Chaser
I'll try this code for fun as well and see if that works too.
(This post was last modified: 07-19-2013, 08:48 PM by Hauken.)
07-19-2013, 08:46 PM
Find
ExpectedIdentifier Offline
Member

Posts: 234
Threads: 10
Joined: Sep 2012
Reputation: 11
#5
RE: Looping a sound til player exit level

(07-19-2013, 08:45 PM)The chaser Wrote: I can't really say it surely, as I'm in my vacations in a computer without amnesia.

It wasn't to do with that, one of the functions was a timer and one was a collide callback, you can have different function types using the same name and it will be fine (although it's not a good idea because it could get confusing!) Smile

(07-19-2013, 08:46 PM)Hauken Wrote: @Sonata-
Yeah you nailed it, it works. Smile

As I said I never worked with loops before so I thought it had to be this complex, the looping tutorial at the wiki should be updated with this.

Thanks man.

@The Chaser
I'll try this code for fun as well and see if that works too.

You're welcome!

Closure ModDB page:

[Image: 16LO8Sx]
(This post was last modified: 07-19-2013, 08:50 PM by ExpectedIdentifier.)
07-19-2013, 08:49 PM
Find
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#6
RE: Looping a sound til player exit level

OK, I'm doing this of the top of my head, so I might miss something, but here's the deal.

First, let me tell you what your script actually does (and how it works):

PHP Code: (Select All)
void OnStart()
{
    
//Looping athmosperhic sound
    
{      
        for(
int i 0<4i++)
        
AddTimer("S1" i10 i"func_sound");   
    }

This part will be executed only once, right at the start of the level, the first time you enter it.
(So if the player can come back to this level from a different level, you might want to use OnEnter() instead, but that's not important now),

The for loop then (immediately) adds 4 timers, with the following names: "S10", "S11", "S12", and "S13".
The names are the result of "S1" + i, because i goes from 0 to 3 (because of the condition i<4).

BTW, by a lucky chance this code works as intended, but the loop should actually look like this (note where's the '{' symbol):

PHP Code: (Select All)
//Looping athmosperhic sound    
    
for(int i 0<4i++)
    {      
        
AddTimer("S1" i10 i"func_sound");   
    } 

So, what happens here is, as soon as the level starts, the func_sound timer function is called, and then ten seconds later it is called again, and then again 10s later, etc.
The "func_sound" timer callback function that get's called is this one:
PHP Code: (Select All)
void func_sound(string &in asTimer)

{
string x asTimer;
    if (
== "S1")
    {
        
PlayGuiSound("door_level_cistern_open.snt"0.5);
    } 


However, inside it, you check if the name of the timer is "S1", and if it is, you tell it to play the sound, but if it isn't, that whole part is skipped, and the function does nothing.
And in your case the timer name is never "S1" - because, remember, the timers are named "S10", "S11", "S12", and "S13".

The good thing is that you can scrap the if statement because in your case nothing else calls that particular function - it is always going to be one of those timers, so you can change it to this:
PHP Code: (Select All)
void func_sound(string &in asTimer)
{
    
PlayGuiSound("door_level_cistern_open.snt"0.5);


If you try and run the level now, you should hear the sound played 4 times, once every 10 seconds.
But, that's not what you want.

PHP Code: (Select All)
void func_sound (string &in asParentstring &in asChildint alState)
{
AddEntityCollideCallback("Player""sound_scare""func_sound"true1); 


As the things are arranged now, this entire function is never called. Never ever.
This is because the game is not aware of this function. To make it aware, you would use AddEntityCollideCallback, but not like this. AddEntityCollideCallback tells the game to call a specific function when two specific entities colide. But, since AddEntityCollideCallback is inside of this function, and the game doesn't know about this function and thus never calls it, AddEntityCollideCallback never executes.

But, the game will automatically call OnStart(), right? (Or OnEnter(), if you used that one.)
So, all you need to do is to move the call to AddEntityCollideCallback there. Your code will now look like this:
PHP Code: (Select All)
void OnStart()
{
    
AddEntityCollideCallback("Player""sound_scare""func_sound"true1);

    
//Looping athmosperhic sound
    
for(int i 0<4i++)
    {
        
AddTimer("S1" i10 i"func_sound");   
    }
}

void func_sound (string &in asParentstring &in asChildint alState)   // collide callback
{
    
}

void func_sound(string &in asTimer)      // timer callback
{
    
PlayGuiSound("door_level_cistern_open.snt"0.5);


OK. Now, the game knows what to do. When the Player colides with sound_scare, the func_sound collide callback will be called, but now it is empty, so it does nothing.
What you wanted to happen on collision? The sound to play? Well, just move the for loop that adds the timers there, like this:

PHP Code: (Select All)
void OnStart()
{
    
AddEntityCollideCallback("Player""sound_scare""func_sound"true1);
}

void func_sound (string &in asParentstring &in asChildint alState)   // collide callback
{
    
//Looping athmosperhic sound
    
for(int i 0<4i++)
    {
        
AddTimer("S1" i10 i"func_sound");   
    }
}

void func_sound(string &in asTimer)      // timer callback
{
    
PlayGuiSound("door_level_cistern_open.snt"0.5);


And there you go. It should work now (unless I overlooked some errors in your original code).

LOL, I was multi ninja'd during the time it took me to write all that, but read it anyway, it will be useful - it will help you understand how the code works together.

And yeah, you should use different names for the callback functions, just to make the code more readable.
(This post was last modified: 07-19-2013, 09:12 PM by TheGreatCthulhu.)
07-19-2013, 09:06 PM
Find




Users browsing this thread: 3 Guest(s)