Per Frame Handlers - CBATeam/CBA_A3 GitHub Wiki

Examples

This will run a PFH only for as long as the condition is fulfilled. It will run the provided code upon exiting once the condition reports false and remove the PFH. Can be used to replace waitUntil loops in unscheduled environment.

_codeToRun  - <CODE> code to Run stated between {}
_parameters - <ANY> OPTIONAL parameters, will be passed to  code to run, exit code and condition
_exitCode   - <CODE> OPTIONAL exit code between {} code that will be executed upon ending PFEH default is {}
_condition  - <CODE THAT RETURNS BOOLEAN> - OPTIONAL conditions during which PFEH will run default {true}
_delay      - <NUMBER> (optional) delay between each execution in seconds, PFEH executes at most once per frame
private _codeToRun = {hint str _this};
private _parameters = player;
private _exitCode = {hint format ["Player %1 has died", name _this]};
private _condition = {alive _this};
private _delay = 2;

[{
    params ["_args", "_handle"];
    _args params ["_codeToRun", "_parameters", "_exitCode", "_condition"];

    if (_parameters call _condition) then {
        _parameters call _codeToRun;
    } else {
        _handle call CBA_fnc_removePerFrameHandler;
        _parameters call _exitCode;
    };
}, _delay, [_codeToRun, _parameters, _exitCode, _condition]] call CBA_fnc_addPerFrameHandler;

Local variables can be appened to the arguments array and are thus available in the next execution of the PFH.

[{
    params ["_args", "_handle"];
    _args params ["_vehicle", "_distanceTraveled", "_lastPosition"];

    private _currentPosition = getPosASL _vehicle;
    _distanceTraveled = _distanceTraveled + vectorMagnitude (_currentPosition vectorDiff _lastPosition);
    _args set [1, _distanceTraveled];
    _args set [2, _currentPosition];

    hintSilent format ["%1 moved %2 meters", name _vehicle, floor _distanceTraveled];
}, 1, [cameraOn, 0, getPosASL cameraOn]] call CBA_fnc_addPerFrameHandler;
⚠️ **GitHub.com Fallback** ⚠️