Detect player throwing grenades - 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: Detect player throwing grenades (
/showthread.php?tid=648615)
Detect player throwing grenades -
Manyula - 25.01.2018
Hi,
the title says it all: How do I reliably check for when a player throws a grenade? I've browsed the forums already and found this function here.
Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
new weaponid = GetPlayerWeapon(playerid);
if(newkeys & KEY_FIRE && weaponid == 16)//16 for grenade
{
// your codes
}
return 1;
}
This is not 100% reliable though. Grenades are thrown with a small delay after pressing the fire key which allows players to switch weapons mid-animation and have the animation play out without actually throwing a grenade. A possible solution would be to run a short but frequently repeating timer for the duration of the animation and check if the currently armed weapon is a grenade. Thoughts?
Re: Detect player throwing grenades -
pollo97 - 25.01.2018
Probably you should use OnPlayerWeaponShot, checking if weapon is grenade, but I have not test yet.
Re: Detect player throwing grenades -
Manyula - 25.01.2018
OnPlayerWeaponShot get only called for weapons that send bullet data which is not the case for thrown weapons or RPGs.
Re: Detect player throwing grenades -
Hrb - 25.01.2018
You can try to check the weapon ID after the KEY_FIRE is released. (It should work.)
Something like this:
Код:
#define RELEASED(%0) \
(((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
new weaponid = GetPlayerWeapon(playerid);
if(RELEASED(KEY_FIRE) && weaponid == 16)
{
SendClientMessage(playerid, -1, "granat on the way!");
return 1;
}
Re: Detect player throwing grenades -
Manyula - 25.01.2018
Quote:
Originally Posted by Hrb
You can try to check the weapon ID after the KEY_FIRE is released. (It should work.)
|
This is still no different to the example I provided in the OP. You can still switch weapons before the grenade is actually thrown. In this case, it doesn't matter if the key is pressed or released.