[Tutorial] Fixing Sniper Scope's attachment issue
#1

Alright so, too many members encounter the issue of head attachments causing disturbance while aiming with a sniper rifle, so we will understand now how to completely solve this and play in peace without his issue anymore!


1. Definitions & Declarations
Okay, so we will be defining PRESSING and RELEASED, to check if the player is pressing the aiming key (KEY_HANDBRAKE) so we can remove the attachment and re-add it once the aiming key (KEY_HANDBRAKE) is released (is no longer being pressed). Easy!

Code:
#define PRESSING(%0,%1) (%0 & (%1))
#define RELEASED(%0) (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
Now, we will declare a variable to store that the player is having the helmet enabled so we can determine whether the player had the helmet attachment or not so after releasing the aiming key (KEY_HANDBRAKE) with the sniper rifle, we will re-add the helmet again.

Code:
new
		Helmet_Attached_Object[MAX_PLAYERS]
;


2. Scripting
Now we will head off to OnPlayerKeyStateChange so we can create the fix. Read the comments within the code blocks to understand the meaning of the lines in this part and their use.
Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    /*
	    Now if the player has the helmet enabled, and is holding a sniper rifle the attachment,
	    will be hidden (removed) temporarily until the player releases the key.
    */
	if(PRESSING(newkeys, KEY_HANDBRAKE))
	{
	    if(Helmet_Attached_Object[playerid] == 1 && GetPlayerWeapon(playerid) == 34)
	    {
        	if(IsPlayerAttachedObjectSlotUsed(playerid, 1))
        	{
        	    RemovePlayerAttachedObject(playerid, 1);
        	}
		}
	}
    /*
	    Now when the player releases (stops pressing) the aiming key, the attachment
	    will be re-added to his head so it doesn't disturb him any longer!
    */
	else if(RELEASED(KEY_HANDBRAKE))
	{
	    if(Helmet_Attached_Object[playerid] == 1 && GetPlayerWeapon(playerid) == 34)
	    {
       	    SetPlayerAttachedObject(playerid, 1, 19103, 2, 0.18, 0.00, 0.01, -5.30, 175.80, -158.00, 1.00, 1.00, 1.00);
		}
	}
	return 1;
}

3. Testing the script
Would you like to test the benefits of the codes above? Try it by yourself!
Code:
public OnPlayerSpawn(playerid)
{
    Helmet_Attached_Object[playerid] = 1; // Setting the variable to "1" so that the player has the helmet.
    SetPlayerAttachedObject(playerid, 1, 19103, 2, 0.18, 0.00, 0.01, -5.30, 175.80, -158.00, 1.00, 1.00, 1.00); // Attaching helmet on slot 1.
    GivePlayerWeapon(playerid, 34, 20); // Giving the player a sniper rifle with 20 rounds of ammo to test the system.
    return 1;
}


Would like to have the full script? Copy the whole code!
Code:
#include <a_samp>

/*

   Okay, so we will be defining PRESSING and RELEASED, to check if the player is pressing
   the aiming key (KEY_HANDBRAKE) so we can remove the attachment and re-add it once
   the aiming key (KEY_HANDBRAKE) is released (no longer being pressed). Easy!

*/

#define PRESSING(%0,%1) (%0 & (%1))
#define RELEASED(%0) (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))

/*
   We will declare the variable "Helmet_Attached_Object", to store if the player
   has the attachment to do the removing and re-adding process properly.
*/

new
		Helmet_Attached_Object[MAX_PLAYERS]
;


public OnPlayerSpawn(playerid)
{
    Helmet_Attached_Object[playerid] = 1; // Setting the variable to "1" so that the player has the helmet.
    SetPlayerAttachedObject(playerid, 1, 19103, 2, 0.18, 0.00, 0.01, -5.30, 175.80, -158.00, 1.00, 1.00, 1.00); // Attaching helmet on slot 1.
    GivePlayerWeapon(playerid, 34, 20); // Giving the player a sniper rifle with 20 rounds of ammo to test the system.
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    /*
	    Now if the player has the helmet enabled, and is holding a sniper rifle the attachment,
	    will be hidden (removed) temporarily until the player releases the key.
    */
	if(PRESSING(newkeys, KEY_HANDBRAKE))
	{
	    if(Helmet_Attached_Object[playerid] == 1 && GetPlayerWeapon(playerid) == 34)
	    {
        	if(IsPlayerAttachedObjectSlotUsed(playerid, 1))
        	{
        	    RemovePlayerAttachedObject(playerid, 1);
        	}
		}
	}
    /*
	    Now when the player releases (stops pressing) the aiming key, the attachment
	    will be re-added to his head so it doesn't disturb him any longer!
    */
	else if(RELEASED(KEY_HANDBRAKE))
	{
	    if(Helmet_Attached_Object[playerid] == 1 && GetPlayerWeapon(playerid) == 34)
	    {
       	    SetPlayerAttachedObject(playerid, 1, 19103, 2, 0.18, 0.00, 0.01, -5.30, 175.80, -158.00, 1.00, 1.00, 1.00);
		}
	}
	return 1;
}
Okay so, now once the player aims with the sniper scope, the helmet attachment variable is set to 1 (Means it is activated) the script will automatically fix the issue. Once the player aims with sniper rifle while having the helmet on his head, it'll disappear, then when he releases it (Stops pressing the aiming key) it will automatically re-add the helmet.

NOTICE: Make sure to set the variable Helmet_Attached_Object to 0 once you want the player to drop the helmet so it doesn't get bugged. E.g. at OnPlayerSpawn add:
Code:
Helmet_Attached_Object[playerid] = 0;


Like in the example above, you can make that with different head attachments using a specific variable and a unique slot ID of the attachment to make this works properly.

Have fun!
Reply
#2

what if I had 10 objects attached to my head ?
Reply
#3

What if the object is not a helmet?
Is the object offset accurate for every skin?
Reply
#4

what if my helmet index is 2?
Reply
#5

Quote:
Originally Posted by Sreyas
View Post
what if I had 10 objects attached to my head ?
Each attachment has a separate slot ID, so it's easy rather than use slot one you can use two, three, four and so on. And rather than create a special variable for each attachment you can use something like:
Code:
new Attachments[3];
So you can use 3 attachments like that .. (0, 1 and 2). While each attachment has a unique slot ID.

Quote:
Originally Posted by Troydere
View Post
What if the object is not a helmet?
Is the object offset accurate for every skin?
If the attachment is not helmet you can change the offsets and the object ID and it will remain accurate as well as you attach anything to your head.

Quote:
Originally Posted by ISmokezU
View Post
what if my helmet index is 2?
We won't use slot one but slot two, we just need the slot ID to check if the helmet is already attached to head so we can remove it. We can also alternatively relay on the variable.

Tutorial will be updated shortly.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)