Jump to content
feldmaus

respawn pilot and his vehicle and get pilot into vehicle again

Recommended Posts

Good Morning,

 

for my SP/MP Mission i modified the script from Evo Dan's vehicle respawn script for vehilce respawn and made a new script for respawn AI Soldier-units. When the Chopper and the AI gets killed, the Chopper and pilot gets spawned at the Base, then the Pilot shall get in his specific vehicle. I have some more Pilots for CAS/EVAC/Halo/Support. My scripts are working, but my new Pilot doesnt know anymore where his old vehicle is, because the old vehicle got deleted and he didnt got the reference to the new vehicle.

 

Here are my scripts,

 

Vehicle Respawn Script:

/*  [EVO] Dan's vehicle respawn script modified by Steam-User immerfestedruv
    In the Unit Init Field insert <0 = [this,5] execVM "vehicle_respawn.sqf";> for example.
    Will run only on the server, as it only needs to run in one place,
    but obviously can be used in both singleplayer and multiplayer */

_vehicle = _this select 0; // First Argument, get vehicle's details
_respawntime = _this select 1; // Second Argument, 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
_vehicleName = vehicleVarName _vehicle; // get the Variable-Name from the Vehicle
_n = 1;

if(isServer) then{
    while{_n == 1} do{
        hint format ["Fahrzeug-Variablen-Name lautet ",_vehicleName];
        if((!alive _vehicle) || (!canMove _vehicle)) then { //true if vehicle is not alive or it cannot move
            sleep 240; // respawn time between respawn/move and deletion
            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 setVehicleVarName _vehicleName;
            
            /* Next Code-Line will be executed for the new Object by your CPU, but time of execution unkown.
            This behaves similar to the code in the init-field of your object. */
            [[[_vehicle,_respawntime],"vehicle_respawn.sqf"],"BIS_fnc_execVM",false,false] spawn BIS_fnc_MP;
            _n = 0; // break out condition
        };
        sleep 120; // sleep for a bit in order to reduce processing calls
    };
};

 

Unit Respawn Script:

/*  Based on [EVO] Dan's vehicle respawn script modified by Steam-User immerfestedruv
    In the Unit Init Field insert <0 = [this,5,blackfish_1,"Driver"] execVM "unit_respawn.sqf";> for example.
    Will run only on the server, as it only needs to run in one place,
    but obviously can be used in both singleplayer and multiplayer */

_unit = _this select 0; // First Argument, get Unit's details
_respawntime = _this select 1; // Second Argument,get the init set respawn time
_vehicle = _this select 2; // Third Argument, Vehicle in which the unit shall get in
_vehicleSeat = _this select 3; // Forth Argument, on which Seat shall the unit sit
_facingofunit = getDir _unit; // get original facing
_positionofunit = getPosATL _unit; // get original position
_unittype = typeOf _unit; // get the unit type
_unitgroup = group _unit; // get group of the original unit
_unitName = vehicleVarName _unit; // get the Variable-Name of the Vehicle
_unitRank = rank _unit; // get the units orignal rank
_unitSkill = skill _unit; // get the original skill of the unit
_n = 1;

if(isServer) then{
    while{_n == 1} do{
        if (alive _vehicle)
        then {	hint format ["Fahrzeug %1 lebt noch.",_vehicle];
            switch (_vehicleSeat) do {
                case "Driver": {_unit moveInDriver _vehicle};
                case "Gunner": {_unit moveInGunner _vehicle};
                case "Turret": {_unit moveInTurret _vehicle};
                case "Commander": {_unit moveInCommander _vehicle};
                default {_unit moveInCargo _vehicle};
            };
        } else {hint format ["Kein Fahrzeug für %1 ",_unit];};
        if((!alive _unit) || (!canMove _unit)) then { //true if unit is not alive or it cannot move
            sleep 120; // additional time delay between unit being killed and unit being deleted			
            
            _unit setPosATL [0,0,0]; // move old unit away before respawn new unit
            sleep 1; // wait a bit for safety
            
            // create a new unit of same type at starting position
            _unittype createUnit [_positionofunit, _unitgroup,"newUnit = this", _unitSkill, _unitRank]; 
            newUnit setVehicleVarName _unitName;
            newUnit setPosATL _positionofunit; // set correct position
            newUnit setDir _facingofunit; // set correct facing of the unit

            sleep _respawntime; // respawn time between respawn/move and deletion
            deleteVehicle _unit; // clear up old units?
            
            /* Next Code-Line will be executed for the new Object by your CPU, but time of execution unkown.
            This behaves similar to the code in the init-field of your object. */
            [[[newUnit,_respawntime,_vehicle,_vehicleSeat],"unit_respawn.sqf"],"BIS_fnc_execVM",false,false] spawn BIS_fnc_MP;
            _n = 0; // break out condition
        };
        sleep 60; // sleep for a bit in order to reduce processing calls
    };
};

How do i can get my Pilot in his new specific Chopper?

 

And is it useful to use the command setVehicleVarName/vehicleVarName in a Multiplayer/Internet/Singleplayer Mission?

 

Another idea would be to not delete the vehicle but to repair it and move it to origin position. How to?

 

regards

 

EDIT: Link to my working but not finished and optimized script,

unit_respawn.sqf

vehicle_respawn.sqf

Share this post


Link to post
Share on other sites

I got a bit further. After the vehicle and my Pilot respawns, he will start to search for his vehicle with the command <nearestObjects>,

            _nO = nearestObjects [_vehiclePosition,[],100];
            for "_i" from 0 to (count _nO - 1) do {
                _xy = _nO select _i;
                if ({vehicleVarName _xy}==_vehicleName) then {
                    hint "interessant!";
                };
            };

It doesnt work, because of the wrong bracket syntax. Need Help. :-)

Does somebody know how i can optimize my code. I dont wanna create temporary objects if not needed.

 

regards

Share this post


Link to post
Share on other sites

The following piece of Code does work but is performance side a nightmare,

 

            _nO = nearestObjects [_vehiclePosition,[],100];
            for "_i" from 0 to (count _nO - 1) do {
                _xy = _nO select _i;
                _xyV = vehicleVarName _xy;
                if (_xyV==_vehicleName) then {
                    hint "interessant!";
                };
            };

Anyone has hints for me to get my code optimized?

Share this post


Link to post
Share on other sites

Why don't you just use createVehicleCrew to spawn the pilot inside the chopper?

You could always setPos him somewhere else and let him run back to his assigned vehicle if that's the plan.

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

or use the vehicle respawn module. You can write a code into it. Works in SP/MP

Share this post


Link to post
Share on other sites

@Grumpy Old Man, just one reason is that my Base is on the USS Nimitz career and the spawn modules doesnt work as well on ships. Then i can not differ between units. Player units shall not respawn in my mission. And i worked a hole day on the respawn module and the Garbage collection but didnt got it together to work.

 

If the Garbage collector deletes vehicles and units, then these units/vehicles can not respawn, because the dead units dont exist anymore?! Means i can not use both together?

 

And when i get the respawn module without garbage collector to work the dead bodies will lie on my ship deck until they gets deleted, but how can the new units spawn on their last position, when the dead units didnt got deleted before?

Share this post


Link to post
Share on other sites

Good evening,

 

would be very glad if anyone can check my Code,

                _nearObjs = nearestObjects [_vehPos,[],30];
                for "_i" from 0 to (count _nearObjs - 1) do {
                    if ((vehicleVarName (_nearObjs select _i)) == _vehName) then {
                        if (!isNull (_nearObjs select _i)) then {
                            if (alive (_nearObjs select _i)) then {
                                _vehicle = _nearObjs select _i;
                            };
                        };
                    };
                };

It gives me a wrong object. Any hint or idea?

 

Further on i will be very thankful over every optimisation hint. You get my actual Code in my first thread at the bottom or,

unit_respawn.sqf

Share this post


Link to post
Share on other sites

Hello,

like this your code can fail because _vehpos and _vehName are undefined. but, tested, if you're writing:  _vehname = "car1" and _vehpos = getpos _vehname; before, and place a vehicle named car1 in editor, then your spawned code will return car1.

I still don't understand what so complicated code.

if (!isNil "car1" && {car1 distance _vehPos < 30}) then {_vehicle == car1}; should do the same trick faster as far as you defined _vehpos.

Share this post


Link to post
Share on other sites

Thanks,

 

the Object which was passing my if conditions was called <L Modules:1>, i dont know it, but there must be some wrong with my code, because this modul has no vehicleVarName. Ich checked the variables. Because of this fact my code in Line 59 must be wrong or the _vehName was not defined.

 

(vehicleVarName (_nearObjs select _i)) == _vehName

The vehName was defined in the top of my sqf file.

 

cheers

Share this post


Link to post
Share on other sites

Thanks to all,

 

got it to work. Problem was the script was fit only to my initialization but not after the first respawn. I post my new script in the first thread at the bottom line. I am not sure if i should make a new thread, because now i try to inherite the syncronization from my first vehicle to the new vehicles.

 

regards

Share this post


Link to post
Share on other sites

How to respawn manned (or not) vehicle, air/ground/sea, with their previous waypoints:

For those would like something simple working:

 

- Place a vehicle respawn module;

- link it to any vehicle you want;

- write in expression field of the module :

if (count crew (_this select 1) >0) then {createVehicleCrew  (_this select 0); _gr = group (_this select 0); _gr copyWaypoints (group (_this select 1))};

 

Enjoy.

Share this post


Link to post
Share on other sites

Hi @pierremgi,

 

thank you for your help.

 

i have the <createvehicle> command in my script at it doesnt place the vehicles on the top of the career. The respawn module doesn't too, even with respawn position module. So i need at least the setPosATL command. And because of the limited place on the career it has to be respawned at a specific place on the deck. If this place where the vehicle will respawn isn't empty then it jumps up and get damaged or will be destroyed again. When a vehicle on my ship explodes, it will stay there as wreck, when a new vehicle respawns their where the wreck is, it explodes again. If i clean the ship deck with the clean module it deletes the vehicle object, so that the vehicle object can not respawn again.

 

But despite these facts, my code gives me some more realistic features.

+ time dependent service request. After getting destroyed it will take time to get the service again, depending on the aircraft type. Each aircraft has it own respawn time.

+ Animation of the pilots, when they go to their vehicle and then get in to it(maybe future), this takes also time.

 

regards

Share this post


Link to post
Share on other sites
15 minutes ago, feldmaus said:

Hi @pierremgi,

 

thank you for your help.

 

i have the <createvehicle> command in my script at it doesnt place the vehicles on the top of the career. The respawn module doesn't too, even with respawn position module. So i need at least the setPosATL command. And because of the limited place on the career it has to be respawned at a specific place on the deck. If this place where the vehicle will respawn isn't empty then it jumps up and get damaged or will be destroyed again. When a vehicle on my ship explodes, it will stay there as wreck, when a new vehicle respawns their where the wreck is, it explodes again. If i clean the ship deck with the clean module it deletes the vehicle object, so that the vehicle object can not respawn again.

 

But despite these facts, my code gives me some more realistic features.

+ time dependent service request. After getting destroyed it will take time to get the service again, depending on the aircraft type. Each aircraft has it own respawn time.

+ Animation of the pilots, when they go to their vehicle and then get in to it(maybe future), this takes also time.

 

regards

Yes, carrier environment is garbled by some altitude consideration and automatic behavior; Nothing difficult but some workaround!

Share this post


Link to post
Share on other sites

Hi All,

 

i want to change my Code to avoid the loops in my script, therefore i want to insert the addEventHandler in my AI pilots Init Line to execute my script. And in my script i want to insert an <addEventHandler> in the init argument, is this possible?

 

May AI respawn like players to keep their Init-Code?

Or may AI only spawn?

How can i add Code to the Init of my AI after <createVehicle>?

 

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

×