Detecting melee
#1

hey,
Is it possible to somehow detect when player swings a melee weapon like baseball bat or katana? If so, what would be the best way to do so?
Reply
#2

GetPlayerAnimationIndex() should do exactly what you want.
Reply
#3

Not really sure. You can check OnPlayerKeyStateChange, but you need to make sure player isn't falling/in car/in water, and is equipped with a melee.
But I think a better way is using GetPlayerAnimationIndex and GetAnimationName.
Here is a list of animations:
https://sampwiki.blast.hk/wiki/Animations
You have animations for Knife and Baseball(baseball bat), pretty sure there is other melees as well.
Reply
#4

But I need to make like a callback that will be called every time player swings a melee weapon. If I used it with let's say OnPlayerUpdate and check player animation, it would spam the callback as long as the animation is running
Reply
#5

Quote:
Originally Posted by zaibaslr2
Посмотреть сообщение
But I need to make like a callback that will be called every time player swings a melee weapon. If I used it with let's say OnPlayerUpdate and check player animation, it would spam the callback as long as the animation is running
You're right. You can combine OnPlayerKeyStateChange with GetPlayerAnimationIndex.
I mean, a player can't swing a bat without hitting KEY_FIRE, he has to. So, you can do something like:
pawn Код:
OnPlayerKeyStateChange(playerid, oldkeys, newkeys)
{
    if(PRESSED(KEY_FIRE)) {
        if(GetPlayerAnimationIndex(playerid) > 0) {
        //...
Reply
#6

Quote:
Originally Posted by [XST]O_x
Посмотреть сообщение
You're right. You can combine OnPlayerKeyStateChange with GetPlayerAnimationIndex.
I mean, a player can't swing a bat without hitting KEY_FIRE, he has to. So, you can do something like:
pawn Код:
OnPlayerKeyStateChange(playerid, oldkeys, newkeys)
{
    if(PRESSED(KEY_FIRE)) {
        if(GetPlayerAnimationIndex(playerid) > 0) {
        //...
You could spam the KEY_FIRE and it will still work. I need it to be called only once per hit
Reply
#7

Bump
Reply
#8

Put this where you want
Код:
if(GetPlayerWeapon(playerid) == WEAPON_BAT) {
               //do something
}
Source:
Weapon IDs: https://sampwiki.blast.hk/wiki/Weapons
GetPlayerWeapon: https://sampwiki.blast.hk/wiki/GetPlayerWeapon
Reply
#9

Quote:
Originally Posted by bgedition
Посмотреть сообщение
Put this where you want
Код:
if(GetPlayerWeapon(playerid) == WEAPON_BAT) {
               //do something
}
Source:
Weapon IDs: https://sampwiki.blast.hk/wiki/Weapons
GetPlayerWeapon: https://sampwiki.blast.hk/wiki/GetPlayerWeapon
No dude it just detects if player has a melee weapon.
I need to make a callback that gets called every time player swings/hits a melee weapon, just like OnPlayerWeaponShot
Reply
#10

I thought of something like this, totally untested:
pawn Код:
forward OnPlayerSwingBat(playerid);

new bool: pBatting[ MAX_PLAYERS ] = {false, ...};

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(KEY_FIRE)) {
        if(!GetPlayerAnimationIndex(playerid)) //Player is not performing any animation
            pBatting[playerid] = false; //Reset batting flag

        if(GetPlayerAnimationIndex(playerid)) { //Player is performing an animation
            new animlib[32];
                new animname[32];
                GetAnimationName(GetPlayerAnimationIndex(playerid),animlib,32,animname,32);

                if(!strcmp(animname, "Bat_1") || !strcmp(animname, "Bat_2") || !strcmp(animname, "Bat_3") || !strcmp(animname, "Bat_4")) { //Player does bat animation
                    if(!pBatting[ playerid ]) //Make sure batting flag isn't raised to prevent spamming the callback
                        OnPlayerSwingBat(playerid); //Call the callback

                    pBatting[playerid] = true; //Raise the flag
                 }

            }
    }
}

public OnPlayerSwingBat(playerid)
{
    printf("Player %d swung his bat.", playerid);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)