Jump to content
redarmy

How to apply this with a sleep and loop?

Recommended Posts

essentially im deleting all air vehicles that contain dead crew with the following,in a radio trigger,as most other cleanup ways dont clean up if dead crew are still inside a vehicle.

 

{ 
    if ({alive _x} count crew _x < 1) then { 
        { 
            deleteVehicle _x 
        } count (crew _x); 
        deleteVehicle _x 
    } 
} forEach (nearestObjects [player,["Air"],14000])

I however want to place this into the mission init so it can be handled automatically over time and i wont have to call it on a trigger.

 

i tried the following in init.sqf but got errors:

 

while {true} do {
{ 
    if ({alive _x} count crew _x < 1) then { 
                                     sleep 5;
        { 
            deleteVehicle _x 
        } count (crew _x); 
        deleteVehicle _x 
    } 
} forEach (nearestObjects [player,["Air"],14000]) };

Whats wrong with this code?  

 

EDIT: Clearly somethings horribly wrong here as its locking up the PC lol. Im guessing "while true" is causing this and it needs to be handled differently? is my sleep command wrongly writen?

Share this post


Link to post
Share on other sites
7 hours ago, jazzraill said:

try 


[] spawn
	{
//your code here
};

in init.sqf

i also already tried that but get syntax errors.

Share this post


Link to post
Share on other sites

Try that,

while {sleep 5; true} do{
	{
		if ({alive _x} count crew _x < 1) then {
			deleteVehicleCrew _x;
			deleteVehicle _x;
		};
	}forEach (nearestObjects [player,["Air"],14000])
};
  • Thanks 1

Share this post


Link to post
Share on other sites

red army, your code doesn't work for several reasons:
1) you run the code with a sleep and while loop in an environment that does not accept these commands. In order to stop and loop the script, you need to spawn it or run it using execVM command.
See: Scheduled Environment
2) the block in the count command should return true, but your block does not return anything, therefore the script does not work.
See: count (Alternative Syntax), deleteVehicle (Main Syntax Return Value)

So, try this code in init.sqf:

[] spawn 
	{
		waitUntil {time > 5};
		
		while {true} do 
			{				
				{
					if ({alive _x} count crew _x < 1) then 
						{
							deleteVehicleCrew _x;
							deleteVehicle _x;
						};
				} forEach (nearestObjects [player,["Air"],14000])
				
				sleep 5;
			};
	};

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Why using nearestObjects in such wide area, if you can use vehicles command?

Avoid a while true loop.

Just delete them when they die!

In initServer.sqf:
 

addMissionEventHandler ["EntityKilled", { 
  params ["_killed"]; 
  private _veh = objectParent _killed; 
  call {
    if (_killed isKindOf "air") exitWith {
      deleteVehicleCrew _killed; 
      deleteVehicle _killed; 
    };
    if (_killed in crew _veh) exitWith {deleteVehicle _killed};
  }; 
}];

 

  • Like 1

Share this post


Link to post
Share on other sites
4 hours ago, pierremgi said:

Why using nearestObjects in such wide area, if you can use vehicles command?

Avoid a while true loop.

Just delete them when they die!

In initServer.sqf:
 


addMissionEventHandler ["EntityKilled", { 
  params ["_killed"]; 
  private _veh = objectParent _killed; 
  call {
    if (_killed isKindOf "air") exitWith {
      deleteVehicleCrew _killed; 
      deleteVehicle _killed; 
    };
    if (_killed in crew _veh) exitWith {deleteVehicle _killed};
  }; 
}];

 

This will essentially delete a vehicle immedietly once crew are dead correct? is there any way to put a delay on that? otherwise a helo whose pilots get killed by .50 cal fire will show the helo simply dissapear mid air.

 

Also i wasnt aware the "AddMissioneventhandler". i assumed it had to be placed on each group and i was using a respawn,so this would be perfect if there was a delay. Is it able to be used with a sleep at least??

Share this post


Link to post
Share on other sites

addMissionEventHandler is for... mission, neither group nor unit. Place it in initServer.sqf or init.sqf (depending on what you want to do). Here initserver.sqf is OK for deleting vehicles/units.

 

You can spawn a code like this:

 

addMissionEventHandler ["EntityKilled", {
  params ["_killed"];
  private _veh = objectParent _killed;
  call {
    if (_killed isKindOf "air") exitWith {
      _killed spawn {
        params ["_killed"];
        waitUntil {sleep 2; isTouchingGround _killed};
        sleep 5;
        deleteVehicleCrew _killed;
        deleteVehicle _killed;
      };
    };
    if (_killed in crew _veh) exitWith {
      deleteVehicle _killed
    };
  };
}];

You see the principle.

  • Thanks 1

Share this post


Link to post
Share on other sites
4 hours ago, pierremgi said:

addMissionEventHandler is for... mission, neither group nor unit. Place it in initServer.sqf or init.sqf (depending on what you want to do). Here initserver.sqf is OK for deleting vehicles/units.

 

You can spawn a code like this:

 


addMissionEventHandler ["EntityKilled", {
  params ["_killed"];
  private _veh = objectParent _killed;
  call {
    if (_killed isKindOf "air") exitWith {
      _killed spawn {
        params ["_killed"];
        waitUntil {sleep 2; isTouchingGround _killed};
        sleep 5;
        deleteVehicleCrew _killed;
        deleteVehicle _killed;
      };
    };
    if (_killed in crew _veh) exitWith {
      deleteVehicle _killed
    };
  };
}];

You see the principle.

Oh i see what your doing there,touching ground,thats clever thinking. This is exactly what was needed. Thank you

Share this post


Link to post
Share on other sites

I use this in a re spawning script for single player. It does a few things. 1) Sense I fighting huge battles. I assign a group for the dead then delete that group along with holder group for newly spawned so I can simulate thousands of troops fighting. 2) it waits till the player is a certain distance and a few random seconds to hide the body. You can modify this to suit your needs. I use a killed EH for this script. pierremgi script is pretty great. so here is another option if you want. Always good to learn different ways. Because that one script you disregard I find a lot of time I end up using it else where for different reasons, or parts of a function I am not using.

_deadgroup = createGroup [civilian,true];

if !(isNil "_unit") then {
	[_unit] joinSilent _deadgroup;

	[_unit] spawn {
		params ["_unit"];
		while {!isNil "_unit"} do {
			waitUntil {sleep 1; (isNil "_unit") || (_unit distance player > 100)};
			if !(isNil "_unit") then {
				if (_unit distance player <= 300) then {sleep (1+ (random 30 + random 29))} else {sleep (1+ (random 15 + random 14))};
				hideBody _unit;
				if (_unit distance player <= 300) then {sleep 7} else {sleep 0.5};
				deleteVehicle _unit;
				_unit = Nil;

			};
		};
	};
};

 

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

×