This is just a very small C-Script contribution. I needed a code that could query if a key was pressed ONCE (So holding the key won't repeat the respective action).
For instance, I needed the player to press SPACEBAR once for every Jump of the character, because Holding the SPACEBAR should be used to Grab over ledges after jumping. With the normal key_pressed(), the jump would be repeated all the time while SPACE is held.
Also, commands like "NEXT_WEAPON", "PREVIOUS WEAPON", and certain attacks with the mouse button should not be quickly repeated: they should work only once for each time the key is pressed.
Code:
define KEY_NORMAL,0;
define KEY_ONCE,1;
var is_key_hold[300];
function keypressed (keycode, keymode)
{
if (key_pressed(keycode))
{
if (is_key_hold[keycode])
{
if (keymode == KEY_ONCE)
{
return (off); //press once mode
}
return (on); //normal mode
}
else
{
is_key_hold[keycode] = on;
return (on); //key ON in both normal and press_once modes
}
}
else
{
is_key_hold[keycode]=off;
return (off);
}
}
To use it instead of key_pressed, use:
if (keypressed(scancode,KEY_NORMAL) ) {...}
To query if a key has been pressed one time (not being held), use:
if (keypressed(scancode,KEY_ONCE) ) {...}
It's important to completely replace key_pressed by this function, at least for the needed non-repeating keys, because the keypressed() function automatically sets the key HOLD flags on and off in an array.
Thanks for your comments!