Hello all i have a small issue with a script I'm creating and I'm hoping that someone has experience with addSwitchableUnit that can help me solve this.
My issue is that for some reason after switching units my old unit remains and i loose the ability to respawn from the new unit.
Any help welcomed.
DCV_ChangeClass =
{
//["Citizen4",player] call DCV_ChangeClass;
private ["_NewType", "_infoarray", "_currentunit", "_OrginalDir", "_OrginalPosition", "_OrginalView", "_newUnit", "_dummyUnit", "_dummyGroup", "_fSelectWeapon","_player"];
_NewType = _this select 0;
_player = _this select 1;
_Currentunit = _player;
_OrginalDir = getdir _player;
_OrginalPosition = getpos _player;
_OrginalView = cameraView;
//Create an array of the current players information
_infoarray =
[
//Get Current weapons
weapons _currentunit,
//Get Current Ammo
magazines _currentunit,
//Get Current Rank
rank _currentunit,
//Get Current Score
score _currentunit,
//Get Current Group
group _currentunit,
//Get Current location
getPos _currentunit,
//Check if group leader is player
(leader _currentunit) == _currentunit,
//Get original name
vehicleVarName _currentunit
];
//Function to select players current weapon and bring it up ready for use
_fSelectWeapon =
{
private ["_unit", "_weap", "_cfg", "_muz", "_array"];
_unit = _this select 0;
_weap = _this select 1;
_cfg = (configFile >> "CfgWeapons" >> _weap >> "muzzles");
if (isArray _cfg) then
{
_array = getArray _cfg;
_muz = _array select 0;
if (_muz == "this") then
{
_muz = _weap;
};
}else{
_muz = _weap;
};
if (player hasWeapon _weap) then
{
_unit selectWeapon _muz;
};
};
//Create Dummy Group used to create the new unit
_dummyGroup = createGroup (side _currentunit);
_dummyUnit = (_infoarray select 4) createUnit [_NewType, [1000,10,0], [], 0, "NONE"];
//Change player group to the dummy group
[_currentunit] join _dummyGroup;
//Create the New unit to be spawned info
_newUnit = _dummyGroup createUnit [_NewType, [(getpos _player select 0)+200,(getpos player select 1)+200,0], [], 0, "NONE"];
//Make the new unit switchable
addSwitchableUnit _newUnit;
//Display some output to the user while we switch them
titleCut ["Changing Outfit","black faded", 2];
//DEBUG
_player sidechat format ["Name before: %1",name _player];
sleep 1;
//Clean out old eventhandelers from old unit
_currentunit removeAllEventHandlers "killed";
_currentunit removeAllEventHandlers "Fired";
_currentunit removeAllEventHandlers "hit";
_currentunit removeAllEventHandlers "getin";
_currentunit removeAllEventHandlers "getout";
//Set Old units name to old(original name)
_currentunit setVehicleInit format["this setVehicleVarName 'old%1'; old%1 = this", _infoarray select 7];
//Set New units name to original name
_newUnit setVehicleInit format["this setVehicleVarName '%1'; %1 = this;", _infoarray select 7];
//Process both units init commands
processInitCommands;
//Debug Output
player sidechat format ["Name after: %1",name _player];
sleep 0.5;
//Transfer player to new unit
selectNoPlayer;
setPlayable _newUnit;
selectPlayer _newUnit;
[_newUnit] joinsilent startgroup;
startgroup selectLeader _newUnit;
//Remove Temp Group
deleteVehicle _dummyUnit;
deletegroup _dummyGroup;
//Remove Player switch on new and old units
removeSwitchableUnit _newUnit;
removeSwitchableUnit _currentunit;
sleep 1;
//Remove old unit
[_currentunit]spawn
{
private ["_currentunit"];
_currentunit = _this select 0;
while{(not(isnull _currentunit))}do
{
_currentunit setdammage 1;
sleep 0.01;
deleteVehicle _currentunit;
};
};
//Set Unit location back to original
player setdir _OrginalDir;
player setpos _OrginalPosition;
if (cameraOn == player) then
{
player switchCamera _OrginalView;
};
//RE-Add Current Loadout
removeAllWeapons player;
removeAllItems player;
//Ammo
if(count(_infoarray select 1) > 0)then
{
{
player addMagazine _x;
}foreach (_infoarray select 1);
};
//Weapons
_notallowed = [];
if(count(_infoarray select 0) > 0)then
{
{
if(_x in _notallowed)then
{
}else{
player addWeapon _x;
};
}foreach (_infoarray select 0);
reload player;
};
if ((primaryWeapon player) != "") then
{
[player, primaryWeapon player] call _fSelectWeapon;
};
sleep 0.5;
//Reinit unit
titleCut["", "BLACK in",0.5];
};