Skip to content

Instantly share code, notes, and snippets.

@GeorgeDettmer
Last active December 24, 2018 01:32
Show Gist options
  • Select an option

  • Save GeorgeDettmer/4a92eafb71be1ca14eb7 to your computer and use it in GitHub Desktop.

Select an option

Save GeorgeDettmer/4a92eafb71be1ca14eb7 to your computer and use it in GitHub Desktop.
Example of position persistence using profileNamespace on server
if isServer then { //only run on server
addMissionEventHandler ["HandleDisconnect",{ //add an event to save data on player disconnect
_uids = profileNamespace getVariable ["#MOSES#uids",[]]; //get all uids for players with saved positions
_idx = _uids find _uids; //get the "index" of the uid in the array
if (_idx == -1) then { //if uid not in list
_idx = _uids pushBack _uids; //add it and store new index
profileNamespace setVariable ["#MOSES#uids",_uids]; //set uids again, a new one was added
};
_positions = profileNamespace getVariable ["#MOSES#lastPositions",[]]; //get saved positions
_directions = profileNamespace getVariable ["#MOSES#lastDirections",[]]; //get saved directions
_positions set [_idx,getPosATL _unit]; //add new positon at index
_directions set [_idx,direction _unit]; //add new direction at index
profileNamespace setVariable ["#MOSES#lastPositions",_positions]; //set new data in profilenamsepace
profileNamespace setVariable ["#MOSES#lastDirections",_directions]; //set new data in profilenamsepace
saveProfileNamespace; //save
false
}];
"#MOSES#requestPosition" addPublicVariableEventHandler { //add event to run when requestPosition variable is publicVariable'd from client
_this = _this select 1;
_unit = _this select 0; //get unit
_died = _this select 1; //determine if its a player died type request
_uids = profileNamespace getVariable ["#MOSES#uids",[]]; //get uids, like with disconnect handler
_idx = _uids find _uids;
if (_idx == -1) exitWith {}; //if uid does not exist, do nothing. Theres not data for them
_position = profileNamespace getVariable ["#MOSES#lastPositions",[]]; //get positions
_direction = profileNamespace getVariable ["#MOSES#lastDirections",[]] //get directions
if _died exitWith { //if they died... EXIT WITH
_position set [_idx,nil]; //remove postions
_direction set [_idx,nil]; //remove direction
saveProfileNamespace; //save
};
_position = _position select _idx; //get position for uid
_direction = _direction select _idx; //get direction for uid
if (isNil "_position" || isNil "_direction") exitWith {}; //if either dont exits, exit...
_unit setPosATL _position; //set position
_unit setDir _direction; //set direction
};
};
if !hasInterface exitWith {};
player addEventHandler ["Killed",{ //add killed event handler
missionNamespace setVariable ["#MOSES#requestPosition",[player,false]]; //send the request, false means the died
}];
missionNamespace setVariable ["#MOSES#requestPosition",[player,true]]; //request postion be set by server, if it exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment