Script Question: Code for any keyboard button to play random sound.

Started by sexygenocide, Fri 17/04/2009 22:39:47

Previous topic - Next topic

sexygenocide

Hello there, first post so be nice. I've been using AGS on and off since it was DOS but never with anything too scripty.

Anyway, basically I need a script that will play a random sound say (SOUND1 to SOUND10) when any key is hit. Preferably without the use of any character scripting as there wont be a mouse to use. I'll give you a bit of background:

Basically me and my friend have a tradition of building awesome bin's for our houseparties for birthdays etc. It started off pretty basic, but in an effort to continually top our previous bins we've decided for this one we want a bin that thanks you (i.e. plays random sound) every time you deposit something into it. The keyboard from an old computer will placed strategically at the bottom of the bin so it will be impacted every time a bottle or a can goes into it. Since we can't be entirely sure which key will be struck the script needs to apply to all of the keys.

Any help will be much appreciated as you'll be coding the new generation of Binotrons.

densming

There's a function in the GlobalScript.asc global script file called on_key_press.  That's what you'll need to use.

Code: ags

function on_key_press(int keycode) {
  int i;
  i = Random(9) + 1;
  PlaySound(i);
}


You'll have to test it out though.  This probably isn't the best way to accomplish what you're trying to do, since the bottle that hits the keyboard will likely keep the key pressed down, and once you get more than a few objects in the bin the weight of the objects will likely press all the keys at once.  You'll have to be very "strategic" in the placement of the keyboard...

sexygenocide

Ah thank you densming that code worked perfectly. As for the keyboard problem you mentioned we've overcame that by using a thin sheet of metal that indents on impact, hitting the keyboard,  but then returns to it's normal shape. Imagine the thin lid to a large metal tub of chocolates, seems to work well so far. Try it on a bin at home!

SMF spam blocked by CleanTalk