2015-02-12 12:23:38 +00:00
/*
* Author: commy2
*
* Sets a public variable, but wait a certain amount of time to transfer the value over the network. Changing the value by calling this function again resets the windup timer.
*
* Argument:
* 0: Object the variable should be assigned to (Object)
* 1: Name of the variable (String)
* 2: Value of the variable (Any)
* 3: Windup time (Number, optional. Default: 1)
*
* Return value:
* Nothing.
*/
#include "script_component.hpp"
2015-05-14 18:06:06 +00:00
PARAMS_4(_object,_varName,_value,_sync);
2015-02-12 12:23:38 +00:00
if (isNil "_sync") then {
_sync = 1;
};
// set value locally
_object setVariable [_varName, _value];
// "duh"
if (!isMultiplayer) exitWith {};
// generate stacked eventhandler id
private "_idName";
_idName = format ["ACE_setVariablePublic_%1", _varName];
2015-02-12 14:56:57 +00:00
// exit now if an eh for that variable already exists
private "_allIdNames";
_allIdNames = [GETMVAR(BIS_stackedEventHandlers_onEachFrame,[]), {_this select 0}] call FUNC(map);
if (_idName in _allIdNames) exitWith {};
2015-02-12 12:23:38 +00:00
// when to push the value
private "_syncTime";
_syncTime = diag_tickTime + _sync;
2015-02-12 14:56:57 +00:00
// add eventhandler
2015-02-12 12:23:38 +00:00
[_idName, "onEachFrame", {
// wait to sync the variable
2015-02-12 14:56:57 +00:00
if (diag_tickTime > _this select 2) then {
2015-02-12 12:23:38 +00:00
// set value public
2015-02-12 14:56:57 +00:00
(_this select 0) setVariable [_this select 1, (_this select 0) getVariable (_this select 1), true];
2015-02-12 12:23:38 +00:00
// remove eventhandler
2015-02-12 14:56:57 +00:00
[_this select 3, "onEachFrame"] call BIS_fnc_removeStackedEventHandler
2015-02-12 12:23:38 +00:00
};
2015-02-12 14:56:57 +00:00
}, [_object, _varName, _syncTime, _idName]] call BIS_fnc_addStackedEventHandler;
2015-02-12 12:23:38 +00:00
nil