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!