Detect player throwing grenades
#1

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

Probably you should use OnPlayerWeaponShot, checking if weapon is grenade, but I have not test yet.
Reply
#3

OnPlayerWeaponShot get only called for weapons that send bullet data which is not the case for thrown weapons or RPGs.
Reply
#4

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

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)