Attract-Mode Support Forum

Attract-Mode Support => Scripting => Topic started by: wrybread on November 16, 2017, 05:29:31 PM

Title: Play sound when starting game?
Post by: wrybread on November 16, 2017, 05:29:31 PM
I'm trying to play a different sound when I switch selected ROM, switch display and start a ROM. All works well with the below code except starting a ROM. Is the transition not "ToGame"?

Code: [Select]
function fade_transitions( ttype, var, ttime ) {


switch ( ttype ) {
  case Transition.ToNewList:
local Wheelclick = fe.add_sound("sounds/teleport.wav")
Wheelclick.playing=true
return false;
break;
  }

switch ( ttype ) {
  case Transition.ToGame:
  local Wheelclick = fe.add_sound("sounds/boom.wav")
Wheelclick.playing=true
break;
  }

switch ( ttype ) {
  case Transition.ToNewSelection:
  ;case Transition.ToNewList:
local Wheelclick = fe.add_sound("sounds/clicked.wav")
Wheelclick.playing=true
break;
  }


return false;
}

fe.add_transition_callback( "fade_transitions" );



Title: Re: Play sound when starting game?
Post by: jedione on November 16, 2017, 06:49:30 PM
it;s because entering a rom is not a wheelclick...

this should work for you. just change the sound file name, but i think you know that.

Code: [Select]
function togame( ttype, var, ttime ) {
 switch ( ttype ) {
   case Transition.ToGame:
     local sound = fe.add_sound("intro.mp3");
sound.playing=true;
  break;
  }
 return false;
}
fe.add_transition_callback( "togame" );
Title: Re: Play sound when starting game?
Post by: keilmillerjr on November 16, 2017, 09:20:56 PM
You can use one switch with multiple case statements as well, to clean up your code and DRY (don't repeat yourself).
Title: Re: Play sound when starting game?
Post by: wrybread on November 16, 2017, 11:26:31 PM
Thanks for the tip on cleaning up the switch statement. I think this is a better way to write it, but I'm still not sure I understand the "break" statements, and whether I need one in each case:

Code: [Select]
function fade_transitions( ttype, var, ttime ) {

switch ( ttype ) {

  case Transition.ToGame:
  local sound = fe.add_sound("sounds/Burgertime_Dies.wav");
sound.playing=true;
break;

  case Transition.ToNewList:
local Wheelclick = fe.add_sound("sounds/Asteroids_Tone02.wav");
Wheelclick.playing=true;
break;

  case Transition.ToNewSelection:
local Wheelclick = fe.add_sound("sounds/clicked.wav");
Wheelclick.playing=true;
break;
  }


return false;
}


fe.add_transition_callback( "fade_transitions" );

But still no joy.

And substituting the whole block for the code that @jedione posted also doesn't work for me.

Any idea why?
Title: Re: Play sound when starting game?
Post by: wrybread on November 17, 2017, 12:52:13 AM
It works if I change the transition to FromGame (it then plays the sound when I return from the game), so I'm wondering if it's just not giving the sound enough time to play?

Can someone confirm that playing sounds with ToGame works?

Title: Re: Play sound when starting game?
Post by: keilmillerjr on November 17, 2017, 07:30:11 AM
It works if I change the transition to FromGame (it then plays the sound when I return from the game), so I'm wondering if it's just not giving the sound enough time to play?

Can someone confirm that playing sounds with ToGame works?

You need to wait until your sound is finished playing before returning the transition function. Look at my FadeToGame plugin. Maybe it help.
Title: Re: Play sound when starting game?
Post by: jedione on November 17, 2017, 07:24:09 PM
not shure why it dont work for you,  this is what i get with the
same code that i posted....

preview...https://youtu.be/gJMP5VHeAM8 (https://youtu.be/gJMP5VHeAM8)

so i would say ToGame is working.
Title: Re: Play sound when starting game?
Post by: 8bitsdeep on November 19, 2017, 06:04:28 AM
Thanks for the tip on cleaning up the switch statement. I think this is a better way to write it, but I'm still not sure I understand the "break" statements, and whether I need one in each case

"break" marks the end of each case. Without it, the code would continue on and execute the next case even if it wasn't a match.  Similar to the closing } on an if statement.