PDA

View Full Version : Right mouse Button + F = Tazer!


imagician
07/10/2011, 09:31 PM
Hi guys, I need your help.
How can I convert that ONLY if a player click the right mouse button + F, that he can /tazer the other players?
I already have:

if ((newkeys & 16) & & (oldkeys & 128) & & (oldkeys & 16))

This does not work, can help me one?
Sincerely, Imagician.

grand.Theft.Otto
07/10/2011, 09:44 PM
Follow the // commented lines:



// Defining KEY_AIM (right mouse button), as it is not defined in the SA-MP includes)

// place at the top of your script:

#define KEY_AIM (128)

// Under OnPlayerKeyStateChange

if(newkeys == KEY_AIM && newkeys == KEY_SECONDARY_ATTACK) // i think KEY_AIM is RMB and SECONDARY_ATTACK is the F key
{
// tazer code here
return 1;
}


Try it.

Gamer_Z
08/10/2011, 12:18 AM
Follow the // commented lines:



// Defining KEY_AIM (right mouse button), as it is not defined in the SA-MP includes)

// place at the top of your script:

#define KEY_AIM (128)

// Under OnPlayerKeyStateChange

if(newkeys == KEY_AIM && newkeys == KEY_SECONDARY_ATTACK) // i think KEY_AIM is RMB and SECONDARY_ATTACK is the F key
{
// tazer code here
return 1;
}


Try it.

http://wiki.sa-mp.com/wiki/OnPlayerKeyStateChange


How NOT to check for a key

Let's 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 its 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 held 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?

grand.Theft.Otto
08/10/2011, 12:42 AM
Hmm, thanks. He can learn from there.

I'm not too familiar scripting/using keys to select things in my server.