Problem with multiple key holds
#1

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?
Reply
#2

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;
}
Reply
#3

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
Reply
#4

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
Reply
#5

I hold my hands up to the above! My mistake same layout as above, with use of the above information
Reply
#6

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");
    }
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)