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

add createPerFrameHandlerObject #441

Merged
merged 6 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class CfgFunctions {
class Misc {
F_FILEPATH(addPerFrameHandler);
F_FILEPATH(removePerFrameHandler);
F_FILEPATH(createPerFrameHandlerObject);
F_FILEPATH(deletePerFrameHandlerObject);
F_FILEPATH(addPlayerAction);
F_FILEPATH(removePlayerAction);
F_FILEPATH(createNamespace);
Expand Down
140 changes: 140 additions & 0 deletions addons/common/fnc_createPerFrameHandlerObject.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_createPerFrameHandlerObject

Description:
Creates a PFH object, that will execute code every frame, or every x number of seconds.

Parameters:
_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. (optional) <ANY>
_start - Function that is executed when the PFH is added. (optional) <CODE>
_end - Function that is executed when the PFH is removed. (optional) <CODE>
_runCondition - Condition that has to return true for the PFH to be executed. (optional, default {true}) <CODE>
_exitCondition - Condition that has to return true to delete the PFH object. (optional, default {false}) <CODE>
_private - List of local variables that are serialized between executions. (optional) <CODE>

Passed Arguments:
_this - The PFH logic. <LOCATION>

More variables are attached to this PFH logic than can be retrieved via 'getVariable'. (_this getVariable "params")
It is not advised to overwrite these variables with 'setVariable'!

"params" - Parameters passed by this function. Same as _args from above. <ANY>
"handle" - A number representing the handle of the PFH. <NUMBER>
"private" - List of local variables that are serialized between executions. Same as _private from above. <ARRAY>
"start" - Same as _start from above. <CODE>
"end" - Same as _end from above. <CODE>
"run" - Same as _function from above. <CODE>
"run_condition" - Same as _runCondition from above. <CODE>
"exit_condition" - Same as _exitCondition from above. <CODE>
"serialize" - Internal reserved variable.
"deserialize" - Internal reserved variable.

The PFH logic can be used to store additional custom variables.

Returns:
_logic - The PFH logic. <LOCATION>

Examples:
(begin example)
[
{ systemChat format ["frame! params: %1", _this getVariable "params"]; },
0,
["some_params", [1,2,3]],
{ systemChat format ["start! params: %1", _this getVariable "params"]; _test = 127; },
{ systemChat format ["end! params: %1", _this getVariable "params"]; systemChat str [_test] },
{ random 1 > 0.5 },
{ random 1 > 0.8 },
"_test"
] call CBA_fnc_createPerFrameHandlerObject;
(end)

Author:
Nou & Jaynus, donated from ACRE project code for use by the community; commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [
["_function", {}, [{}]],
["_delay", 0, [0]],
["_args", []],
["_start", {}, [{}]],
["_end", {}, [{}]],
["_runCondition", {true}, [{}]],
["_exitCondition", {false}, [{}]],
["_private", [], ["", []]]
];

if (_private isEqualType "") then {
_private = [_private];
};

private _logic = call CBA_fnc_createNamespace;

_logic setVariable ["start", _start];
_logic setVariable ["run_condition", _runCondition];
_logic setVariable ["exit_condition", _exitCondition];
_logic setVariable ["run", _function];
_logic setVariable ["end", _end];
_logic setVariable ["params", _args];
_logic setVariable ["private", _private];

// prepare serialization and deserialization code
private _serialize = [];

{
_serialize pushBack compile format ["_logic setVariable ['%1', %1]", _x];
} forEach (_logic getVariable "private");

private _deserialize = [];

{
_deserialize pushBack compile format ["%1 = _logic getVariable '%1'", _x];
} forEach (_logic getVariable "private");

_logic setVariable ["serialize", _serialize];
_logic setVariable ["deserialize", _deserialize];

// add per frame handler
private _handle = [{
// all functions get _logic as _this param. Params inside: _logic getVariable "params";
params ["_logic"];

if (isNil "_logic" || {isNull _logic}) exitWith {
(_logic getVariable "handle") call CBA_fnc_removePerFrameHandler;
};

// deserialize
private (_logic getVariable "private");
{ call _x } forEach (_logic getVariable "deserialize");

// check exit condition - exit if false
Copy link
Contributor

Choose a reason for hiding this comment

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

exit if true

if (_logic call (_logic getVariable "exit_condition")) exitWith {
// execute end code
_logic call (_logic getVariable "end");

(_logic getVariable "handle") call CBA_fnc_removePerFrameHandler;
_logic call CBA_fnc_deleteNamespace;
};

// check Run Condition - only continue if true
if (_logic call (_logic getVariable "run_condition")) then {
// execute code
_logic call (_logic getVariable "run");

// serialize
{ call _x } forEach (_logic getVariable "serialize");
};
}, _delay, _logic] call CBA_fnc_addPerFrameHandler;

_logic setVariable ["handle", _handle];

// run start code
private (_logic getVariable "private");
_logic call (_logic getVariable "start");

// serialize
{ call _x } forEach (_logic getVariable "serialize");

_logic // returns logic because you can get the handle from it, and much more
30 changes: 30 additions & 0 deletions addons/common/fnc_deletePerFrameHandlerObject.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_deletePerFrameHandlerObject

Description:
Deletes a PFH object that was previously created via CBA_fnc_createPerFrameHandlerObject

Parameters:
_logic - The PFH object <LOCATION>

Returns:
None

Examples:
(begin example)
_pfhLogic call CBA_fnc_deletePerFrameHandlerObject;
(end)

Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_logic", locationNull, [locationNull]]];

if (!isNull _logic) then {
_logic call (_logic getVariable "end");
Copy link
Contributor

Choose a reason for hiding this comment

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

How about changing the handler's run and exit conditions to make it "end itself"?


(_logic getVariable "handle") call CBA_fnc_removePerFrameHandler;
_logic call CBA_fnc_deleteNamespace;
};