Detecting melee - 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: Detecting melee (
/showthread.php?tid=591392)
Detecting melee -
zaibaslr2 - 11.10.2015
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?
Re: Detecting melee -
Spmn - 11.10.2015
GetPlayerAnimationIndex() should do exactly what you want.
Re: Detecting melee -
[XST]O_x - 11.10.2015
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.
Re: Detecting melee -
zaibaslr2 - 12.10.2015
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
Re: Detecting melee -
[XST]O_x - 12.10.2015
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) {
//...
Re: Detecting melee -
zaibaslr2 - 12.10.2015
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
Re: Detecting melee -
zaibaslr2 - 13.10.2015
Bump
Re: Detecting melee -
bgedition - 13.10.2015
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
Re: Detecting melee -
zaibaslr2 - 13.10.2015
Quote:
Originally Posted by bgedition
|
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
Re: Detecting melee -
[XST]O_x - 13.10.2015
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);
}