From 43ca690411660c41acbe00fd9c433bfa5ce3c6e4 Mon Sep 17 00:00:00 2001 From: commy2 Date: Mon, 11 Jan 2016 00:45:37 +0100 Subject: [PATCH] fix addPFH and removePFH error in scheduled, fix #230 --- addons/common/CfgFunctions.hpp | 4 +- addons/common/XEH_preInit.sqf | 2 - addons/common/fnc_addPerFrameHandler.sqf | 52 +++++++++++---------- addons/common/fnc_removePerFrameHandler.sqf | 50 ++++++++------------ addons/common/init_perFrameHandler.sqf | 23 +++------ 5 files changed, 56 insertions(+), 75 deletions(-) diff --git a/addons/common/CfgFunctions.hpp b/addons/common/CfgFunctions.hpp index c21d4a223..faa3588b3 100644 --- a/addons/common/CfgFunctions.hpp +++ b/addons/common/CfgFunctions.hpp @@ -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 @@ -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 diff --git a/addons/common/XEH_preInit.sqf b/addons/common/XEH_preInit.sqf index ebcdd02f7..63fd3d057 100644 --- a/addons/common/XEH_preInit.sqf +++ b/addons/common/XEH_preInit.sqf @@ -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)); diff --git a/addons/common/fnc_addPerFrameHandler.sqf b/addons/common/fnc_addPerFrameHandler.sqf index 6b641c109..cb789915c 100644 --- a/addons/common/fnc_addPerFrameHandler.sqf +++ b/addons/common/fnc_addPerFrameHandler.sqf @@ -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. + _delay - The amount of time in seconds between executions, 0 for every frame. [optional] (default: 0) + _args - Parameters passed to the function executing. This will be the same array every execution. [optional] 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. 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 diff --git a/addons/common/fnc_removePerFrameHandler.sqf b/addons/common/fnc_removePerFrameHandler.sqf index ec662f4bc..7c34a100d 100644 --- a/addons/common/fnc_removePerFrameHandler.sqf +++ b/addons/common/fnc_removePerFrameHandler.sqf @@ -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. Returns: None @@ -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 diff --git a/addons/common/init_perFrameHandler.sqf b/addons/common/init_perFrameHandler.sqf index 8dd2a0563..7baca2610 100644 --- a/addons/common/init_perFrameHandler.sqf +++ b/addons/common/init_perFrameHandler.sqf @@ -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); };