diff --git a/addons/common/CfgFunctions.hpp b/addons/common/CfgFunctions.hpp index 0f4043466..9a39402a7 100644 --- a/addons/common/CfgFunctions.hpp +++ b/addons/common/CfgFunctions.hpp @@ -121,6 +121,8 @@ class CfgFunctions { class Misc { PATHTO_FNC(addPerFrameHandler); PATHTO_FNC(removePerFrameHandler); + PATHTO_FNC(updatePerFrameHandlerDelay); + PATHTO_FNC(getPerFrameHandlerDelay); PATHTO_FNC(createPerFrameHandlerObject); PATHTO_FNC(deletePerFrameHandlerObject); PATHTO_FNC(addPlayerAction); diff --git a/addons/common/fnc_getPerFrameHandlerDelay.sqf b/addons/common/fnc_getPerFrameHandlerDelay.sqf new file mode 100644 index 000000000..3e5cbaa16 --- /dev/null +++ b/addons/common/fnc_getPerFrameHandlerDelay.sqf @@ -0,0 +1,32 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_getPerFrameHandlerDelay + +Description: + Returns the current delay of an existing perFrameHandler. + +Parameters: + _handle - The existing perFrameHandler's handle. + +Returns: + _return current Delay of perFrameHandler. Will return -1 if failed. + +Examples: + (begin example) + _currentDelay = [_handle] call CBA_fnc_getPerFrameHandlerDelay; + (end) + +Author: + Mokka, OverlordZorn +---------------------------------------------------------------------------- */ + +params [["_handle", -1, [0]]]; + +[{ + params ["_handle"]; + + private _index = GVAR(PFHhandles) param [_handle]; + if (isNil "_index") exitWith {-1}; + GVAR(perFrameHandlerArray) select _index select 1 + +}, [_handle]] call CBA_fnc_directCall; \ No newline at end of file diff --git a/addons/common/fnc_updatePerFrameHandlerDelay.sqf b/addons/common/fnc_updatePerFrameHandlerDelay.sqf new file mode 100644 index 000000000..0bc756e88 --- /dev/null +++ b/addons/common/fnc_updatePerFrameHandlerDelay.sqf @@ -0,0 +1,46 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_updatePerFrameHandlerDelay + +Description: + Updates the delay of an existing perFrameHandler. + + If the new delay is shorter then the previous delay and the next iteration would have happend in the past, it will execute now and the following iteration will be executed based on current time + new delay. + +Parameters: + _handle - The existing perFrameHandler's handle. + _delay - The amount of time in seconds between executions, 0 for every frame. (optional, default: 0) + +Returns: + true if successful, false otherwise + +Examples: + (begin example) + _wasSuccessful = [_handle, _newDelay] call CBA_fnc_updatePerFrameHandlerDelay; + (end) + +Author: + Mokka, OverlordZorn +---------------------------------------------------------------------------- */ + +params [["_handle", -1, [0]], ["_newDelay", 0, [0]]]; + +[{ + params ["_handle", "_newDelay"]; + + private _index = GVAR(PFHhandles) param [_handle]; + if (isNil "_index") exitWith {false}; + private _entry = GVAR(perFrameHandlerArray) select _index; + private _prvDelay = _entry#1; + _entry set [1, _newDelay]; + + private _newDelta = _entry#2 - _prvDelay + _newDelay; + private _tickTime = diag_tickTime; + + // if the next iteration Time with the updated delay would have been in the past, next iteration will be set to "now" so the following iteration will respect the new delay between iterations + if (_newDelta < _tickTime) then { _newDelta = _tickTime; }; + _entry set [2, _newDelta]; + + true + +}, [_handle, _newDelay]] call CBA_fnc_directCall; \ No newline at end of file