Skip to content

Commit

Permalink
Merge pull request CBATeam#236 from CBATeam/fixPFHscheduled
Browse files Browse the repository at this point in the history
Fix issues with PerFrameHandlers
  • Loading branch information
Killswitch00 committed Jan 12, 2016
2 parents 8c0ae29 + 2deccce commit a39aeab
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 75 deletions.
4 changes: 2 additions & 2 deletions addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CfgFunctions
// CBA_fnc_addPerFrameHandler
class addPerFrameHandler
{
description = "Add a handler that will execute every frame, or every x number of seconds";
description = "Add a handler that will execute every frame, or every x number of seconds.";
file = "\x\cba\addons\common\fnc_addPerFrameHandler.sqf";
};
// CBA_fnc_addPlayerAction
Expand Down Expand Up @@ -438,7 +438,7 @@ class CfgFunctions
// CBA_fnc_removePerFrameHandler
class removePerFrameHandler
{
description = "Remove a handler that you have added using CBA_fnc_addPerFrameHandler";
description = "Remove a handler that you have added using CBA_fnc_addPerFrameHandler.";
file = "\x\cba\addons\common\fnc_removePerFrameHandler.sqf";
};
// CBA_fnc_removePlayerAction
Expand Down
2 changes: 0 additions & 2 deletions addons/common/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ LOG(MSG_INIT);
ADDON = false;

CBA_nil = [nil];
GVAR(PFHhandles) = [];
GVAR(nextPFHid) = -1;
GVAR(centers) = [];
CBA_actionHelper = QUOTE(PATHTO(actionHelper));
GVAR(delayless) = QUOTE(PATHTOF(delayless.fsm));
Expand Down
49 changes: 25 additions & 24 deletions addons/common/fnc_addPerFrameHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,42 @@
Function: CBA_fnc_addPerFrameHandler
Description:
Add a handler that will execute every frame, or every x number of seconds
Add a handler that will execute every frame, or every x number of seconds.
Parameters:
_func - The function you wish to execute
_delay - The amount of time in seconds (can be less than 0) between executions, 0 for everyframe.
_params - Parameters passed to the function executing. This will be the same array every execution.
_function - The function you wish to execute. <CODE>
_delay - The amount of time in seconds between executions, 0 for every frame. [optional] (default: 0) <NUMBER>
_args - Parameters passed to the function executing. This will be the same array every execution. [optional] <ANY>
Returns:
_handle - a number representing the handle of the function. Use this to remove the handler.
_handle - a number representing the handle of the function. Use this to remove the handler. <NUMBER>
Examples:
(begin example)
[{player sideChat format["every frame! _this: %1", _this];}, 0, ["some","params",1,2,3]] call CBA_fnc_addPerFrameHandler;
_handle = [{player sideChat format ["every frame! _this: %1", _this];}, 0, ["some","params",1,2,3]] call CBA_fnc_addPerFrameHandler;
(end)
Author:
Nou & Jaynus, donated from ACRE project code for use by the community.
Nou & Jaynus, donated from ACRE project code for use by the community; commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"

private ["_handle", "_data", "_publicHandle"];
params ["_func","_delay", ["_params",[]]];

if (!isNil "_func") then {
_handle = if (GVAR(nextPFHid) == -1) then {
GVAR(nextPFHid) = count GVAR(perFrameHandlerArray);
GVAR(nextPFHid)
} else {
GVAR(nextPFHid) = GVAR(nextPFHid) + 1;
GVAR(nextPFHid)
};

_publicHandle = GVAR(PFHhandles) pushback _handle;
_data = [_func, _delay, 0, diag_tickTime, _params, _publicHandle];
GVAR(perFrameHandlerArray) pushBack _data;
params [["_function", {}, [{}]], ["_delay", 0, [0]], ["_args", []]];

if (_function isEqualTo {}) exitWith {-1};

if (isNil QGVAR(PFHhandles)) then {
GVAR(PFHhandles) = [];
};

if (count GVAR(PFHhandles) >= 999999) exitWith {
WARNING("Maximum amount of per frame handlers reached!");
diag_log _function;
-1
};
_publicHandle

private _handle = GVAR(PFHhandles) pushBack count GVAR(perFrameHandlerArray);

GVAR(perFrameHandlerArray) pushBack [_function, _delay, diag_tickTime, diag_tickTime, _args, _handle];

_handle
50 changes: 20 additions & 30 deletions addons/common/fnc_removePerFrameHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Function: CBA_fnc_removePerFrameHandler
Description:
Remove a handler that you have added using CBA_fnc_addPerFrameHandler
Remove a handler that you have added using CBA_fnc_addPerFrameHandler.
Parameters:
_handle - The function handle you wish to remove
_handle - The function handle you wish to remove. <NUMBER>
Returns:
None
Expand All @@ -18,34 +18,24 @@ Examples:
(end)
Author:
Nou & Jaynus, donated from ACRE project code for use by the community.
Nou & Jaynus, donated from ACRE project code for use by the community; commy2
---------------------------------------------------------------------------- */

#include "script_component.hpp"

params ["_publicHandle"];

if (isNil "_publicHandle" || {_publicHandle < 0} || {(count GVAR(PFHhandles)) <= _publicHandle}) exitWith {// Nil/no handle or handle is out of bounds of Public Handle Array
WARNING("Invalid or not existing PFH ID.");
};

private "_handle";
_handle = GVAR(PFHhandles) select _publicHandle;
if (isNil "_handle") exitWith {}; // Nil handle, nil action
GVAR(PFHhandles) set [_publicHandle, nil];
GVAR(perFrameHandlerArray) set [_handle, nil];
_newArray = [];

GVAR(nextPFHid) = ({
private ["_newHandle", "_return"];
_return = false;
if !(isNil "_x") then {
_x params ["", "", "", "", "", "_publicH"];
_newHandle = _newArray pushBack _x;
GVAR(PFHhandles) set [_publicH, _newHandle];
_return = true;
};
_return
} count GVAR(perFrameHandlerArray)) - 1;
GVAR(perFrameHandlerArray) = _newArray;
params [["_handle", -1, [0]]];

if (_handle < 0 || {_handle >= count GVAR(PFHhandles)}) exitWith {};

[{
params ["_handle"];

GVAR(perFrameHandlerArray) deleteAt (GVAR(PFHhandles) select _handle);
GVAR(PFHhandles) set [_handle, nil];

{
_x params ["", "", "", "", "", "_handle"];
GVAR(PFHhandles) set [_handle, _forEachIndex];
} forEach GVAR(perFrameHandlerArray);
}, _handle] call CBA_fnc_directCall;

nil
32 changes: 15 additions & 17 deletions addons/common/init_perFrameHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GVAR(perFrameHandlerArray) = [];
GVAR(fpsCount) = 0;
GVAR(lastCount) = -1;
GVAR(lastFrameRender) = 0;
GVAR(lastTickTime) = 0;

PREP(perFrameEngine);

Expand Down Expand Up @@ -128,25 +129,22 @@ FUNC(monitorFrameRender) = {
FUNC(onFrame) = {
TRACE_1("Executing onFrame",nil);
GVAR(lastFrameRender) = diag_frameNo;
// if(GVAR(lastCount) > (GVAR(fpsCount)-1)) then {
// hint "FUCK UP IN SEQUENCE!";
// };
// player sideChat format["fps: %1 %2 %3", (GVAR(fpsCount)/diag_fps), diag_fps, GVAR(fpsCount)];
// GVAR(lastCount) = GVAR(fpsCount);
// GVAR(fpsCount) = GVAR(fpsCount) + 1;
// player sideChat format["c: %1", GVAR(perFrameHandlerArray)];
GVAR(lastTickTime) = diag_tickTime;

{
_x params ["_function", "_delay", "_delta", "", "_args", "_handle"];

if !(isNil "_x") then {
_handlerData = _x;
if (_handlerData params ["_func", "_delay", "_delta", "", "_args", "_idPFH"]) then {
if (diag_tickTime > _delta) then {
[_args, _idPFH] call _func;
_delta = diag_tickTime + _delay;
//TRACE_1("data", _data);
_handlerData set [2, _delta];
};
};
if (diag_tickTime > _delta) then {
_x set [2, _delta + _delay];
[_args, _handle] call _function;
false
};
} count GVAR(perFrameHandlerArray);
};

// fix for save games. subtract last tickTime from ETA of all PFHs after mission was loaded
addMissionEventHandler ["Loaded", {
{
_x set [2, (_x select 2) - GVAR(lastTickTime) + diag_tickTime];
} forEach GVAR(perFrameHandlerArray);
}];

0 comments on commit a39aeab

Please sign in to comment.