SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 25/01/2018, 05:46 PM   #1
Manyula
Huge Clucker
 
Manyula's Avatar
 
Join Date: Jan 2012
Location: Germany
Posts: 284
Reputation: 47
Default Detect player throwing grenades

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.

Code:
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?
__________________
Manyula is offline   Reply With Quote
Old 25/01/2018, 05:52 PM   #2
pollo97
Big Clucker
 
pollo97's Avatar
 
Join Date: Sep 2011
Location: Italy
Posts: 83
Reputation: 4
Default Re: Detect player throwing grenades

Probably you should use OnPlayerWeaponShot, checking if weapon is grenade, but I have not test yet.
pollo97 is offline   Reply With Quote
Old 25/01/2018, 05:58 PM   #3
Manyula
Huge Clucker
 
Manyula's Avatar
 
Join Date: Jan 2012
Location: Germany
Posts: 284
Reputation: 47
Default Re: Detect player throwing grenades

OnPlayerWeaponShot get only called for weapons that send bullet data which is not the case for thrown weapons or RPGs.
__________________
Manyula is offline   Reply With Quote
Old 25/01/2018, 06:02 PM   #4
Hrb
Little Clucker
 
Hrb's Avatar
 
Join Date: Mar 2012
Posts: 26
Reputation: 5
Default Re: Detect player throwing grenades

You can try to check the weapon ID after the KEY_FIRE is released. (It should work.)

Something like this:

Code:
#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;
}
Hrb is offline   Reply With Quote
Old 25/01/2018, 06:08 PM   #5
Manyula
Huge Clucker
 
Manyula's Avatar
 
Join Date: Jan 2012
Location: Germany
Posts: 284
Reputation: 47
Default Re: Detect player throwing grenades

Quote:
Originally Posted by Hrb View Post
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.
__________________
Manyula is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect if player is facing towards another player? AndreiWow Scripting Help 1 07/02/2017 03:50 PM
Bug with throwing grenades? Midzi Bug Reports 1 21/07/2015 09:14 PM
how to check if player is throwing grenade PSYCHOBABYKILLA Scripting Help 7 24/04/2015 08:07 AM
How to detect if a player is in range of another player? rangerxxll Scripting Help 7 10/03/2014 11:48 PM
Detect if player faces a wall & player Biesmen Scripting Help 7 25/10/2011 12:55 PM


All times are GMT. The time now is 01:54 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.