Yet another post involving triggers. I have already tried to make timers using different unorthodox methods in my past attempts. This time I think I have a slightly improved version of a callback timer. Based on trigger countdown, this is fairly straight forward implementation. One peculiar thing about it is that the trigger never executes OnActivation statement, only OnDeactivation. Was a pretty unexpected finding as well. It seems that if the timeout is set to interruptible and the trigger is reset before it has the chance to execute OnActivation, the trigger considers that it has been deactivated. Here is the code:

KK_fnc_setTimerCallback = { if !isServer exitWith {objNull}; private ["_f","_t","_tr"]; _f = _this select 0; _t = _this select 1; _tr = createTrigger ["EmptyDetector", [0,0,0]]; _tr setTriggerTimeout [_t, _t, _t, true]; _tr setTriggerStatements [ "!triggerActivated thisTrigger", "", "0 = thisTrigger call " + _f ]; if (_this select 2) then { 0 = _tr call (missionNamespace getVariable _f); }; _tr };

A few things. You don’t want to create triggers on clients in Multiplayer, this is why I restricted it to server only. So this function is to be used on server only or in single player, sorry about that. OnDeactivation statement, from which your function is called will be executed in unscheduled environment, so if you have lots of things happening in it, consider rather spawning and managing scheduled environment scopes from it.

Syntax

timer = [function, interval, force] call KK_fnc_setTimerCallback;

Params

  • function: STRING – name of the function to be executed upon timeout. The trigger object is passed to the function in _this variable in case you would like to remove the trigger and stop further executions.
  • interval: NUMBER – desired interval in seconds. 0.5 seems to be the minimum interval possible.
  • force: BOOLEAN – will also execute the function immediately upon the timer setup.

Return

  • timer: OBJECT – trigger object.

Example

//execute my_fnc about every 5 sec 10 times in a row i = 1; my_fnc = { hint str time; i = i + 1; if (i > 10) then { deleteVehicle _this; }; }; _timer = ["my_fnc", 4.5, true] call KK_fnc_setTimerCallback;

Because of trigger default refresh rate, the actual interval could be on average 0.5 seconds longer than passed interval. If you need more precise timer, you can construct something in FSM.

Enjoy,
KK