OnPlayerKeyStateChange
#1

a question ...
What is the difference between:
Код:
# define PRESSED (%0) (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
//where %0 is KEY_FIRE.
and:

Код:
if ((newkeys & KEY_FIRE) && !(oldkeys & KEY_FIRE))
Sorry but I find the difference, I want to learn it =).
Sorry for my bad English.
Edit...
Reply
#2

I'm not sure. I know the second version will make it so that if you press the key, it will still register it even if you're holding other keys. I'm not sure about the first version. I'm sure it does something similar. It looks like they both use bitwise functions.
Reply
#3

Since this has the same subject (almost), I'll just ask here.. :P

Whats the difference between "==" and "&" ? :P
Everyone uses "&" for keystates, so I've been wondering a long time :P
Reply
#4

Quote:
How NOT to check for a key

Lets presume that you want to detect when a player presses their FIRE button, the obvious code would be:

if (newkeys == KEY_FIRE)

This code may even work in your testing, but it is wrong and your testing is insufficient. Try crouching and pressing fire - your code will instantly stop working. Why? Because "newkeys" is no longer the same as "KEY_FIRE", it is the same as "KEY_FIRE" COMBINED WITH "KEY_CROUCH".
How to check for a key

So, if the variable can contain multiple keys at once, how do you check for just a single one? The answer is bit masking. Each key has it's own bit in the variable (some keys have the same bit, but they are onfoot/incar keys, so can never be pressed at the same time anyway) and you need to check for just that single bit:

if (newkeys & KEY_FIRE)

Note that the single "&" is correct - this is a bitwise AND, not a logical AND, which is what the two ampersands are called.

Now if you test this code it will work whether you are crouching or standing when you press the fire key. However there is still one slight problem - it will fire as long as you are holding the key. OnPlayerKeyStateChange is called every time a key changes and that code is true whenever the the fire key is help down. If you press fire the code will fire, if that key is held and you press crouch - that code will fire again because a key (crouch) has changed and fire is still held down How do you detect when a key is first pressed, but not trigger again when it's still held and another key changes?
Reply
#5

The first one is a key being pressed (Not held). The second one is a key being released.

Larzi:

"==" is equal to obviously. "&" is binary AND. I also used to have a problem understanding why "&" is used, but then I had to learn subnetting. Look in Wikipedia for binary and, they have a good explanation. The goal is to be able to detect which combination of keys is being pressed, and not just one key.

No Keys:
00000000

Let's say Fire Key == 8
00010000

And Let's say Aim Key == 128
00010001

Get the point? This way we can detect a combination of keys instead of just assigning any numbers in sequence (i.e. 1,2,3,4).

Now we can detect just ONE of those keys, or all of them.
Is player holding Fire Key along with other keys we don't care about?

Current Keys:
00010001

We do an AND operation:
And operation means true and true, just like in English.

Current: 00010001
Fire key:00010000
Result is:00010000

The result is fire key. The player is holding fire key along with other keys. By the way, I wrote these binary values reversed, no idea why.
Reply
#6

my question was not "Whats the difference between "==" and "&" ?"
Reply
#7

Quote:
Originally Posted by Malice
The first one is a key being pressed (Not held). The second one is a key being released.

Larzi:
Don answered you, I answered you. The rest was to Larzi as stated.

Either way, it's actually very related.
Reply
#8

thanks malice, but I was wrong .

Код:
What is the difference between:

# define PRESSED (%0) (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
//where %0 is KEY_FIRE.

and:

if ((newkeys & KEY_FIRE) && !(oldkeys & KEY_FIRE))//
Sorry.
Reply
#9

Ty for explaining for me, lol :P
Biggest thanks to Don Correli who "explained" best :>
Reply
#10

You wanted to know what "&" does and that's what it does.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)