Problem with multiple key holds - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Problem with multiple key holds (
/showthread.php?tid=454763)
Problem with multiple key holds -
CaveDweller - 30.07.2013
Hey. I'm trying to detect whenever a player is holding the fire key (for an IsPlayerShooting function) but at the moment I'm struggling.
pawn Код:
if(HOLDING(KEY_FIRE))
{
holdingFire[playerid] = 1;
}
else
{
holdingFire[playerid] = 0;
}
The above code works fine provided that the person stands still and shoots, but as soon as they are walking and shooting it just sets holdingFire to 0. What do you recommend I do to fix this?
Re: Problem with multiple key holds -
JimmyCh - 30.07.2013
I'm not sure if you can make use of this, but you can also check IsPlayerMoving, using this stock:
pawn Код:
stock bool:IsPlayerMoving(playerid)
{
new Float:Velocity[3];
GetPlayerVelocity(playerid, Velocity[0], Velocity[1], Velocity[2]);
if(Velocity[0] == 0 && Velocity[1] == 0 && Velocity[2] == 0) return false;
return true;
}
Re: Problem with multiple key holds -
FUDDS - 30.07.2013
All your original code does is consider a single key at any time. Checking if they are pressing that key is correct, but you need to check that the released key is also the fire key. Otherwise, if a player is holding the fire key but then presses forwards too, it is not the fire key so it disables the function. Under OnPlayerKeyStateChange, do:
pawn Код:
if ( ( oldkeys - newkeys ) == KEY_FIRE )
{
// stopped firing
}
else
if ( ( newkeys - oldkeys ) == KEY_FIRE )
{
// firing
}
This checks that the key released is the fire key
Re: Problem with multiple key holds -
Konstantinos - 30.07.2013
Quote:
Originally Posted by FUDDS
All your original code does is consider a single key at any time. Checking if they are pressing that key is correct, but you need to check that the released key is also the fire key. Otherwise, if a player is holding the fire key but then presses forwards too, it is not the fire key so it disables the function. Under OnPlayerKeyStateChange, do:
pawn Код:
if ( ( oldkeys - newkeys ) == KEY_FIRE ) { // stopped firing } else { if ( ( newkeys - oldkeys ) == KEY_FIRE ) // firing }
This checks that the key released is the fire key
|
Read this please.
How NOT to check for a key
and this too:
How to check for a key
Re: Problem with multiple key holds -
FUDDS - 30.07.2013
I hold my hands up to the above! My mistake

same layout as above, with use of the above information
Re: Problem with multiple key holds -
CaveDweller - 30.07.2013
I'm still struggling with this. I've got the code below, but whenever I fire I don't get the message.
pawn Код:
if ( ( oldkeys - newkeys ) & KEY_FIRE )
{
// stopped firing
}
else
if ( ( newkeys - oldkeys ) & KEY_FIRE )
{
//firing
SendClientMessage(playerid, -1, "firing");
}