If I use "IsKeyPressed" in a piece of code, it will run that code over and over if the key is held down. What can I use that will only run the code once each time the key is pressed (ie. To do an action over and over, the key must must be pressed over and over)?
you could set a global int when the key is pressed and re-set it when the key is released.
ie
if (IsKeyPressed(*) == 1 && released == 1)
{
SetGlobalInt (released, 0)
stuff
}
else SetGlobalInt (released, 1)
}
should work...
Hmm, it probably should, but it doesn't. I can still hold the key down and get the action continuously.
ooops, try making that:
GetGlobalInt (released) == 1
Oh, I did that ^_^ I knew what you were saying, but the code just doesn't work. It's almost like it doesn't have time to work when the button is held down... I don't know. I don't really understand the inner workings of the engine.
hmmm, maybe try adding a wait??
I doubt it will work but its worth a try :/
Uh.... Couldn't you just put the script in on_key_press???
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 (...) {
//your script here
}
}
Uh... Yeah, I'm pretty sure that this only happens on a key press, hence the name on_key_press, and doesn't repeatedly execute the script if the key is held down. I'm pretty sure. You have some serious issues dude. ???
This is in on_key_press... dude.
Im out of ideas :/ I dont see why the global int idea doesnt work -_-;
Ill see if I can think of something,
and it might be an idea to come on IRC and pimp the thread ;D
I would, but it's 2am and I have uni tomorrow. I'll try to think about this some more but I don't know how successful I'll be.
Modifying Hotspots code a little:
(in rep_ex)
if ((IsKeyPressed(*) == 1) && (GetGlobalInt(1) == 0)) {
Ã, stuff
Ã, SetGlobalInt (1, 1);
}
else if (IsKeyPressed(*) == 0) {
Ã, SetGlobalInt (1, 0);
}
Works for me, but it might depend on what exactly you want to happen.
Perhaps on_key_press is called once a key is pressed and released, no matter how long it was held, and only then executes the code? That wouldn't cause it to repeat the action, though, so probably not.
I think Ashen's code would work.
edit: I was typing in my own edit of Hotspot's code, then realised it turned out exactly like ashen's only meant for the on_key_press function. heh
Don't forget to use a #define RELEASED [number of GI here], but you probably already did that.
I'm still trying to figure out why he would use IsKeyPressed IN on_key_press. It's one or the other. IsKeyPressed will work in repeatedly_execute, yeah. Maybe it might work in on_key_press, but there's really no point for it there is there? In on_key_press you can just say if (keycode==key) stuff and in repeatedly_execute you could do if (IsKeyPressed(key)==1) stuff else if (IsKeyPressed(key)==0) stuff. I don't see why you would try using IsKeyPressed in on_key_press. And what are you trying to do? You never actually specified WHAT you are trying to do. You are trying to figure out how to correctly use on_key_press, but other than that I have no clue.
Thanks a lot Ashen (and Ginny, seeing as you came up with the same basic code ^_^), that code works fine.
monkey: It's okay, take a breath. I didn't specify what I was trying to do because I didn't think it was necessary when I made this thread (and it wasn't). I thought there's be some quick answer (which there wasn't but it turned out fine). Sure, you can use keycode instead of IsKeyPressed, but I don't know of any difference it makes. If you can tell me, please do. I'll make any necessary changes.