Skip to content

Commit b63898f

Browse files
committed
Added onStateEntered and onStateLeaving functions
1 parent aae332c commit b63898f

6 files changed

+40
-5
lines changed

addons/statemachine/example.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class MyAddon_Statemachine {
1313
// States are just subclasses of the state machine
1414
class Initial {
1515
onState = "";
16+
onStateEntered = "";
17+
onStateLeaving = "";
1618

1719
// Transitions are also just subclasses of states
1820
class InCombat {

addons/statemachine/example.sqf

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
private _stateMachine = [{allGroups select {!isPlayer leader _x}}] call CBA_statemachine_fnc_create;
1414

15-
[_stateMachine, {}, "Initial"] call CBA_statemachine_fnc_addState;
16-
[_stateMachine, {}, "Alert"] call CBA_statemachine_fnc_addState;
15+
[_stateMachine, {}, {}, {}, "Initial"] call CBA_statemachine_fnc_addState;
16+
[_stateMachine, {}, {}, {}, "Alert"] call CBA_statemachine_fnc_addState;
1717

1818
[_stateMachine, "Initial", "Alert", {combatMode _this == "YELLOW"}, {
1919
// Set skill once on transition
20+
// This could also be done in the onStateEntered function
2021
{
2122
_x setSkill ["spotDistance", ((_x skill "spotDistance") * 1.5) min 1];
2223
_x setSkill ["spotTime", ((_x skill "spotTime") * 1.5) min 1];

addons/statemachine/fnc_addState.sqf

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ Parameters:
99
_onState - code that is executed when state is active (frequency
1010
depends on amount of objects active in state machine)
1111
<CODE>
12+
(Default: {})
13+
_onStateEntered - code that is executed once when state was entered, after
14+
onTransition (also once for the intial state) <CODE>
15+
(Default: {})
16+
_onStateLeaving - code that is executed once when exiting state, before
17+
onTransition <CODE>
18+
(Default: {})
1219
_name - unique state name <STRING>
1320
(Default: "stateX" with X being a unique number)
1421
@@ -28,6 +35,8 @@ SCRIPT(addState);
2835
params [
2936
["_stateMachine", locationNull, [locationNull]],
3037
["_onState", {}, [{}]],
38+
["_onStateEntered", {}, [{}]],
39+
["_onStateLeaving", {}, [{}]],
3140
["_name", "", [""]]
3241
];
3342

@@ -45,6 +54,8 @@ if (_name == "") then {
4554
_states pushBack _name;
4655
_stateMachine setVariable [QGVAR(states), _states];
4756
_stateMachine setVariable [ONSTATE(_name), _onState];
57+
_stateMachine setVariable [ONSTATEENTERED(_name), _onStateEntered];
58+
_stateMachine setVariable [ONSTATELEAVING(_name), _onStateLeaving];
4859
_stateMachine setVariable [TRANSITIONS(_name), []];
4960

5061
diag_log "derp";

addons/statemachine/fnc_clockwork.sqf

+19-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ SCRIPT(clockwork);
4141
_stateMachine setVariable [QGVAR(tick), _tick + 1];
4242

4343
private _current = _list select _tick;
44-
private _thisState = _current getVariable [QGVAR(state) + str _id, _stateMachine getVariable QGVAR(initialState)];
44+
private _thisState = _current getVariable (QGVAR(state) + str _id);
45+
46+
if (isNil "_thisState") then {
47+
// Item is new and gets set to the intial state, onStateEntered
48+
// function of initial state gets executed as well.
49+
_thisState = _stateMachine getVariable QGVAR(initialState);
50+
_current setVariable [QGVAR(state) + str _id, _thisState];
51+
_current call (_stateMachine getVariable ONSTATEENTERED(_thisState));
52+
};
4553

4654
// onState functions can use:
4755
// _stateMachine - the state machine
@@ -52,15 +60,24 @@ SCRIPT(clockwork);
5260
private _thisOrigin = _thisState;
5361
{
5462
_x params ["_thisTransition", "_condition", "_thisTarget", "_onTransition"];
55-
// Transition conditions and onTransition functions can use:
63+
// Transition conditions, onTransition, onStateLeaving and
64+
// onStateEntered functions can use:
5665
// _stateMachine - the state machine
5766
// _this - the current list item
5867
// _thisTransition - the current transition we're in
5968
// _thisOrigin - the state we're coming from
69+
// _thisState - same as _thisOrigin
6070
// _thisTarget - the state we're transitioning to
71+
// Note: onTransition and onStateLeaving functions can change
72+
// the transition target by overwriting the passed
73+
// _thisTarget variable.
74+
// Note: onStateEntered functions of initial states won't have
75+
// some of these variables defined.
6176
if (_current call _condition) exitWith {
77+
_current call (_stateMachine getVariable ONSTATELEAVING(_thisOrigin));
6278
_current call _onTransition;
6379
_current setVariable [QGVAR(state) + str _id, _thisTarget];
80+
_current call (_stateMachine getVariable ONSTATEENTERED(_thisTarget));
6481
};
6582
} forEach (_stateMachine getVariable TRANSITIONS(_thisState));
6683
};

addons/statemachine/fnc_createFromConfig.sqf

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ private _stateMachine = [_list] call FUNC(create);
3232
{
3333
private _state = configName _x;
3434
private _onState = compile getText (_x >> "onState");
35-
[_stateMachine, _onState, _state] call FUNC(addState);
35+
private _onStateEntered = compile getText (_x >> "onStateEntered");
36+
private _onStateLeaving = compile getText (_x >> "onStateLeaving");
37+
[_stateMachine, _onState, _onStateEntered, _onStateLeaving, _state] call FUNC(addState);
3638

3739
false
3840
} count ("true" configClasses _config);

addons/statemachine/script_component.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515

1616
#define TRANSITIONS(var) (var + "_transitions")
1717
#define ONSTATE(var) (var + "_onState")
18+
#define ONSTATEENTERED(var) (var + "_onStateEntered")
19+
#define ONSTATELEAVING(var) (var + "_onStateLeaving")

0 commit comments

Comments
 (0)