From 2f4f6f3161b664ed1570f3092c6c39ee29a20daf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Badano?= Date: Sat, 17 Jan 2015 02:58:24 -0300 Subject: [PATCH] common: add waitAndExecute function --- addons/common/XEH_preInit.sqf | 1 + .../common/functions/fnc_waitAndExecute.sqf | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 addons/common/functions/fnc_waitAndExecute.sqf diff --git a/addons/common/XEH_preInit.sqf b/addons/common/XEH_preInit.sqf index e15792b0d8..366b959251 100644 --- a/addons/common/XEH_preInit.sqf +++ b/addons/common/XEH_preInit.sqf @@ -119,6 +119,7 @@ PREP(toBitmask); PREP(toHex); PREP(toNumber); PREP(unmuteUnit); +PREP(waitAndExecute); // ACE_Debug PREP(exportConfig); diff --git a/addons/common/functions/fnc_waitAndExecute.sqf b/addons/common/functions/fnc_waitAndExecute.sqf new file mode 100644 index 0000000000..c57073f004 --- /dev/null +++ b/addons/common/functions/fnc_waitAndExecute.sqf @@ -0,0 +1,41 @@ +/* + * Author: CAA-Picard + * + * Executes a code once with a given game time delay, using a PFH + * + * Argument: + * 0: Code to execute (Code or String) + * 1: Parameters to run the code with (Array) + * 2: Delay in seconds before executing the code (Number) + * 3: Interval of time in which the execution is evaluated, 0 means every frame (Number) + * + * Return value: + * PFH handler ID + */ +#include "script_component.hpp" + +EXPLODE_4_PVT(_this,_func,_params,_delay,_interval); + +[ + { + EXPLODE_2_PVT(_this,_params,_phfId); + EXPLODE_2_PVT(_params,_delayedExecParams,_startTime); + EXPLODE_3_PVT(_delayedExecParams,_func,_funcParams,_delay); + + // Exit if the time was not reached yet + if (time < _startTime + _delay) exitWith {}; + + // Remove the PFH + [_pfhId] call cba_fnc_removePerFrameHandler; + + // Compile the function if necesary + if (typeName _func == "STRING") then { + _func = compile _func; + }; + + // Execute the function + _funcParams call _func; + }, + _interval, + [_this, time] +] call CBA_fnc_addPerFrameHandler \ No newline at end of file