Skip to content

Commit

Permalink
fix addPFH and removePFH error in scheduled, fix #230
Browse files Browse the repository at this point in the history
  • Loading branch information
commy2 committed Jan 10, 2016
1 parent 18e898a commit 43ca690
Show file tree
Hide file tree
Showing 5 changed files with 56 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
52 changes: 28 additions & 24 deletions addons/common/fnc_addPerFrameHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,45 @@
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) = [];
GVAR(lastPFHid) = -1;
};
_publicHandle

GVAR(lastPFHid) = GVAR(lastPFHid) + 1;

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

private _handle = GVAR(PFHhandles) pushBack GVAR(lastPFHid);

GVAR(perFrameHandlerArray) pushBack [_function, _delay, 0, 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 > GVAR(lastPFHid)}) 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
23 changes: 6 additions & 17 deletions addons/common/init_perFrameHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,14 @@ 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)];

{
_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, diag_tickTime + _delay];
[_args, _handle] call _function;
false
};
} count GVAR(perFrameHandlerArray);
};

0 comments on commit 43ca690

Please sign in to comment.