Jump to content
Danskkat

If Armed Then setCaptive False?

Recommended Posts

Okay I'm REALLY new to the editor so excuse the dumb.

 

This co-op mission calls for the players to infiltrate a zone, arm themselves, do their thing, etc. The enemy is supposed to treat them as friendly until they are armed as they should be dressed like civilians.

 

So far my Google Fu has revealed that I can make the RedFor team play it cool by putting all BluFor players as "this setCaptive true"

This is awesome.

 

What I have not been able to figure out is how to set it so that if a player picks up any form of weapon they are "this setCaptive false"

It appears that I can possibly say "if unit acquires item" but that looked a little too confining. I am willing to accept that I may just need to list off all the weapons available in the map and turn them into "if player has [maguffin] then setCaptive false" sorts of things.

 

Thank you!

Share this post


Link to post
Share on other sites

You'll want to combine the "Take" eventHandler with the isKindOf command

 

this addEventHandler ["Take", {
	
	_unit = _this select 0; //the unit doing the action
	_item = _this select 2; //the item taken
	
	if ((_item isKindOf ["Rifle", configFile >> "CfgWeapons"]) || (_item isKindOf ["Pistol", configFile >> "CfgWeapons"]) || (_item isKindOf ["Launcher", configFile >> "CfgWeapons"])) then 
		{
			_unit setCaptive false;
			hint "You picked up a weapon enemies will now be hostile!";
		}; 
}];

 

The eventhandler will fire whenever the unit takes items, and the assigned script using isKindOf will check the item if it's part of a restricted category by looking at the weapon configuration file. For simplicity's sake the example above only checks for rifles, pistols and launchers.

 

Simply add the eventHandler to the player unit init boxes.

 

Check out eventHandlers, odds are they can do a lot of stuff on actions you want already.

  • Like 1

Share this post


Link to post
Share on other sites

if(currentWeapon _unit=="")then{hint "I'm unarmed!";};

  • Like 1

Share this post


Link to post
Share on other sites

You could also use this:

 

TAG_fnc_captiveCheck = {

	params ["_unit"];
	_unit setCaptive true;
	systemchat "You appear as civilian.";

	waitUntil {currentWeapon _unit != ""};

	_unit setCaptive false;
	systemchat "You no longer appear as civilian.";

};

player spawn TAG_fnc_captiveCheck;

 

Cheers

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Thank you for the replies, guys!

 

L3TUC3 - So I add the addEventHandler to each player unit and it will basically wait until the individual player grabs a weapon?

 

Grumpy Old Man - Where would I put that?

Share this post


Link to post
Share on other sites

Depends on how the mission is set up.

onPlayerRespawn.sqf or initPlayerLocal.sqf, from a function or trigger, quite some possibilities.

 

Cheers

Share this post


Link to post
Share on other sites

Grumpy Old Man - Gotcha, thanks. I don't know how to do .sqf's yet. Is it just like a Notepad file I can make?

Share this post


Link to post
Share on other sites

Yes, It's basically a text file with .sqf filename (better be .sqf because of binarization as far as I know).

Might as well start out with one of the most powerful tools for .sqf, made by almighty Tom_48_97.

Has changed scripting for me, can't get back.

 

Cheers

Share this post


Link to post
Share on other sites

Grumpy Old Man - Whoa! That's amazing, downloading now. This might take some time to unpack...

Share this post


Link to post
Share on other sites

I'd recommend using CBA for this.

With CBA loaded you can put this in your initPlayerLocal.sqf and it even will work if player's weapon changes from empty hands to a binoculars - or from weapon in his hands to binoculars. Additionally it checks if the next enemy is no closer than 50 meters and doesn't know enough about the player:

 

private _weapon = currentWeapon player;
if (_weapon isEqualTo "" || _weapon isEqualTo (binocular player)) then {
	player setCaptive true;
};

undercover_switch = {
	params [
		["_unit", player, [objNull]]
		,["_weapon", currentWeapon player, [""]]
	];
	if (_weapon isEqualTo (binocular _unit)) exitWith {};
	if ( _weapon isEqualTo "" && (_unit findNearestEnemy _unit) distance _unit > 50 && (_unit findNearestEnemy _unit) knowsAbout _unit < 1.5 ) exitWith {
		if !(captive _unit) then {
			systemChat "You are now undercover.";
		};
		_unit setCaptive true;
	};
	if (captive _unit) then {
		systemChat "You are no longer undercover.";
	};
	_unit setCaptive false;
};

undercover_evh = ["weapon", {[_this select 0, _this select 1] call undercover_switch}] call CBA_fnc_addPlayerEventHandler;

 

  • Like 3

Share this post


Link to post
Share on other sites

belbo - That's really cool! So from what I'm seeing here is this suggesting that you're only captive or "undercover" as long as you're holding binocs?

Share this post


Link to post
Share on other sites

You become undercover as long as you don't have any weapon in your hand beside your binoculars. If you change to a weapon, you are no longer undercover. The binoculars don't change your previous undercover-status. One problem are vehicles though. That's why I've written this right now:

 

undercover_scriptfnc_switch_inVeh = {
	params [
		["_unit", player, [objNull]]
		,["_veh", vehicle player, [objNull]]
	];
	if ( (_veh currentWeaponTurret [-1]) isEqualTo "" && (_veh currentWeaponTurret [0]) isEqualTo "" ) exitWith {
	};
	if (captive _unit) then {
		systemChat "You are no longer undercover.";
	};
	_unit setCaptive false;
};

undercover_scriptevh_inVeh = ["vehicle", {[_this select 0, _this select 1] call undercover_scriptfnc_switch_inveh}] call CBA_fnc_addPlayerEventHandler;

In addition with the on foot-stuff this should make every scenario thinkable.

  • Like 2

Share this post


Link to post
Share on other sites

One problem with that is findNearestEnemy though. It only returns an enemy if the player actually knows something about the enemy unit. That's quite stupid, but we can write a function that looks for the closest enemy at hand:

 

private _weapon = currentWeapon player;
if (_weapon isEqualTo "" || _weapon isEqualTo (binocular player)) then {
	player setCaptive true;
};

//our function to find the nearest enemy reliably:
fnc_findNearestEnemy = {
	params [
		["_unit", objNull, [objNull]]
		,["_radius", 600, [0]]
		,"_closest"
	];
	//maybe there already is one?
	_closest = (_unit findNearestEnemy _unit);
	if ( !isNull _closest ) exitWith {
		_closest;
	};
	//who are our enemies?
    private _enemySides = [side _unit] call BIS_fnc_enemySides;
  	//are there any enemies within the radius?
	private _nearEnemies = allUnits select { (_x distance _unit) < _radius && (side _x) in _enemySides};
	//let's loop through the enemies to find the closest one:
	private _closestdist = _radius+1;
	{
		if (_x distance _unit < _closestdist) then {
			_closest = _x;
			_closestdist = _x distance _unit;
		};
	} forEach _nearEnemies;
	//return the closest at the end of the function:
	_closest;
};
//the next part is what happens if the player changes his weapon (on foot):
undercover_scriptfnc_switch_onfoot = {
	params [
		["_unit", player, [objNull]]
		,["_weapon", currentWeapon player, [""]]
	];
  	//if the player changed to binoculars, there will be no change (if he was undercover, he will remain undercover - if not, then not)
	if (_weapon isEqualTo (binocular _unit)) exitWith {};
  	//if the player changes to no weapon AND the next enemy is further away then 50 meters AND no enemy knows something about the player within 600 meters he will go undercover:
	if ( _weapon isEqualTo "" && ([_unit,60] call fnc_findNearestEnemy) distance _unit > 50 && ([_unit,600] call fnc_findNearestEnemy) knowsAbout _unit < 1.5 ) exitWith {
      	//he should know about it, if it worked, but not if he already was undercover:
		if !(captive _unit) then {
			systemChat "You are now undercover.";
		};
      	//the "hear" of our code:
		_unit setCaptive true;
	};
  	//the stuff below here is being executed, if none of the above applied:
	//if player was undercover, he should know that he is not anymore:
  	if (captive _unit) then {
		systemChat "You are no longer undercover.";
	};
  	//and this switches him back to his regular self:
	_unit setCaptive false;
};
//the following is what appens if the player jumps into a vehicle:
undercover_scriptfnc_switch_inVeh = {
	params [
		["_unit", player, [objNull]]
		,["_veh", vehicle player, [objNull]]
	];
  	//if the vehicle has no weapons, nothing will change:
	if ( (_veh currentWeaponTurret [-1]) isEqualTo "" && (_veh currentWeaponTurret [0]) isEqualTo "" ) exitWith {};
	//but if it did, this will happen:
  	if (captive _unit) then {
		systemChat "You are no longer undercover.";
	};
  	//the player is no longer undercover:
	_unit setCaptive false;
};

//and here we add the cba-playerEventHandler: They trigger everytime something happens and will execute some code:
//this one executes the code within {} (which is our undercover_scriptfnc_switch_onfoot from above) if the player changes his weapon:
undercover_scriptevh_onFoot = ["weapon", {[_this select 0, _this select 1] call undercover_scriptfnc_switch_onfoot}] call CBA_fnc_addPlayerEventHandler;
//this one executes the code within {} (which is our undercover_scriptfnc_switch_inVeh from above) if the player changes his vehicle:
undercover_scriptevh_inVeh = ["vehicle", {[_this select 0, _this select 1] call undercover_scriptfnc_switch_inveh}] call CBA_fnc_addPlayerEventHandler;
//and done.

Now it'll check reliably for the next enemy. If it finds an enemy within 50 meters or if any enemy in a radius of 600 meters knows about the player while the player tries to go undercover, the player will not go into undercover mode.

  • Like 3

Share this post


Link to post
Share on other sites

Okay, so how do I give someone mad props on this forum?

I'm loving your notes belbo, thank you so much for writing this out. Bookmarked, saved, already seeing what I can reverse engineer from what you did. SO VERY cool.

Did you just write that so someone can go BACK undercover if no enemy is aware of them?

Share this post


Link to post
Share on other sites

Not automatically, no. But if he switches to his weapon and back to no weapon again, he will go undercover again.

  • Like 1

Share this post


Link to post
Share on other sites

Right, so if someone went full hostile, broke cover, then vamoosed and took cover, enemy broke contact, he put away his weapons could theoretically be back to "undercover?"

Share this post


Link to post
Share on other sites

Yeah, but only if absolutely no enemy within a radius of 600 meter knows that the player is even there. knowsAbout 1.5 is the minimum amount of knowledge (except for no knowledge, of course). If the player has a firefight and breaks contact, the enemies around him will have at least knowsAbout 2 or rather even 4.

That's what this does:

([_unit,60] call fnc_findNearestEnemy) distance _unit > 50 && ([_unit,600] call fnc_findNearestEnemy) knowsAbout _unit < 1.5

The first part means: if no enemy is within 50 meters at all; the second part means that no enemy may know something about the player in a radius of 600 meters. If any of these conditions are not met, the player can't go undercover.

Share this post


Link to post
Share on other sites

1394752126931_zpssibe67s2.jpg

You are the coolest person ever. Thank you so much for helping out with my question and going so much further! <3

Share this post


Link to post
Share on other sites
On 3/24/2017 at 12:25 AM, Belbo said:

I'd recommend using CBA for this.

With CBA loaded you can put this in your initPlayerLocal.sqf and it even will work if player's weapon changes from empty hands to a binoculars - or from weapon in his hands to binoculars. Additionally it checks if the next enemy is no closer than 50 meters and doesn't know enough about the player:

 


private _weapon = currentWeapon player;
if (_weapon isEqualTo "" || _weapon isEqualTo (binocular player)) then {
	player setCaptive true;
};

undercover_switch = {
	params [
		["_unit", player, [objNull]]
		,["_weapon", currentWeapon player, [""]]
	];
	if (_weapon isEqualTo (binocular _unit)) exitWith {};
	if ( _weapon isEqualTo "" && (_unit findNearestEnemy _unit) distance _unit > 50 && (_unit findNearestEnemy _unit) knowsAbout _unit < 1.5 ) exitWith {
		if !(captive _unit) then {
			systemChat "You are now undercover.";
		};
		_unit setCaptive true;
	};
	if (captive _unit) then {
		systemChat "You are no longer undercover.";
	};
	_unit setCaptive false;
};

undercover_evh = ["weapon", {[_this select 0, _this select 1] call undercover_switch}] call CBA_fnc_addPlayerEventHandler;

 

wow, that's great. It actually works! Thanks for the post; Belbo!

Share this post


Link to post
Share on other sites
On 3/24/2017 at 7:27 AM, Belbo said:

One problem with that is findNearestEnemy though. It only returns an enemy if the player actually knows something about the enemy unit. That's quite stupid, but we can write a function that looks for the closest enemy at hand:

 


private _weapon = currentWeapon player;
if (_weapon isEqualTo "" || _weapon isEqualTo (binocular player)) then {
	player setCaptive true;
};

//our function to find the nearest enemy reliably:
fnc_findNearestEnemy = {
	params [
		["_unit", objNull, [objNull]]
		,["_radius", 600, [0]]
		,"_closest"
	];
	//maybe there already is one?
	_closest = (_unit findNearestEnemy _unit);
	if ( !isNull _closest ) exitWith {
		_closest;
	};
	//who are our enemies?
    private _enemySides = [side _unit] call BIS_fnc_enemySides;
  	//are there any enemies within the radius?
	private _nearEnemies = allUnits select { (_x distance _unit) < _radius && (side _x) in _enemySides};
	//let's loop through the enemies to find the closest one:
	private _closestdist = _radius+1;
	{
		if (_x distance _unit < _closestdist) then {
			_closest = _x;
			_closestdist = _x distance _unit;
		};
	} forEach _nearEnemies;
	//return the closest at the end of the function:
	_closest;
};
//the next part is what happens if the player changes his weapon (on foot):
undercover_scriptfnc_switch_onfoot = {
	params [
		["_unit", player, [objNull]]
		,["_weapon", currentWeapon player, [""]]
	];
  	//if the player changed to binoculars, there will be no change (if he was undercover, he will remain undercover - if not, then not)
	if (_weapon isEqualTo (binocular _unit)) exitWith {};
  	//if the player changes to no weapon AND the next enemy is further away then 50 meters AND no enemy knows something about the player within 600 meters he will go undercover:
	if ( _weapon isEqualTo "" && ([_unit,60] call fnc_findNearestEnemy) distance _unit > 50 && ([_unit,600] call fnc_findNearestEnemy) knowsAbout _unit < 1.5 ) exitWith {
      	//he should know about it, if it worked, but not if he already was undercover:
		if !(captive _unit) then {
			systemChat "You are now undercover.";
		};
      	//the "hear" of our code:
		_unit setCaptive true;
	};
  	//the stuff below here is being executed, if none of the above applied:
	//if player was undercover, he should know that he is not anymore:
  	if (captive _unit) then {
		systemChat "You are no longer undercover.";
	};
  	//and this switches him back to his regular self:
	_unit setCaptive false;
};
//the following is what appens if the player jumps into a vehicle:
undercover_scriptfnc_switch_inVeh = {
	params [
		["_unit", player, [objNull]]
		,["_veh", vehicle player, [objNull]]
	];
  	//if the vehicle has no weapons, nothing will change:
	if ( (_veh currentWeaponTurret [-1]) isEqualTo "" && (_veh currentWeaponTurret [0]) isEqualTo "" ) exitWith {};
	//but if it did, this will happen:
  	if (captive _unit) then {
		systemChat "You are no longer undercover.";
	};
  	//the player is no longer undercover:
	_unit setCaptive false;
};

//and here we add the cba-playerEventHandler: They trigger everytime something happens and will execute some code:
//this one executes the code within {} (which is our undercover_scriptfnc_switch_onfoot from above) if the player changes his weapon:
undercover_scriptevh_onFoot = ["weapon", {[_this select 0, _this select 1] call undercover_scriptfnc_switch_onfoot}] call CBA_fnc_addPlayerEventHandler;
//this one executes the code within {} (which is our undercover_scriptfnc_switch_inVeh from above) if the player changes his vehicle:
undercover_scriptevh_inVeh = ["vehicle", {[_this select 0, _this select 1] call undercover_scriptfnc_switch_inveh}] call CBA_fnc_addPlayerEventHandler;
//and done.

Now it'll check reliably for the next enemy. If it finds an enemy within 50 meters or if any enemy in a radius of 600 meters knows about the player while the player tries to go undercover, the player will not go into undercover mode.


Need help

i created a initPlayerLocal.sqf file with this script inside, i wrote it down line by line, but the way it works in the game right now is whenever i equip gun, it always make me go uncaptive, and i can’t go back undercover even though there isn’t any enemy around me. How can i fix it?

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

×