diff --git a/addons/common/XEH_preInit.sqf b/addons/common/XEH_preInit.sqf index 94a0a490bf..faf0362779 100644 --- a/addons/common/XEH_preInit.sqf +++ b/addons/common/XEH_preInit.sqf @@ -119,6 +119,7 @@ PREP(setName); PREP(setParameter); PREP(setPitchBankYaw); PREP(setVariableJIP); +PREP(setVariablePublic); PREP(setSetting); PREP(setSettingFromConfig); PREP(stringToColoredText); diff --git a/addons/common/functions/fnc_setVariablePublic.sqf b/addons/common/functions/fnc_setVariablePublic.sqf new file mode 100644 index 0000000000..81093a2679 --- /dev/null +++ b/addons/common/functions/fnc_setVariablePublic.sqf @@ -0,0 +1,59 @@ +/* + * 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" + +private ["_object", "_varName", "_value", "_sync"]; + +_object = _this select 0; +_varName = _this select 1; +_value = _this select 2; +_sync = _this select 3; + +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]; + +// 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 {}; + +// when to push the value +private "_syncTime"; +_syncTime = diag_tickTime + _sync; + +// add eventhandler +[_idName, "onEachFrame", { + // wait to sync the variable + if (diag_tickTime > _this select 2) then { + // set value public + (_this select 0) setVariable [_this select 1, (_this select 0) getVariable (_this select 1), true]; + + // remove eventhandler + [_this select 3, "onEachFrame"] call BIS_fnc_removeStackedEventHandler + }; +}, [_object, _varName, _syncTime, _idName]] call BIS_fnc_addStackedEventHandler; +nil