17.03.2017, 20:03
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!
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.
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.
3. Testing the script
Would you like to test the benefits of the codes above? Try it by yourself!
Would like to have the full script? Copy the whole code!
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:
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!
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)))
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; }
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!