Jump to content
Sign in to follow this  
macusercom

Triggering setCaptive inside a certain vehicle

Recommended Posts

Hey there!

I want to make a script that people inside a MEDEVAC won't get hurt by enemy AIs. This should work with the setCaptive command, but I have some problems at the moment.

I found a script for this, but I want to either use it for a certain vehicle names or that it automatically detects MEDEVACs (in ACE).

private ["_ldr","_i","_unit"];

_ldr = _this select 0;

while {count (units group _ldr) >=1} do {

for [{_i=0},{_i<count (units group _ldr)},{_i=_i+1}] do {

_unit = (units group _ldr) select _i;

if (vehicle _unit == _unit) then {

if (captive _unit) then {_unit setcaptive false};

} else {

if (! captive _unit) then {_unit setcaptive true};

};

sleep 0.01;

};

sleep 1;

};

I tried this one, but it doesn't seem to work. In this example I wanted to tell the script that the player should be inside the vehicle which HAS to be named "UH60". I guess I'm not the best in scripting oO

private ["_ldr","_i","_unit"];

_ldr = _this select 0;

while {count (units group _ldr) >=1} do {

for [{_i=0},{_i<count (units group _ldr)},{_i=_i+1}] do {

_unit = (units group _ldr) select _i;

if (vehicle _unit == _unit) and ((vehicle _unit) is "UH60") then {

if (captive _unit) then {_unit setcaptive false};

} else {

if (! captive _unit) then {_unit setcaptive true};

};

sleep 0.01;

};

sleep 1;

};

The solution to enable this even for multiple vehicles for all classes if you have them (e.g. to have a MEDEVAC, that won't get shot) is here (thanks to XxAnimusxX!):

(Just paste it into an .sqf file and activate it through the mission's init.sqf with: execVM "scripts\YOURFILENAME.sqf")

captiveInVehicle = 
{
   (_this select 2) setCaptive ((_this select 2) in (_this select 0));
};

_vehicleTypes = ["UH60M_MEV_EP1", "M1133_MEV_EP1"];
{
   if ((typeOf _x) in _vehicleTypes ) then
   {
       {
           _x setCaptive true;
       } forEach crew _x;

       _x addEventHandler ["GetIn", "_this call captiveInVehicle;"];
       _x addEventHandler ["GetOut", "_this call captiveInVehicle;"];
   };
} forEach vehicles;

For just one vehicle it's here:

(Paste it into the vehicle's init in the mission editor)

this addEventHandler ["GetIn", {(_this select 2) setCaptive true;}]; 
this addEventHandler ["GetOut", {(_this select 2) setCaptive false;}];

Edited by Macusercom

Share this post


Link to post
Share on other sites

How about using an event handler which automatically sets the player getting in to captive and deactivates it upon disembarking:

UH60_medevac addEventHandler ["GetIn", {(_this select 2) setCaptive true;}];
UH60_medevac addEventHandler ["GetOut", {(_this select 2) setCaptive false;}];

And if you have the superclass of your vehicle or the exact classname, you could do an automatic search:

captiveInVehicle = {
   (_this select 2) setCaptive ((_this select 2) in (_this select 0));
};
{
   if (_x isKindOf "UH60") then
   {
       _x addEventHandler ["GetIn", "_this call captiveInVehicle;"];
       _x addEventHandler ["GetOut", "_this call captiveInVehicle;"];
   };
} forEach vehicles;

Edited by XxAnimusxX

Share this post


Link to post
Share on other sites

My problem was that if someone leaves the vehicle while someone is still inside of it, the AIs will start shooting at the vehicle and at the soldier, who got out a moment before. I think it disables "captive" for all if someone gets out.

Share this post


Link to post
Share on other sites

Did this problem occur while testing the code I supplied or with your own code? The "my problem was" confuses me somehow :D

Well of course the AI will target and engage the soldiers who get out of the heli, because they're not captive anymore if they do.

The rest of your soldiers in the heli should be captive though, but please be advised that the eventhandler will only trigger upon someone

entering/leaving the vehicle, if you script-move your soldiers into your heli or spawn the heli with soldiers, they won't be captive.

But we can easily overcome that problem:

captiveInVehicle = {
   (_this select 2) setCaptive ((_this select 2) in (_this select 0));
};
{
   if (_x isKindOf "UH60M_MEV_EP1") then
   {
       {
           _x setCaptive true;
       } forEach crew _x;

       _x addEventHandler ["GetIn", "_this call captiveInVehicle;"];
       _x addEventHandler ["GetOut", "_this call captiveInVehicle;"];
   };
} forEach vehicles; 

Share this post


Link to post
Share on other sites

I'm sorry. The script that I had caused these weird problems. Your script is working fine. The AIs are attacking the chopper sometimes, but only if a friendly unit is in front of it :D

Could you please tell me how to add classnames to your script, so that I can say e.g. isKindOf "UH60M_MEV_EP1" and "M1133_MEV_EP1"? (Thanks for also including the ID for the MEDEVAC Blackhawk though!) :)

Share this post


Link to post
Share on other sites

If you have a static set of classnames, you could do it like this:

captiveInVehicle = 
{
   (_this select 2) setCaptive ((_this select 2) in (_this select 0));
};

_vehicleTypes = ["UH60M_MEV_EP1", "M1133_MEV_EP1"];
{
   if ((typeOf _x) in _vehicleTypes ) then
   {
       {
           _x setCaptive true;
       } forEach crew _x;

       _x addEventHandler ["GetIn", "_this call captiveInVehicle;"];
       _x addEventHandler ["GetOut", "_this call captiveInVehicle;"];
   };
} forEach vehicles;  

Now you can easily add more classes to the _vehicleTypes-Array.

Share this post


Link to post
Share on other sites

That was the thing I was searching for. Thank you very, very much. You just helped me creating a useful MEDEVAC for our missions! Greetings from Austria! :)

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
Sign in to follow this  

×