How do I wait for a sound effect to end before running any other script lines?

Started by mishels, Sat 16/09/2006 20:15:01

Previous topic - Next topic

mishels

I noticed that when I use PlaySound it runs the sound effect and continues doing other stuff on the list of thigs to do. I want the game to wait for the sound to end and then continue.

I have tried this code:

PlaySound(1);
while(IsSoundPlaying())
{
  // Do nothing
}
DisplayMessage(6);
cEgo.ChangeRoom(3);


but the game encounters an internal error with this description:
Error: run_text_script1: error -6 running function 'hotspot1_a':
Error: Script appears to be hung (15001 while loop iterations without an update in Room 2 script (line 11)


While writing this message I realized that this code can not wait for the sound to end since this code is run in the big game loop so the solution probably lies in an event that singnals the end of the sound effect.

But that is just a guess.
I also saw the wait functions and though that as I know the time length of the sound clip I can wait as many loops as neccessary to end the sound.
But I am looking for the cleaner, more generic solution.
I remember reading about some function that runs each loop, I could in theory wait for the sound to finish on that loop and call a function to continue the execution of my script, I am sure that will work, but still I keep thinking there is a simpler solution which must be in this wonderful tool.

So, how do I wait for the end of a sound effect and do some stuff after it?

Thanks,
Mishel.

Barbarian

Couldn't a simple "Wait" command work in this case?

So, for example, have the code look like:
Code: ags
 PlaySound(1); Wait(200); DisplayMessage(6); cEgo.ChangeRoom(3);


Of course, you'll need to play around with the number in the Wait command according to how long the sound lasts.
Conan: "To crush your enemies, see them driven before you, and to hear the lamentation of the women!"
Mongol General: "That is good."

Blade of Rage: www.BladeOfRage.com

Ashen

Actually, I think you where nearly there:

Code: ags

PlaySound(1);
while(IsSoundPlaying())
{
  Wait(1);// Do nothing
}
DisplayMessage(6);
cEgo.ChangeRoom(3);


This is basically Barbarian's suggestion, but you don't need to figure out how long the sound lasts.

EDIT: Which, re-reading your question, is kind of what you wanted. Excellent.
Regarding the Error message, what do you mean by "this code is run in the big game loop"? Hopefully, having the Wait command in there will clear this up, anyway.
I know what you're thinking ... Don't think that.

mishels

Thanks Ashen!
It worked like a charm!

I now understand what the Wait function really does... it is so cool!

When I talked about the big loop I reffered to the loop that runs all the frames of the game. Each game has a main loop that recieves input and outputs whatever is needed.
We do not actually see the loop or can control it since it is already handled by the engine but we get to run our scripts at certain points within the loop.
Each pass of the loop is a game cycle.

Now I see that calling the Wait command holds the rest of the script to be executed on the next cycle, or after 200 cycles, whatever we put on the Wait function parameters. This is really a cool feature, it saves us a really big and cluttered repeatedly_execute funtion.
By the way, that function is called strait from the big loop every pass of the loop and that is why the code there will be executed every cycle and not just when some event happens.

Anyway, Thanks again, it is a joy to see this work so beutifully with so little effort programatically.

Mishel.

SMF spam blocked by CleanTalk