Jump to content
Robustcolor

Help with a simple weapon restriction EH

Recommended Posts

Hi, i'm trying to restrict a specific weapon to an active amount of 1, since i'm using an Arsenal my guess was doing this from initPlayerLocal.sqf.

[missionNamespace, "arsenalClosed", {

if (allPlayers findif {primaryWeapon _x isKindOf "BAF_L85A2_RIS_SUSAT"}  !=-1) then  {
player removeWeapon "BAF_L85A2_RIS_SUSAT";
};

}] call BIS_fnc_addScriptedEventHandler;

Is there a simplier way of checking if only the weapon exist, even if its dropped on the ground or in someones primary slot?

Share this post


Link to post
Share on other sites

I am not sure about it but you may be assigned the weapon as your primary weapon before exiting the arsenal. This means that the check inside the if statement will return true even if you are the only one with the weapon in the game, thus removing the weapon from your inventory. Haven't really tested it but I am just throwing an idea here.

 

Additionally, I am not sure you have to add the event handler when each client joins the game, because the event handler is added to the mission namespace (at least in the way you have implemented it). I am not entirely sure about it either so you should make sure to test that too. In case I am right though, you could just put it inside initServer.sqf for MP, or init.sqf for SP.

 

Finally, I am not sure there's a better way to achieve what you want. One minor detail though is that maybe you should also check the inventories of the players, in case someone has the weapon but it is not equipped when the event handler is called.

  • Like 2

Share this post


Link to post
Share on other sites

Thanks @ZaellixA

 

2 hours ago, ZaellixA said:

you could just put it inside initServer.sqf for MP

Ah yes, maybe, but i guess player won't work then.

 

I tried adding it from initServer.sqf but i don't think it can work from there? This give me error.

[missionNamespace, "arsenalClosed", {

[(_this select 0)] spawn {

if (allPlayers findif {primaryWeapon _x isKindOf "BAF_L85A2_RIS_SUSAT"} == -1) then  {

(_this select 0) removeWeaponGlobal "BAF_L85A2_RIS_SUSAT";

};};

}] call BIS_fnc_addScriptedEventHandler;

 

2 hours ago, ZaellixA said:

check the inventories of the players, in case someone has the weapon but it is not equipped when the event handler is called.

Precisely, that's why i'm trying to figure out if there's a way of checking for just the weapontype, even if it's dropped on ground, in primary slots or somewhere else. Maybe a check for what type the weapon is could work? Not sure how to solve it.

 

This is what i found looking around in the forum, if someone could help getting it to work inside the ScriptedEH.

(configFile >> "CfgWeapons" >> _weapon >> "type")

 

if (_weapontype isKindOf ["type", configFile >> "CfgWeapons"]) then {}

 

Share this post


Link to post
Share on other sites

Well, regarding the error you get, it is easy to find out why this is the case. If you check the BIKI for the passed arguments to the scriptedEventHandler you use you'll see that the first argument (what is in _this select 0) is displayNull, which of course you cannot use instead of player.

 

Regarding the problem with the use of player now, you could possibly do something like the following (adapting your code):

[missionNamespace, "arsenalClosed", {
// Pass the code to the scheduler
nul = [] spawn {
	private _allPlrs = allPlayers; // Get all the players
	private _wpnIdx = _allPlrs findIf {_x hasWeapon "BAF_L85A2_RIS_SUSAT"}; // Use hasWeapon

	// Check if anyone has the weapon
	if (_wpnIdx != -1) then  {
		// Remove the weapon
		(_allPlrs select _wpnIdx) removeWeaponGlobal "BAF_L85A2_RIS_SUSAT"; // Use global version of remove weapon for MP compatibility
	};
};
}] call BIS_fnc_addScriptedEventHandler;

Please, once more note that this is not tested and you should treat with caution.

 

Now, regarding the way to check the whole loadout for the weapon you could possibly do something like to following to get the loadout first:

private _unit = player; // This will serve as the unit whose inventory you want to check
private _ldOut = getUnitLoadout _unit; // Get its loadout

Now, you can check this BIKI page to see the structure of the returned loadout. Since you have saved it in a variable you can search in there for the weapon. This could possibly be done like:

// I will use the _unit variable to declare the current unit
private _wpn = "BAF_L85A2_RIS_SUSAT";

// First check if the unit has the weapon
if(_unit hasWeapon _wpn) exitWith {
	_unit removeWeaponGlobal _wpn;
};

// Else get the loadout
private _ldOut = getUnitLoadout _unit;
private _uniform = (_ldOut select 3) select 1; // Get only the content of the uniform
private _vest = (_ldOut select 4) select 1; // Get only the content of the vest
private _bckPack = (_ldOut select 5) select 1; // Get only the content of the backpack

// Search in uniform
private _wpnIdx = _uniform findIf {_wpn in (_x select 0)};

// Check if weapon is found
if(_wpnIdx != -1) exitWith {
	_unit removeWeaponGlobal _wpn;
}

// Search in vest
_wpnIdx = _vest findIf {_wpn in (_x select 0)};

// Check if weapon is found
if(_wpnIdx != -1) exitWith {
	_unit removeWeaponGlobal _wpn;
}

// Search in backpack
_wpnIdx = _bckPack findIf {_wpn in (_x select 0)};

// Check if weapon is found
if(_wpnIdx != -1) exitWith {
	_unit removeWeaponGlobal _wpn;
}

Please keep in mind (once more) that this is untested. Additionally, the searching in the uniform, vest and backpack might need refinement (there may be an error with this _x select 0 part there). Finally, there must be a way to make the code more compact without so many duplicates (or "triplicates" if you would like) in there but I just wanted to provide some insight as to what could a possible solution look like.

 

I do hope this provides a starting point for your solution. Please don't hesitate to ask again if you encounter more issues or you want to refine and/or improve upon any of the presented code or your own versions.

  • Like 1

Share this post


Link to post
Share on other sites
On 12/14/2020 at 1:48 AM, ZaellixA said:

private _allPlrs = allPlayers; // Get all the players private _wpnIdx = _allPlrs findIf {_x hasWeapon "BAF_L85A2_RIS_SUSAT"}; // Use hasWeapon

If i would put the forbidden weapons in an array like below, hasWeapon command won't work.

_weapons = ["BAF_L85A2_RIS_SUSAT","launch_NLAW_F"];

 

Share this post


Link to post
Share on other sites
3 hours ago, Robustcolor said:

If i would put the forbidden weapons in an array like below, hasWeapon command won't work.


_weapons = ["BAF_L85A2_RIS_SUSAT","launch_NLAW_F"];

 

Well, according to the BIKI, hasWeapon expects a single string on the right-hand side of the command. If you place the "banned" weapons in an array and you pass that to the hasWeapon command then you are passing an incorrect type of argument (array vs string). If you want to check for many banned weapons you could possibly do something like (rewriting the provided code with some improvements and corrections):

/*
 * I assume you have the forbiden weapons
 * in an array named _fWpn.
 */

[missionNamespace, "arsenalClosed", {
// Pass the code to the scheduler
nul = [] spawn {
	private _allPlrs = allPlayers; // Get all the players

	{
		// Use a while loop to make sure you will remove ALL "illegal" weapons from the players
		while {true} do {
			// Get some variables
			private _curPlr = _x; // The current player from the array
			private _wpnIdx = _fWpn findIf {_curPlr hasWeapon _x}; // Does the player have a forbiden weapon?
			
			// If no weapon found exit the while-loop
			if(_wpnIdx == -1) exitWith {};

			// Else
			_curPlr removeWeaponGlobal (_fWpn select_wpnIdx); // Remove the weapon
		};
	} forEach _allPlrs;
  };
}] call BIS_fnc_addScriptedEventHandler;

As always 😅, the code is not tested, so treat with caution. Of course, there might be a better way to do that but I do hope that this at least provides some insight towards a valid solution.

 

Please let us know if you happen to need more help or you would like help refining this, or another solution of yours.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×