Jump to content
xBowBii

[Snippet] profileNamespace vehicleSaving

Recommended Posts

Hey guys,

 

This is just a snippet of code I've written some months ago, I tested it and it seemed to work perfectly but you might need to tweak some lines for it to work, I can't remember..

 

The code uses profileNamespace to write to a .var file some information, in this case all vehicles including its position, damaged (and non-damaged) hitpoints, fuel, cargo, ... I even used it to save placed objects (player-built, missions, ...) for my own survival mod (WIP).

 

 

loadVehicles.sqf

/*
	File: 		loadVehicles.sqf
	Author: 	xBowBii (aka xbwb)

	08/03/15
*/

_vehicles = profileNamespace getVariable ["SURV_Vehicles",nil];

{

	private ["_log","_vtype","_vpos","_vdir","_vdam","_veh","_vFuel","_vWeap","_vMags"];

	_vType = _x select 0;
	_vPos  = _x select 1;
	_vDir  = _x select 2;
	_vDam  = _x select 3;
	_vVar1 = _x select 4;
	_vFuel = _x select 5;
	_vWeap = _x select 6;
	_vMags = _x select 7;
	_hitp  = _x select 8;

	//"extLOG" callExtension format["0:VehicleLogging: %1 at %2, with dmg %3. Fuel is %4, and Cargo is %5 && %6",_vType,_vPos,_vDam,_vFuel,_vWeap,_vMags];

	if (_vDam == 1) exitWith {/* useless to spawn it */};

	_veh = createVehicle [_vType, _vPos, [], 0, "CAN_COLLIDE"];
	_veh setDir _vDir;
	_veh setDamage _vDam;

	removeAllWeapons _veh;
	removeAllItems _veh;

	_veh setVariable ["YOUR_OWN_CUSTOMVAR",_vVar1,true];

	{
		_veh setHitPointDamage [(_x select 0), (_x select 1)];
	} forEach _hitp;

	_veh setFuel _vFuel;

	{
		_veh addWeaponCargoGlobal [_x,1];
	} forEach _vWeap;
	
	{
		_veh addMagazineCargoGlobal [_x,1];
	} forEach _vMags;

} forEach _vehicles;

sleep 20;
SURV_VehiclesLoaded = true; 

saveVehicles.sqf

/*
	File: 		saveVehicles.sqf
	Author: 	xBowBii (aka xbwb)
	
	08/03/15
*/
SURV_VehiclesLoaded = false;
waitUntil {SURV_VehiclesLoaded};

while {true} do
{
	profileNamespace setVariable ["SURV_Vehicles",[]];

	{
	
		_Vehs  = profileNamespace getVariable ["SURV_Vehicles",[]];
		_vehicle = _x;

		_xType = typeOf _x;
		_xPos  = getPos _x;
		_xDir  = getDir _x;
		_xDmg  = damage _x;
		_xOwns = _x getVariable ["YOUR_OWN_CUSTOMVAR",[]];
		_xFuel = fuel _x;
		_weapons = weaponCargo _x;
		_magazines = magazineCargo _x;

		_hitPoints = [];
		_cachekey = format["%1_HP", _xType];
		_hitpoints = missionNamespace getVariable[_cachekey, []];
		if (_hitpoints isEqualTo []) then
		{
			_na = configProperties[configFile >> "CfgVehicles" >> _xType >> "HitPoints", "_hitpoints pushBack configName _x; true", true];
			missionNamespace setVariable[_cachekey, _hitpoints];
		};

		_finalHitPs = [];
		{
			_finalHitPs = _finalHitPs + [[_x,_vehicle getHitPointDamage _x]];
		} forEach _hitPoints;

		profileNamespace setVariable ["SURV_Vehicles",(_vehs + [[_xtype,_xpos,_xdir,_xdmg,_xowns,_xFuel,_weapons,_magazines,_finalHitPs]])];

	} forEach vehicles;
	saveProfileNamespace;

	sleep 20;
};

Both scripts executed by this:

if (isServer) then
{
	execVM "\PATH\TO\loadVehicles.sqf";
	execVM "\PATH\TO\saveVehicles.sqf";
};

If you want to use it, please do so, just remember crediting my work even though I won't use it myself and I had a lot of fun to write this tiny script.

 

I hope you appreciate my work, please post suggestions if you have any!

xbwb

Edited by xBowBii
simple snippet, no need to credit me.

Share this post


Link to post
Share on other sites

Getting this error...

 

 

Share this post


Link to post
Share on other sites
16 hours ago, TomMack said:

Getting this error...

 

 

Try replacing the "Nil" default value of _vehicles to "[]", or try executing saveVehicles first

 

Remove my multiplayer checks (SURV_VehiclesLoaded) bit of code, it might bug things

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, xBowBii said:

Try replacing the "Nil" default value of _vehicles to "[]", or try executing saveVehicles first

 

Remove my multiplayer checks (SURV_VehiclesLoaded) bit of code, it might bug things

 

This worked perfectly, thank you.

Share this post


Link to post
Share on other sites

As I mentioned in a different thread - here is a modified version of the script to handle ammo boxes.

 

This modified version saves specific ammoboxes, in an area which can be defined with a radius, to the profile namespace. This makes it very useful for persistent missions. This version remove compatibility with multiplayer and Dedicated servers however.

 

saveAmmobox.sqf : https://pastebin.com/7Z5M37hp

loadAmmobox.sqf : https://pastebin.com/UBDjKLGq

 

As mentioned in the comments of the scripts; do add different types of ammobox, you cant go to this link (https://community.bistudio.com/wiki/Arma_3_CfgVehicles_Equipment) and "find" (ctrl+f "Ammo") the different ammobox classnames, alternatively, you can right click the ammobox in 3DEN editor and Log > Classnames.

 

Thanks so much to XBWB for the basis of the script and helping me to produce this version.

 

P.s. My additions to the script have been commented.

 

  • Like 1

Share this post


Link to post
Share on other sites

How do i get it to save the owner as another script doesn't let me lock/unlock on restart.

Very new, all help appreciated.

Also looking for one to save unit pos, loadout, direction and so on. 😄

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

×