Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with PerFrameHandlers #236

Merged
merged 4 commits into from
Jan 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This forEach loop needs to be above line 32 or it will fail on 2nd call to :

x_A = [{      diag_log text format ["---%1 - A Running %2", diag_tickTime, (_this select 1)];  }, 5, []] call CBA_fnc_addPerFrameHandler;
x_B = [{      diag_log text format ["---%1 - B Running %2", diag_tickTime, (_this select 1)];  }, 5, []] call CBA_fnc_addPerFrameHandler;
[x_A] call CBA_fnc_removePerFrameHandler;
[x_B] call CBA_fnc_removePerFrameHandler;  

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All we really need is to find index of the top level _handle in the perFrameHandlerArray array, so, we could add an exit to the loop and skip the set I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a find would be slow when GVAR(PFHhandles) get's big. With this method you only have to iterate through the PFHs that are still active. No idea which one is better. This is what already was there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp. The problem is that the public handle is pushed to GVAR(PFHhandles) (which is for the internal handles, those who can change).
Thats why removing the PFH twice "works". The loop auto corrects the internal handle...

}, _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);
}];