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


Messages In This Thread
Looping a sound til player exit level - by Hauken - 07-19-2013, 07:59 PM
RE: Looping a sound til player exit level - by TheGreatCthulhu - 07-19-2013, 09:06 PM



Users browsing this thread: 1 Guest(s)