Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 984 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
LucasJoshua, Baklazhan, Hanky27, firatv, wandaluciaia
19053 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Non-repeating keyboard input #87774
08/28/06 00:57
08/28/06 00:57
Joined: Jul 2005
Posts: 366
eleroux Offline OP
Senior Member
eleroux  Offline OP
Senior Member

Joined: Jul 2005
Posts: 366
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!

Re: Non-repeating keyboard input [Re: eleroux] #87775
08/28/06 02:09
08/28/06 02:09
Joined: Aug 2003
Posts: 7,439
Red Dwarf
Michael_Schwarz Offline
Senior Expert
Michael_Schwarz  Offline
Senior Expert

Joined: Aug 2003
Posts: 7,439
Red Dwarf
you could just have used

on_space = grab_function;




"Sometimes JCL reminds me of Notch, but more competent" ~ Kiyaku
Re: Non-repeating keyboard input [Re: Michael_Schwarz] #87776
08/28/06 03:19
08/28/06 03:19
Joined: Jul 2005
Posts: 366
eleroux Offline OP
Senior Member
eleroux  Offline OP
Senior Member

Joined: Jul 2005
Posts: 366
thanks for the tip

My project is a bit complex, although. It reads and saves key labels and definitions from files in portuguese, english or spanish, and uses complex key commands. Some weapons are auto-repeat, others not, others depend on holding a key like a bow and arrow... So I prefer to have things under control this way.

Of course it's up to every user. If you like this quick snippet, you can use at will.


Moderated by  adoado, checkbutton, mk_1, Perro 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1