StateMachine

The StateMachine, as the name suggests, watches over the different states of an entity.

Most parts of a LUA file are single processed LUA functions which are done once every time they are used.

States however, are like an active process - like the inner part of a while-loop.

## What is a Riftbreaker LUA state?

There are no pre-defined lists of states for the LUA files. Everyone can create states as they wish. It is best practice, to give it a fitting name though.

The most used/created state for buildings, is the state "working" - mostly because it has a big meaning and could be used for almost any building as most building do something all the time, and thus be "working".

## Structure

The StateMachine consists of different parts:

### The creation of the StateMachine

Usually at the top of the LUA file, the StateMachine itself gets created.

It's simply:

```
self.fsm = self:CreateStateMachine()

```

### The creation of the states

Right after the creation of the StateMachine itself, the different states get created.

It looks like this:

```
self.fsm:AddState( "building", { enter="\_OnBuildingEnter", execute="\_OnBuildingExecute", exit="\_OnBuildingExit", interval=0.5" } )

```

That line consists of different parts,

 from="\*"
 enter="\_OnBuildingEnter"
Executed only once on state enter
 execute="\_OnBuildingExecute"
Executed every tick/interval until state change/ duroation limit reached
 exit="\_OnBuildingExit"
Executed only once on state exit
 interval=0.5

The 3 main parts in the { } brackets reflect the normal lifecycle of a state(Enter, Execute, Exit).

It enters the state, it does the state, it exits the state. How and when it shall do these steps of the lifecycle depends on you.

You also don't need to use all 3 of them, you could just have

```
self.fsm:AddState( "idle", { execute="OnIdle" } )

```

### The use of the states

To switch between the states inside the code, you have to tell the game to change the state of an entity by using:

```
self.fsm:ChangeState( "building" )

```

Like this, the part of the state "building" switches to the next one.

### The functions triggered by the states

After a state switches, the function which is defined in the creation of the states get executed. In the LUA file it would look like this:

```
self.fsm = self:CreateStateMachine()
self.fsm:AddState( "building", { enter="\_OnBuildingEnter", execute="\_OnBuildingExecute", exit="\_OnBuildingExit" } )
self.fsm:ChangeState( "building" )
...
...

entity\_name:\_OnBuildingEnter ( state )
...
...

```

Since the state of > "building" < gets changed in line 3, it would trigger the part of > enter="\_OnBuildingEnter" <. That part would start the function in line 8 > entity\_name:\_OnBuildingEnter ( state ) <

### State functions

State functions are generally like every other function, but they can look like

```
entity_name:_OnBuildingEnter ( state )

or

entity_name:_OnBuildingEnter ( state, dt )
```

The first one has just the state

The second one also uses dt - dt means delta time in seconds, the time that elapsed from the last enter/update call.

## Methods

```
self.fsm:ChangeState("statename")
self.fsm:GetState("statename")
self.fsm:GetCurrentState()
self.fsm:Deactivate()

```