Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Candle on Fri 23/04/2004 01:38:17

Title: if (IsKeyPressed(368)=1 StopMusic ();
Post by: Candle on Fri 23/04/2004 01:38:17
I want to have F10 key when pressed all music will stop . I put in this .
Quote// script for room: Player enters screen (after fadein)
if (IsKeyPressed(368)=1 StopMusic ();
 
}
But got this and not sure what it means ?
QuoteCompile Error
---------------------------
There was an error compiling your script. The problem was:

In: 'Main script'

Error (line 10): end of input reached in middle of expression

What do I have wrong ?
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: strazer on Fri 23/04/2004 02:49:53
if (IsKeyPressed(368)==1) StopMusic ();

The conditional must always be enclosed by brackets.
And you have to use two == when doing a comparison.
You use one = to assign a value.
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: Candle on Fri 23/04/2004 03:14:34
Thank you , man there is so much to learn  and i can't retain most of it .

EDIT
well no errors but it didn't stop the music ?
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: strazer on Fri 23/04/2004 03:20:01
In which function did you put it?
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: Gilbert on Fri 23/04/2004 03:26:56
I see that you put it in the player enters screen after fade-in, that way it will ONLY work if the key is kept pressed during the room change. If you want it to effect whenever the player pressed the key, put it into the repeatedly execute function.

Alternatively (not many people know this), you can actually create a function in the room:

function on_key_press(int keycode) {
 if (keycode==368) StopMusic();
}

Right, it's the same function name as the one in the global script, in that way, if a key is pressed, the engine will parse the room function first, followed by the global one.
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: Candle on Fri 23/04/2004 03:29:26
Yes i put it // script for room: Player enters screen (after fadein) so all change it .. lol
Thanks guys .
I know  a lot of people like to be able to turn the music off so thats why I want to do this .
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: Candle on Fri 23/04/2004 03:34:32
Can this be done in the main global script file so they just have to do it  one time and no music ?
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: strazer on Fri 23/04/2004 03:35:37
Yes, just put this in your global on_key_press function:

if (keycode == 368) StopMusic();
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: Candle on Fri 23/04/2004 03:42:32
Where would I add it in all that code . I put it in the end and had a } at he end of it but it didn't like that . lol
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: strazer on Fri 23/04/2004 04:03:15
Assuming you started off the default game, your on_key_press function should look something like

function on_key_press(int keycode) {
 // called when a key is pressed. keycode holds the key's ASCII code
 if (IsGamePaused() == 1) keycode=0;  // game paused, so don't react to keypresses
 if (keycode==17)  QuitGame(1);   // Ctrl-Q
 if (keycode==363) SaveGameDialog();   // F5
 if (keycode==365) RestoreGameDialog();  // F7
 if (keycode==367) RestartGame();  // F9
 if (keycode == 368) StopMusic();  // F10
 if (keycode==434) SaveScreenShot("scrnshot.bmp");  // F12
 if (keycode==9)   show_inventory_window();  // Tab, show inventory

 if (keycode==19)  Debug(0,0);  // Ctrl-S, give all inventory
 if (keycode==22)  Debug(1,0);  // Ctrl-V, version
 if (keycode==1)   Debug(2,0);  // Ctrl-A, show walkable areas
 if (keycode==24)  Debug(3,0);  // Ctrl-X, teleport to room
}

Put the line where I marked it red.
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: Candle on Fri 23/04/2004 04:07:22
Thank you strazer , thats what i needed to know . it works .
Thanks again . now I have to write it down or all forget it . lol
Title: Re:if (IsKeyPressed(368)=1 StopMusic ();
Post by: on Thu 06/05/2004 21:31:59
Hi I see the above works very well. Can I impose on yhis topic and ask how one would resume the music.

I can only assume that I may well get a RTFM (Read the Friggin Manual) response and in which case I will I promise  ;D

Johnnie
Title: Re: if (IsKeyPressed(368)=1 StopMusic ();
Post by: strazer on Thu 06/05/2004 21:50:54
Hm, I would probably do it this way (for MP3/OGG music files):
Global script

int oldmusictrack=1; // stores number of current music track
int oldmusicpos=0; // stores position in current music track

Global on_key_press (see above)

  if (keycode == 368) { // F10
   if (IsMusicPlaying()) { // music is on
      oldmusictrack = GetCurrentMusic(); // save number of current music track
      oldmusicpos = GetMP3PosMillis(); // save current position in music track
      StopMusic(); // stop current music track
   else { // music is off
      PlayMusic(oldmusictrack); // re-play previous music track
      SeekMP3PosMillis(oldmusicpos); // jump to position where it left off
   }
  }

EDIT: Changed int oldmusictrack=0 to int oldmusictrack=1 so that when the game starts without music playing and the user presses F10, the first music track starts playing.

P.S.: Glad I could help. :)
Title: Re: if (IsKeyPressed(368)=1 StopMusic ();
Post by: on Thu 06/05/2004 21:54:47
:o - Now that's a quick response.

Much appreciation strazer. I will sure give it a go l8r on.

Wow this board is a welcome site!!!!!   ;D