if ((Player getVariable "SpareTime") > 0) then {...
« Changelog 02/11/2009ArmA Script Recoded 1.3 »

New to ArmA 2: FSM - An introduction

Permalink 11/01/09 13:11, by jonas, Categories: ArmA

This entry is not about a kind of religion, it's about the finite state machine.
To be honest: It already exists in Armed Assault, but without proper documentation and proper script commands it was worthless.

In ArmA II we finally has the script command and the tool.

But first:
What's a finite state machine?

It's an informatics concept. More details can be found on Wikipedia. I only want to refer to the interesting parts &#59;)
We have several states including on start and one end state. They are connected with conditions. The machine starts in the start state. Then it will check the conditions. If one is true, it will move into the state the condition is connected to. Then it checks the conditions of the new state and so on, till it reaches the end state.

How is this useful?


Let's take a look at a fsm I made some time ago:
We have a civilian standing around (in a building, so it's better he doesn't move &#59;) ).
Whenever a enemy soldier comes to close, he will raise its hand in the air, then if some bullets fly around his head, he'll start complaining.
So we have three states: The initial, the surrender state and the complaining state.
From the initial state we move to the surrender state if an enemy is in range, if he is gone, we move back to the initial state.
So we can control the behaviour of units and it is a powerful instrument for this.

One note about the shown fsm: This is a very ugly fsm since we use the start as a state and don't have an end state. It is just an example :idea:

But the behaviour is not the only area where we can use FSM. We can also use it to replace scripts. More on this in a future entry.

First we're going to take a look at the history:

FSM in Armed Assault:

FSM where already existing in Armed Assault and we could start the via commandFSM and doFSM. The problem was the FSM file format: It wasn't documented. Nevertheless the community managed to solve that problem and created a few editors. The image shows JadeGear's FSM editor with the above described FSM.

The problem was on the side of Armed Assault: Running FSM could get terminated without future notice by the native FSM, the ones written by BIS. Which cause the effect that the civilian from the above FSM will simple drop on ground since the native FSM take control. Which results in rendering the FSM useless.
In combination that we can't check if our FSM is overridden, it renders FSM useless.

In ArmA II we luckily have execFSM which allows us to run FSM without an unit. But more on ArmA II in the next entry.

FSM in ArmA II:

execFSM :yes:
So we can run FSM without being dependent on a unit.

But also our FSM started with doFSM and commandFSM won't get terminated by the native FSM. The native FSM can still overrides our FSM, but it will continue running. So our civilian script from the last entry works in ArmA II.

Back to execFSM: It is not the only new function: completedFSM, getFSMVariable and setFSMVariable are also new. completedFSM will check if a FSM started with execFSM is terminated. setFSMVariable allows to save a variable into a FSM name space. The name space is unique for each FSM since the command requires a FSM handle. Sadly the variables are only access able with getFSMVariable and not directly as a local or global variable within the FSM.


Comment from: kju [Visitor]

Your style is easy to understand and good to read. Great!
What is missing a bit is more details about the possibilities and benefits of FSM and its visual editor.
Maybe a good topic for a follow-up. :)
11/01/09 @ 20:11
Comment from: jonas [Member] Email
jonasWIP ;)
11/01/09 @ 22:38