Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / key delay help

Author
Message
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 25th Jan 2008 01:49
I'm making a potion button in my game but the problem is when they press a button to use a potion it uses more than 1 at once is there a way delay the key from being pressed until the user releases the button or at least delay it for a time?. Oh yes also I'm using keystate for that button.

Windows Is better than everything
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Jan 2008 02:07 Edited at: 25th Jan 2008 02:16
Here's a pretty simple method that uses a flag to detect the release of a key (the p key in this example)



Enjoy your day.
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 25th Jan 2008 02:18
thanks for the help but would you mind explaning it i though keystate was used for scan codes?

Windows Is better than everything
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Jan 2008 02:19 Edited at: 25th Jan 2008 02:20
Latch's example will halt the program, if you want the program to continue you'll need a variable to stop the code being executed more than once. This is what I do

This will only remove a potion if rightkey is pressed and wasn't pressed on the last check.

[edit]
Ok Latch's is better now but I still prefer my way than using a flag.

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Jan 2008 02:26 Edited at: 25th Jan 2008 02:41
@Ed222
I'm not sure which version you saw because I was editing the original when you posted. In the edited version, the scancode for p=25 - I looked that up ahead of time.
iterations is just a count for the main do loop so that I can prove the program isn't being held up waiting for the p key to be released.

The flag to check for whether or not the p key has been pressed and released is called release.

if keystate(p)=1 and release=0
dec potions
release=1
endif

This block checks the keystate of the p key (scan code 25), if it is pressed, decreases the poition count and sets the release flag to 1. The next time through the loop if the release flag is not = 0 then it won't decrease the potion count.

if keystate(p)=0 then release=0

This checks if the p key has been released. If it has, then it sets release=0 so that the next time through the loop if p is pressed and release = 0 then another potion will be deducted. So all in all, if the p key is never released, the variable release will never get set back to 0 so another potion can't be used until the key is released.



{EDIT}
Obese's way is better

Enjoy your day.
Bluestar4
19
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 25th Jan 2008 10:06
another option would be to check for the playing of a speciffic sound associated with the drinking of the potion. sample code below ( you can change the inkey to keystate if you need to )




make sure the sound you load is long enough to provide a decent amount of time for the drinking animation(s)
As long as the drinking sound is playing the drink button will not execute , hence you will only need to hit it once.

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 25th Jan 2008 19:20 Edited at: 25th Jan 2008 19:26
Yet another very basic way (that I tend to use) is to have:

Repeat
Until Scancode()=0


...at the end of the procedure which is called when the key is pressed. (You can use the same method with the mouse and buttons using MouseClick()=0 too).

If it's used a lot, you can put it in a procedure or function:



[Edit] This method halts the program also, but my guess is that it's probably being used in a place where it doesn't matter anyway - like an inventory screen.

In any case, no-one is going to hold the key down - it's just that on faster machines, even a quick keytap can cause a procedure to be executed a number of times.

All of our examples prevent that, so take your pick!

TDK_Man

Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 25th Jan 2008 21:20 Edited at: 25th Jan 2008 21:31
@latch i think i had your old one
@Tdk nope no item screen and yes mostly they won't hold the key but what if they do? they could this glitch
@ latch and OBese87
both of you are saying each others codes are better this can get really confusing.


[edit]
@latch why didn't i think of that

Windows Is better than everything
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Jan 2008 21:39 Edited at: 25th Jan 2008 21:45
Quote: "both of you are saying each others codes are better this can get really confusing."


I think OBese meant the edited version I posted was better than the first version I posted. The first version was a simple soultion if it didn't matter the your whole program would wait until you released the button like in an application as opposed to a game.

I think OBese's version is a little better because he only uses one IF THEN as opposed to my 2; but then again he's using 2 variables and a boolean switch and I'm only using one variable and one boolean switch - I don't know... it's a toss up!

Enjoy your day.
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 25th Jan 2008 21:46 Edited at: 25th Jan 2008 23:53
@latch
oh....anyways i just finished with the potion heres what i did with you code


@everyone

thanks to everyone for helping

Windows Is better than everything
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 26th Jan 2008 02:59
i use a timer based variable myself. this allows you to limit how soon after using the key/potion/whatever you can use it again.

perfect example is say your character has a pistol which can fire shots one after another you'd want a quick recharge time say 200ms but if you then switch to a pump action shotgun it takes longer to ready the weapon between shots and so you need a longer time to recharge say 750ms. or more for your potion: say its a really powerful heal that needs to be limited to usable once every 5 minutes, you'd set the time check to 60000*5 (whatever that number calcs out to)



Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Jan 2008 07:56
@Ortu
I remember you posting that last time this was brought up, it's a cool idea .

Bluestar4
19
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 26th Jan 2008 10:01
either way you do it , you will have to use another variable to check with besides the one your using - the first one will test to see if the key is hit , and the second one will test one of these conditions mentioned above i.e,
if inkey$()="d" and condition_2_met=1 ... endif
if keystate(53)= and sound playing(?) ...endif
if scancode... and timervar>? ... endif

my favorite though when playing these types of games is to hit the drink health potion icon with the mouse (dungeon siege,diablo 2)
all characters in the game on ds take a drink of the health potion if their health is low.

Login to post a reply

Server time is: 2025-06-04 02:11:53
Your offset time is: 2025-06-04 02:11:53