-
Notifications
You must be signed in to change notification settings - Fork 148
/
fnc_addState.sqf
68 lines (57 loc) · 2.44 KB
/
fnc_addState.sqf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_statemachine_fnc_addState
Description:
Adds a state to a state machine.
Parameters:
_stateMachine - a state machine <LOCATION>
_onState - code that is executed when state is active (frequency
depends on amount of objects active in state machine)
<CODE>
(Default: {})
_onStateEntered - code that is executed once when state was entered, after
onTransition (also once for the intial state) <CODE>
(Default: {})
_onStateLeaving - code that is executed once when exiting state, before
onTransition <CODE>
(Default: {})
_name - unique state name <STRING>
(Default: "stateX" with X being a unique number)
Returns:
_name - unique state name or empty string on error <STRING>
Examples:
(begin example)
_name = [_stateMachine, {}] call CBA_statemachine_fnc_addState;
(end)
Author:
BaerMitUmlaut
---------------------------------------------------------------------------- */
SCRIPT(addState);
params [
["_stateMachine", locationNull, [locationNull]],
["_onState", {}, [{}]],
["_onStateEntered", {}, [{}]],
["_onStateLeaving", {}, [{}]],
["_name", "", [""]]
];
private _states = _stateMachine getVariable QGVAR(states);
if (isNull _stateMachine || {_name in _states}) exitWith {""};
// Autogenerate unique name
if (_name == "") then {
private _nextUniqueID = _stateMachine getVariable QGVAR(nextUniqueStateID);
_name = "state" + str _nextUniqueID;
_stateMachine setVariable [QGVAR(nextUniqueStateID), _nextUniqueID + 1];
};
_states pushBack _name;
_stateMachine setVariable [QGVAR(states), _states];
_stateMachine setVariable [ONSTATE(_name), _onState];
_stateMachine setVariable [ONSTATEENTERED(_name), _onStateEntered];
_stateMachine setVariable [ONSTATELEAVING(_name), _onStateLeaving];
_stateMachine setVariable [TRANSITIONS(_name), []];
_stateMachine setVariable [EVENTTRANSITIONS(_name), []];
// First state added is always the intial state
if (isNil {_stateMachine getVariable QGVAR(initialState)}) then {
_stateMachine setVariable [QGVAR(initialState), _name];
GVAR(stateMachines) pushBack _stateMachine; // add it to the list now that it has an initial state
};
_name