Jump to content
Sign in to follow this  
mr_shadow

Keep name and init of vehicle after respawn

Recommended Posts

Hello guys, i was trying to add lines where the vechicle name and init will be the same after respawn.

But I failed. Can you please help me to add it?

This code is correct:

_vehicle = _this select 0; //get vehicle's details
_respawntime = _this select 1; //get the init set respawn time
_facingofvehicle = getDir _vehicle; //get original facing
_positionofvehicle = getPosATL _vehicle; //get original position
_vehicletype = typeOf _vehicle; //get the vehicle type
_n = 1;

if(isServer) then{
while{_n == 1} do{
	if((!alive _vehicle) || (!canMove _vehicle)) then { //true if vehicle is not alive or it cannot move
		sleep 4; //change this to what you like, longer will give you a bigger response delay between unit being killed and the vehicle being deleted
		deleteVehicle _vehicle; //clear up old vehicle
		sleep _respawntime; // respawn time between deletion and then respawn
		_vehicle = _vehicletype createVehicle _positionofvehicle; // create a new vehicle of same type at starting position
		_vehicle setPosATL _positionofvehicle; //set correct position


		_vehicle setDir _facingofvehicle; //set correct facing of the vehicle
		[[[_vehicle,_respawntime],"vehicle_respawn.sqf"],"BIS_fnc_execVM",false,false] spawn BIS_fnc_MP; //replacement for the old setVehicleInit, this does the same and causes the new vehicle to have the respawn script when created
		_n = 0; // break out condition
	};
	sleep 60; // sleep for a bit in order to reduce processing calls (increase this to whatever you like, longer gives better performance but also increases response delay)
};
};

And i was trying to add this:

_unitinit = if (count _this > 6) then {_this select 6} else {};
_haveinit = if (count _this > 6) then {true} else {false};

_unitname = vehicleVarName _vehicle;


if (_haveinit) then 
{_vehicle setVehicleInit format ["%1;", _unitinit];
processInitCommands;};
if (_hasname) then 
{_vehicle setVehicleInit format ["%1 = this; this setVehicleVarName ""%1""",_unitname];
processInitCommands;};

Share this post


Link to post
Share on other sites

setVehicleInit was removed as a valid command in the early stages of Alpha due to security reasons.

You can probably add in a respawn eventHandler to do the same job.

Share this post


Link to post
Share on other sites

Can u give me an example with eventhandler?

Dont have enough experience with it.

Share this post


Link to post
Share on other sites

I'll have a look when I get home if no-ones solved it for you by then.

Need to check on my Arma PC

Share this post


Link to post
Share on other sites

	_vehicle = _this select 0;
_respawntime = _this select 1;
_facingofvehicle = getDir _vehicle;
_positionofvehicle = getPosATL _vehicle;
_vehicletype = typeOf _vehicle;
_vehicleName = vehicleVarName _vehicle; //Name of vehicle from editor

if(isServer) then{
	while{true} do{ //Loop forever
		if((!alive _vehicle) || (!canMove _vehicle)) then { //If dead or disabled
			waitUntil {count crew _vehicle == 0}; // wait until vehicle is empty
			deleteVehicle _vehicle;
			sleep _respawntime;
			_vehicle = _vehicletype createVehicle _positionofvehicle;
			_vehicle setPosATL _positionofvehicle;
			_vehicle setDir _facingofvehicle;
			missionNamespace setVariable [_vehicleName, _vehicle]; //Update vehicle name to point to new vehicle
			publicVariable _vehicleName; //broadcast variable
		};
		sleep 60;
	};
};

Is enough to get the vehicle respawning and giving it the same name as the original vehicle.

No need for the BIS_fnc_MP as this is only going to run on the server as its wrapped in a isServer condition, so just recreate the vehicle and update the reference of the mission name space variable to the new vehicle.

Added a wait until there is no crew, so if the vehicle is only disabled (!canMove) then its not deleted while there are still people in it.

Maybe worth adding some kind of distance check to stop the vehicle being deleted whilst in view of players?

Only thing i don't like about this is the sleep 60, means respawn time could be anywhere between # and #+60 seconds (where # is the respawn time passed to the function). (easy to fix just make the respawning condition a waitUntil rather than checking IF every 60 seconds, but this does mean the script is checked in the schedule more often which is what the 60 seconds is kind of trying to avoid, although whats the difference between the schedule doing a quick condition check and the schedule checking if sleep time has passed?? Does sleep remove the script from the scheduler on some kind of internal call back or is it still in the schedule and being checked , whats the overhead difference??) Sorry offTopic just thinking out loud.

As for the init im not sure there is a way to retrieve it since the commands were removed, just do what you want in this script or to make it compatible for use with multiple vehicles pass the script any code you want run for this particular vehicle e.g

// [this, 15, {_this setdamage 0.5}] spawn vehicleRespawn
_vehicleInit = _this select 2;
_vehicle call _vehicleInit;

Edited by Larrow

Share this post


Link to post
Share on other sites
Already tryed.

What did you try ??? the script or the functions hasname ?

Because if you want to send the name with the entire script you must correct before the line if (isNil _unitname) then {_hasname = false;} else {_hasname = true;};

by this one : if (isNil '_unitname') then {_hasname = false;} else {_hasname = true;};

Share this post


Link to post
Share on other sites

Thanks Larrow , its works.

What did you try ??? the script or the functions hasname ?

Yes genious, Ive tryed what you just posted.

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  

×