PDA

View Full Version : Detecting updown/leftright keys


oliverrud
16/11/2011, 04:29 PM
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?

Biesmen
16/11/2011, 05:23 PM
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.

oliverrud
16/11/2011, 05:36 PM
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?

SmiT
16/11/2011, 05:45 PM
http://wiki.sa-mp.com/wiki/GetPlayerKeys

oliverrud
16/11/2011, 05:46 PM
http://wiki.sa-mp.com/wiki/GetPlayerKeys

Care to elaborate? I don't see how that'll help me detect if the keystate are changed though.

MP2
16/11/2011, 06:04 PM
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;
}



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.