Detecting updown/leftright keys
#1

I know OnPlayerKeyStateChange are not detecting these keys, therefore I'm coming here to ask for help, I'm trying to detect somehow when either of arrow keys are pressed, however OnPlayerUpdate it just checks it too fast so it like detects as if its pressed like maybe 10 times or such, is there another callback or something that I could use for this?
Reply
#2

Quote:
Originally Posted by ******
View Post
You still need to use OnPlayerUpdate, but only use the data when the key state changes. It will be called multiple times as long as the key is held down, which you don't want.
What about setting a variable for a player, and then the key-check will only be called if the variable has the correct value.
Reply
#3

Quote:
Originally Posted by ******
View Post
You still need to use OnPlayerUpdate, but only use the data when the key state changes. It will be called multiple times as long as the key is held down, which you don't want.
How am I supposed to detect if it changes player key state?
Reply
#4

https://sampwiki.blast.hk/wiki/GetPlayerKeys
Reply
#5

Quote:
Originally Posted by SmiT
View Post
Care to elaborate? I don't see how that'll help me detect if the keystate are changed though.
Reply
#6

pawn Code:
new pKey[MAX_PLAYERS][4]; // 0=up 1=down 2=left 3=right

public OnPlayerUpdate(playerid)
{
    new keys, updown, leftright;
    GetPlayerKeys(playerid, keys, updown, leftright);

    if(updown == KEY_UP && !pKey[playerid][0])
    {
        pKey[playerid][0] = 1; // Remember that they are pressing up
        OnPlayerUDLR(playerid, 0); // Call the function
    }
    if(updown != KEY_UP && pKey[playerid][0]) pKey[playerid][0] = 0; // Up no longer pressed

    if(updown == KEY_DOWN && !pKey[playerid][1])
    {
        pKey[playerid][1] = 1; // Remember that they are pressing down
        OnPlayerUDLR(playerid, 1); // Call the function
    }
    if(updown!= KEY_DOWN && pKey[playerid][1]) pKey[playerid][1] = 0; // Down no longer pressed

    if(leftright == KEY_LEFT && !pKey[playerid][2])
    {
        pKey[playerid][2] = 1; // Remember that they are pressing left
        OnPlayerUDLR(playerid, 2); // Call the function
    }
    if(leftright != KEY_LEFT && pKey[playerid][2]) pKey[playerid][2] = 0; // Left no longer pressed

    if(leftright == KEY_RIGHT && !pKey[playerid][3])
    {
        pKey[playerid][3] = 1; // Remember that they are pressing right
        OnPlayerUDLR(playerid, 3); // Call the function
    }
    if(leftright != KEY_RIGHT && pKey[playerid][3]) pKey[playerid][3] = 0; // Right no longer pressed
    return 1;
}
pawn Code:
OnPlayerUDLR(playerid, udlr)
{
    switch(udlr)
    {
        case 0:
        {
            // pressed up
        }
        case 1:
        {
            // pressed down
        }
        case 2:
        {
            // pressed left
        }
        case 3:
        {
            // pressed right
        }
    }
    return 1;
}
Un-tested and rushed.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)