[Tutorial] How to use OnPlayerKeyStateChange properly
#1

How to use OnPlayerKeyStateChange properly

hello again ! i'm making a new tuto on how to use OnPlayerKeyStateChange !

firstly :

why we use that call back anyways ? and why we use it ?

this callback is called when the player press some thing in his keyboard ! like the jumping key , or the sprinting key .

i will tell you how to use keys , instead of commands !!! yeah ! no need for commands any more ! press a button and every thing is done !

secondly :

open a script OR make a new one


starting with the simple ( useful for stunt servers ) :

press 2 to repair your car ! simple and very easy to make .

step 1 ) look for the callback ( use CTRL + F feature )

step 2 ) copy this and i will explain :

pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys == KEY_SUBMISSION)
    {
        if(IsPlayerInAnyVehicle(playerid))
        {
            RepairVehicle(GetPlayerVehicleID(playerid));
        }
    }
    return 1;
}
what is this ?

pawn Code:
if(newkeys == KEY_SUBMISSION)
newkeys = the keys that he allready pressed in that moment

KEY_SUBMISSION = if you didn't change gta sa controls it would be set as the numbre 2 ( in front of w - q - 1 - E - 3 )

pawn Code:
if(IsPlayerInAnyVehicle(playerid))
we know if the player is allready in a vehicle , if he is move to the next line

pawn Code:
RepairVehicle(GetPlayerVehicleID(playerid));
i can't explain the function to you ! that is why we move to GetPlayerVehicleID(playerid)

RepairVehicle needs a vehicle to repair , to know what vehicle the player is driving , we use that function

--------------------------------------------------------------
I hope i explained how this work ! see you later in another tuto

all the keys are :

KEY_ACTION
KEY_CROUCH
KEY_FIRE
KEY_SPRINT
KEY_SECONDARY_ATTACK
KEY_JUMP
KEY_LOOK_RIGHT
KEY_HANDBRAKE
KEY_LOOK_LEFT
KEY_SUBMISSION
KEY_LOOK_BEHIND
KEY_WALK
KEY_ANALOG_UP
KEY_ANALOG_DOWN
KEY_ANALOG_LEFT
KEY_ANALOG_RIGHT
KEY_YES
KEY_NO
KEY_CTRL_BACK
Reply
#2

Thanks, usefull
Reply
#3

Congrats! You have just shown a perfectly fine example on how NOT to check for a key. Try pressing KEY_SUBMISSION while driving. Oops! Your code just stopped working. The wiki explains this matter perfectly fine.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)