← Go back

Arma 3 - Vehicle persistence


Posted on May 22, 2018 at 20:30 (GMT +00:00) by Colin

Today I've put together a couple of functions that will help going forward with vehicle persistence.

The idea is you call a function to return a vehicle state, this includes the class, position, direction, fuel, mass, randomized variations, all damages, textures and current animation states. You can then save the array to a file or database using a unique ID every so often or before the server is closed.

When your mission or server is re-hosted, your initialization process can load up your table or files, call the new createVehicle function which will generate all of the vehicles exactly as they were in the same positions etc.

//
// PX_fnc_getVehicleState :: Get vehicles current state - Note: this does not save inventory
// Author: Colin J.D. Stewart
// Usage: _stateblock = _vehicle call PX_fnc_getVehicleState;
//
PX_fnc_getVehicleState = {
	[
		typeOf _this,
		getPosATL _this,
		getDir _this,
		fuel _this,
		getMass _this,
		(getAllHitPointsDamage _this) select 2,
		getObjectTextures _this,
		animationnames _this apply {_this animationPhase _x}			// thanks Grumpy Old Man here for help
	];
};


This function will return information which should be saved in a database or file (however you plan on saving/loading data (check out my sql plugin)

_stateblock = _vehicle call PX_fnc_getVehicleState;

// save _stateblock array here
(str _stateblock) call PX_fnc_saveToDatabase; // this is a placeholder, you must put your own saving function


Next, when it is time to reload vehicles, you will instead of using the usual createVehicle function, you will use the following by loading the exact stateblock you saved earlier.

//
// PX_fnc_createVehicle :: Creates a vehicle from state array block via PX_fnc_getVehicleState
// Author: Colin J.D. Stewart
// Usage: _stateblock call PX_fnc_createVehicle;
//
PX_fnc_createVehicle = {
	private _veh = createVehicle [_this select 0, _this select 1, [], 0, "FORM"];
	_veh enableSimulationGlobal true;
	_veh setVariable ["BIS_enableRandomization", false];
					
	_veh setDir (_this select 2);
	_veh setFuel (_this select 3);
	_veh setMass (_this select 4);
	
	private _textures = (_this select 6);					
	private _phaseData = (_this select 7);
	
	{ _veh setObjectTextureGlobal [_forEachIndex, _x]; } forEach _textures;
	{ _veh animate [_x, (_phaseData select _forEachIndex)]; } forEach (animationNames _veh);		
	{ _veh setHitIndex [_forEachIndex, _x, false]; } forEach (_this select 5);	
	
	_veh;
};


Calling is very straight forward

_stateblock = _somevar call PX_fnc_loadFromDatabase; // this is a placeholder and must be replaced with your loading function

_myveh = _stateblock call PX_fnc_createVehicle;

// do something with _myveh here


When I get more time I will put together a tutorial that links some of my various samples together, incorporating persistence loading and saving. Meanwhile, if you need help, just drop me a reply!
/Scripting/Programming/Game Modding

Search
Tags
Accident Adoption Albums Algorithms Altcoins Animal Rescue AnL Aquarium Arma ArmA nd Leg Array Partitioning Arrays Assembler Assembly Atanasovsko Award Bazaar Binary Search Bitcoin Bohemia Bootstrap Bulgaria Bulgaria Za Mladite Burgas C C++ Celerity Charity Chemical Shock Chemlight Code Competition Compiler Contest Converter Covid-19 CPP Crypto Cryptocurrency css Data Recovery Database DataTables Decode Dedicated Dedicated Server Delphi Development Dialogs Divide & Conquer DIY Earplugs Event Exchange Eyes Fish Floating-point Flood Fog Freeware Function Funny Gallery Game Game Development Game Modding Gaming Garden Gardening Gazebo GERB GitHub Glitch Hamish Harding Happy New Year Heapify HeapSort Helga HTML HTML entities Introduction Jackie JavaScript JQuery Lake Language Linker Listbox ListNBox lnbSetTooltip MariaDB Maths Merry Christmas Mini Mining Miningpoolhub Mist Modding MPH Multiplayer MySql NGO OceanGate One Nature ORCA organisation OverHertz Paludarium Pandemic Pascal Paul-Henri Nargeolet Paws Furever Pergola Persistence Pets Photography Pier Plugin Programming progress bar Project PX Library Quicksort r53 Racing Replace Scripting Searching Server Shahzada Dawood Simulation Simulator Smoke Snippet Social media Software Sorting Source Sourcecode SQF Statistics Stockton Rush String Strings Submarine Suleman Dawood Terrain Detail Text Titan Tool Tragedy tutorial UCS4Char UE4 Unreal Engine Victims View Distance ViewBug web Web Development Website Welcome Woodworking Ziron Zynk