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.

Code Snippets / [DBC/DBP] Toggle keys

Author
Message
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 27th Nov 2006 04:01 Edited at: 27th Nov 2006 04:01
A very simple thing illustrating how to make a toggle switch. Until recently i was using a much longer code with nested ifs to do the same thing, so this is for anyone else who was equally stupid.



You'll be able to click on this someday.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 27th Nov 2006 10:07
It was just until very recently till I discovered this method myself

sadsack
20
Years of Service
User Offline
Joined: 27th Nov 2003
Location: here
Posted: 30th Nov 2006 21:47
I like it I sure I can find a use for that nice code.
Ian T
22
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 12th Feb 2007 00:36
Nice method!
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 22nd Feb 2007 23:05
Thats neat, the only problem is you cant trigger events depending on the key's old status, but I guess that'd defeat the purpose of having the concise code anyways.

Chris Franklin_
17
Years of Service
User Offline
Joined: 21st Dec 2006
Location: Home
Posted: 21st Mar 2007 01:03
neat thanks

Johnathon
17
Years of Service
User Offline
Joined: 20th Feb 2007
Location: South Park
Posted: 1st Apr 2007 14:35
Can someone please explain that method step by step. It works but i don't understand it
JHA
20
Years of Service
User Offline
Joined: 30th Dec 2003
Location: Massachusetts, USA
Posted: 22nd Jun 2007 17:00 Edited at: 22nd Jun 2007 17:05
I realize that this is an old post, but I am having trouble implementing the above code. By itself, it seems to work, but I cannot get it to work in one of my routines.

Here is the code:


Now this was working fine until I upgraded to DBPro 6.6. Apparently, my variable is not keeping the previous value of the Keystate any more. I've moved the 'last_D = KEYSTATE(32)' line around to different places to no avail.

btw - KEYSTATE(32) is checking for the letter 'D'.

Can anyone suggest another way to get this working?

Thank you
Joe
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 23rd Jun 2007 12:57
I don't know if that's the problem, but you could store "keystate(32)" in a variable, because maybe DBP (or windows, whatever) resets the keystate-value when it's called or something.. maybe this could solve the problem, but I'm not sure.

Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1
JHA
20
Years of Service
User Offline
Joined: 30th Dec 2003
Location: Massachusetts, USA
Posted: 25th Jun 2007 15:00
Well, I am saving it to a variable with the line last_D = Keystate(32). I've tried moving that line right after the IF statement, but it doesn't seem to hold it either. Funny thing is, I can get similar code to work outside of the main program.

I no longer think it is DBPro that is the problem, but I am still stuck.

Thanks for the response though.

Joe
vibe runner
18
Years of Service
User Offline
Joined: 7th Aug 2006
Location: The Future
Posted: 25th Jun 2007 22:45
Look at this, perhaps. Has the advantage of being key-wide friendly. Might be of interest.



Aaaaaaaaaaand it might not.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 22nd Jul 2007 20:27 Edited at: 22nd Jul 2007 20:32
@ Johnathon
I will attempt to explain it step by step.

Quote: "last_spacekey=spacekey()"

Mr. Tank put this at the end but it can be at the front.
This stores the "old" spacekey state for comparison later.

Quote: "if spacekey() > last_spacekey"

This statement is very clever as it is basically the same as
Quote: "If Spacekey() = 1"

except if SPACE already being pressed [Spacekey()=1] it will return a 0 [if 1 > 1 = false]. In other words this statement will only be true if space has been released beforehand, this means there will be no repetition.

Quote: "spacekeytoggle=1-spacekeytoggle"

This is a simple toggle, if the variable is 0 it will become 1, if it is 1 it will become 0.

I've edited and commented the code to make it easier to follow.


[EDIT]
oops, I forgot to reposition the SYNC command. Here is the same code with the SYNC repositioned. Can you figure out why this version works and the other doesn't?



Great code Mr. Tank

I am king of the noobs!
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 24th Jul 2007 02:09
I'm glad people found this interesting/useful. I think it's all been well explained, so that saves me a job.

I have realised a slight flaw in this. If the key is depressed after the command
last_spacekey=spacekey()
and before the
if spacekey()>last_spacekey:....:endif
,then it works.

However, if it happens after the
if spacekey()>last_spacekey:....:endif
and before
last_spacekey=spacekey()
,then it fails to catch the spacekey event. The reason that it tends to work 99.9900% of the time is that the interval between syncs is much longer than the time it takes to execute the code, meaning that the two spacekey() commands are almost certainly the same.
To make it infallible though, do something like this (note that the spacekey() command is only called once per loop now):




SUPER BADASS SPACESHIP X: WEBSITE
FORUM TOPIC
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Jul 2007 03:51 Edited at: 25th Jul 2007 03:56
I just put the SYNC in between the two spacekey() calls so it would register a change . I was calling these "trip switches", and using a lot more code. I prefer your way and the word "toggle".



This can also be an ON_RELEASE toggle switch. That means you can have two different events for press and release! cool



I am king of the noobs!
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 26th Jul 2007 00:46
Nice. The "negative edge" switch is a cool idea.
I maintain that it's best not to call the keystate() or spacekey() twice per loop. It would become problematic if you put other stuff before the "loop" command in your code. Also calling it once and storing the value is quicker than calling the function twice, IIRC.

SUPER BADASS SPACESHIP X: WEBSITE
FORUM TOPIC
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 5th Aug 2007 02:09
Oh right is see what you are saying now, only change the old_state when the switch is toggled, makes sense.

Your signature has been erased by a mod because it was rubbish.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 20th Aug 2007 23:38
A similar version of this for mouse clicks


This allows you to check for mouse-down/mouse-up events easily:
When button 1 is pressed down, Msd = 1
When button 1 is released, Msd = -1
This works exactly the same for all mouse buttons

Login to post a reply

Server time is: 2024-11-22 19:11:32
Your offset time is: 2024-11-22 19:11:32