S2ENGINE HD  1.4.6
Official manual about S2Engine HD editor and programming
GameMachine

GameMachine is a tool specifically designed for visually programming game objects logic and animations.
It is based on the concept of Finite State Machine (FSM): every state is represented as a node of a diagram and is a collection of successive Actions, nodes are connected trough lines representing transitions from a state to another, every transition is triggered by Events. Just as all other tool GameMachine can be accessed by clicking on an icon of the main toolbar:

gameMachine01.jpg

Or by clicking on Tools->Events on the main menu

gameMachine02.jpg

A GameMachine graph is store inside a resource file with extension .gms. All gms files can be accessed via Events browser that is opened by clicking on events icon of the Project tool:

gameMachine06.jpg

To create a new gms resource click left mouse button on events browser free space and click on new file in the appearing drop down menu:

gameMachine04.jpg
Warning
One GameMachine graph is associated to one object of the scene.
A GameMachine graph must be associated to an object inside the scene to be edited.
To link a game machine to an object:
  1. Select the object
  2. Open ClassInspector
  3. Click on fsm parameter
  4. Select the gms file to load
  5. Click on Apply button
    gameMachine07.jpg

After creating a new GameMachine graph and linked it to an object inside the scene you can edit it by clicking left mouse button on the created file icon and choose Inspect in the appearing drop-down menu

gameMachine05.jpg

Alternatively you can press left mouse button directly on the selected object inside the scene and choose fsm on the appearing drop-down menu:

gameMachine03.jpg

The GameMachine tool is composed by two elements:

gameMachine09.jpg
  1. FSM Graph viewport: viewport in which you can view the finite state machine graph, adding new transition and connect nodes (states).
    See also
    Viewports
  2. FSM editor: a tool for editing nodes of the graph, managing variables and events.

When inspecting a gms file associated with an object inside the scene there are two strings inside the Graph Viewport:

  1. The path of the resource file containing the graph
  2. The name of the scene object the graph is linked to
    gameMachine08.jpg
Note
The second string doesn't appear if you inspect a graph that isn't associated to an object inside the scene.

As said Graph Viewport lets you to edit the graph. After some editing graphs can became quite big so they cannot be showed completely inside the viewport, so some input controls have been designed to facilitate graph exploration and editing:

  • Zoom: you can zoom by moving mouse roll when pointing inside graph viewport
  • Scroll: you can scroll the graph by leaving pressed right mouse button and moving

The FSM editor tool has 4 tabs for respectively:

  1. Editing States
  2. Editing Events
  3. Editing Variables
  4. Editing Animations
Note
To apply changes when editing FSM linked to an object of the same you must click to the apply icon of the FSM editor as showed into the following image
gameMachine13.jpg
It is equivalent to the apply button of the ClassInspector
Warning
When working on a GameMachine graph do not forget to save all progress by clicking on the Save button showed into the following image:
gameMachine14.jpg
this will save the gms file that stores the graph.

Finite State Machine

As said Game Machine graphs represent Finite State Machines(FSM). Every node of the GameMachine graph is a state of the FSM, when an event occurs, for example a collision, a transition is generated that makes FSM to pass from the current state to another state, in GameMachine graphs transitions are represented as directional lines connecting nodes. Every frame an object performs a state until an event occurs. Performing a state means executing the set of actions that state contains and/or state animation if exists.

States

To add a new state press left mouse button on a free space of FSM graph viewport:

gameMachine12.jpg

A new node of the graph will appear.
To select an existing state click on the respective graph node in the graph viewport, all state data will appear inside the FSM editor state tab:

gameMachine15.jpg
  1. State Name: Here you can change the name of the state.
  2. State Color: The color of the node showed into the GraphViewport
  3. Actions List: The list of actions executed by the state

Actions

Actions are basic component of a state, they define the behaviour of an object until it remains in that state.
When an object perform a state it just executes, in sequence, all the actions defined in the state. Actions are executed in sequence every frame, unless they have everyframe parameter set to false.
Every action has its own parameters basing on its class.

Note
All actions have a common parameter called enabled. This parameter lets you to exclude the action during the simulation.

Actions can send events depending on class their belong to. Also actions can output some values to be stored inside variables, variables can be used as input to action parameters depending on variable type, parameter type and action class.

To add a new action to a selected state click on the Add button into the state tab of the FSM editor as showed into the following image:

gameMachine16.jpg

A new dialog will appear that let you to choose the action to add:

gameMachine17.jpg
  1. Available actions list: List from which to choose the action. The actions are divided into groups basing on their functionality. Click to an action for selecting it.
  2. Selected action description: A brief description of the selected action
  3. Selected action parameters: The list of parameters of the selected action

After choosing the action click on Add button to add it. For the complete list of actions see Action Classes

Variables

Sometimes it should be necessary to store some action output in order to reuse it inside the same state. This lets you avoiding to create too many states that make Finite State Machine graph growing too much and being too complex. Action output can be store inside variables. Variables can assume values depending on their type:

  • string
  • float
  • int
  • vec3
  • bool

They can be local and global:

  • Local variables: Can be used inside a single FSM, they are shared among all states and they are destroyed when FSM is destroyed.
  • Global variables: They are shared among all FSMs and they are destroyed when level is closed

To Add a new variable open the variables tab in the FSM editor and then:

gameMachine18.jpg
  1. Write the name of the variable
    Note
    make sure the name doesn't exist
  2. Click on the Add button

A new variable will appear in the variables list. At start variable is of type bool but you can change type by clicking on the combobox and choose new type as showed into the following image:

gameMachine19.jpg

Once you have created a variable you can store a value in it depending on the action. Many actions have output parameters that get in input a variable, they are showed as combobox so you can click on them and choose the variable to assign.

gameMachine20.jpg
Note
the output parameters are sensitive to variable type, this means that when you have to choose the variable to assign, only variables of a compatible type are showed.

Once you have stored a value inside a variable you can use it as input of another action parameter. Many action parameters have a combobox on the right, this combo is used to store the input variable to be used instead of specifying directly the value to assign:

gameMachine21.jpg
The value of 'aimY' variable is assigned to the 'angle' parameter instead of 10.0000 value

Events

Events are what FSM needs to change state: when an object is in a state and receives an event it changes state.
How object knows in what is the state to pass to? The answer is: Transitions.
A transition is a couple (event,state) that associates an event that an object can receive and the state in which the object must pass if it receives that event. In other words Transitions define the FSM behaviour, i.e. the new state to be in response to an event basing on previous state.
What means "basing on previous state"?
Transitions are defined for every state, so previous state means the state that contains the transition while new state means the state transition point to. So when object is in a given state it can pass only to states pointed by transitions contained into that current state, this means that for every state only some events produce a transition to another state. The number and type of transitions for every state is the basic of FSM programming.
Events are shared among all FSMs of a game scene.
There are some default events that are generated by system, other events can be generated by Animation Keyframes and by Cutscenes, all other events can be generated by Actions. Default system events are the following:

  • START: This event is generated one time when a FSM starts
  • FINISHED: This event is generated when a state finishes processing. A state finishes when all its actions finish, actions that have everyFrame set to true do not finish. Other actions are temporized, they finish after a given time interval, some other actions finish after one frame they are executed.
  • ENTER_VOLUME: This event is generated when an object enters a volume. The volume must be linked to a trigger.
  • INSIDE_VOLUME
  • EXIT_VOLUME
  • ANIMATION_FINISHED: This event is generated when an animation finish. NOTE that loop animations generate this event every time they reach last frame
  • PLAY_CUTSCENE: This event is generated when a cutscene starts playing
  • STOP_CUTSCENE: This event is generated when a cutscene stops playing
  • PAUSE_CUTSCENE: This event is generated when a cutscene pauses playing
  • HIT: This event is generated when an object is hit by a Projectile or a raycaster or when a RayCast action is performed
  • COLLIDED
  • CONTACT
  • BUTTON_CLICKED
  • BUTTON_PRESSED
  • BUTTON_RELEASED

To Add a new custom event to GameMachine open events tab of FSM editor and then:

  1. Insert Event name
  2. Click on Add button
    gameMachine22.jpg

To Add a transition to a state press left mouse button on the state node inside the Graph Viewport and select Add Transition in the appearing dropdown menu:

gameMachine10.jpg

A new combobox will appear in the state node, then select the event which must trigger the transition in the combobox as showed into the following image:

gameMachine23.jpg

Now you must connect the event to a target state in order to make object pass to the new state when the event occurs. So click on the yellow dot on the transition combobox side and, leaving press end the left mouse button, drag mouse on the blue dot on the side of the target state node you want:

gameMachine24.jpg

Global Transitions

There is a special kind of transition that we call global.
It doesn't need a previous state to work, this means that global transitions can happen in every state, the only condition they depend on is that event they are associated to occurs. A typical example of Global Transition is the transition generated by event START when FSM begins. Thanks to this features Global transitions can help to reduce graph node connections and number of nodes(states) necessary to implement response to an event.
Global Transition are showed as orange comboboxes on top of a state node.

Note
The state receiving START event is the FSM Start State. An FSM always need a Start State to start processing.
gameMachine35.jpg

Since global transitions do not depend on a previous state we DO NOT need to define the transition on a state and then connect it to a target state, BUT we must define the global transition directly on the target state.
To Add a global transition press right mouse button on the state node we want to be transition target state and choose Add Global Transition on the appearing dropdown menu:

gameMachine11.jpg

An Orange combobox will appear on the top of the node. Choose the event that must generate the transition using the dropdown menu of the combobox.

gameMachine25.jpg
Note
As you can note in the previous images, LOCKED state isn't connected to any other state, however if lock event occurs the FSM goes to LOCKED state any state it is previously in.

Animations

GameMachine Finite State Machines are also designed for managing animation transitions.
Every state node of an FSM can play an animation (see also ModelScript and Animations), transition between states correspond to transition (blending) between animations. When a state is associated to an animation, it plays the animation when starts and when animation finishes it fires an ANIMATION_FINISHED event, so you can define a transition to another state that is processed when this event occurs:

gameMachine26.jpg
Note
Animations played in a state are, by default, always played in loop

When you link a gms resource to an object inside the scene, and this object owns a modelscript (see Models) FSM automatically has access to all animations defined for that object. To associate an animation to a state:

  1. Select the state node and open the anim tab of the FSM editor
  2. Choose the animation from the anim combobox as showed into the following image:
    gameMachine27.jpg
Warning
Make sure the FSM is already linked to an object inside the scene, this is necessary because, to show available animations, FSM must access to the linked object modelscript.

You can also set how much time the transition from the animation of current state to animation of another state must take, by setting the CrossFade time.

gameMachine28.jpg
Note
The crossFade time is expressed in milliseconds.
See also
Animation Blending

If animation associated to the state is of type 1D/2D Blended animations 1D/2D Blended there are some other settings you must take care of.
As said 1D/2D Blended animations are special kind of animation owning more that one simple animation inside, they can blend between contained animations basing on input float values inside the [0,1] range.
These float parameters are taken from FSM variables, so you can control animation blending basing on the value of a variable.
The combobox lets you to specify what variable must use to obtain blend value as showed into the following image:

gameMachine29.jpg

1D/2D Blended animations works inside [0,1] range but, usually, variables could assume values inside greater intervals, so you must specify what is the range in which variable assume values in order to make system transform this interval into the [0,1].

gameMachine30.jpg
Note
CrossFade is performed by animations of states belonging to the same Layer.

Layers

The concept of FSM Layers is strictly associated to the concept of Animation Channels. Just as Animation Channels FSM layers are superimposed layers that works independently.
At a lower level every FSM Layer owns an animation channel, so animations belonging to different layers are blended in the same way as they belong to different channels.

Note
When a FSM is created it has, by default, only one layer automatically named as baseLayer and it corresponds to the animation channel of 0 ID.

To Add a new layer into the FSM click on + button in the FSM Graph viewport as showed into the following image:

gameMachine31.jpg

A new layer dialog appears and lets you to insert a name for the new layer. Once a new layer is created you can show it by selecting its name into the layers combobox as showed in the following image:

gameMachine32.jpg

When a layer is selected it is showed in the FSM Graph Viewport, so you can edit it.

Note
Layers own Animation Channels with incrementing IDs starting from baseLayer (ID 0) to successive. This means that a Layer added on top of baseLayer owns an animation channel that covers baseLayer animation channel, and so on.

If the modelscript of the object associated to the FSM contains an Channel mask you can apply it to the selected layer, in this way all animations played in that layer will be masked just as described in Channel mask.
To Associate a mask to the selected layer you must select it from the combobox on the right of layers combobox, as showed in the following image:

gameMachine33.jpg
Note
baseLayer cannot have a mask, because it contains the first animation channel (0 channel).

As said animations CrossFade is performed between states belonging to the same layer. Between different Layers animation blending is performed using channel blending (see Animation Channels).
When you select a state belonging to a layer on top of baseLayer a new group of parameters, channel group, appears into the anim tab of the FSM editor:

gameMachine34.jpg

This group of parameters lets you to decide how the animation channel of the FSM layer must be blended.

  • Enable: animation channel must be enabled for this state? If no, no animations are played.
  • PlayOnce: the channel animation must be stopped when finishes (no loop)
  • FadeInTime: duration of the FadeIn channel transition, in milliseconds
  • FadeOutTime: duration of the FadeOut channel transition, in milliseconds

Animations Example

To better understand How game Machine works with animations and layers we show an example similar to that showed in Scripting Animations.
We use the same marine model and modelscript as in Scripting Animations sample.

  • Create a gms resource
  • Drag and drop it on the marine model.
  • Click on fsm in the appearing dropdown menu as showed into the following image:
    gameMachine36.jpg

  • Select marine character
  • Open ClassInspector
  • Click on Apply button to update marine character
    gameMachine37.jpg

  • Now Add 4 new events:
    1. idle
    2. run
    3. aim
    4. draw_gun
      gameMachine43.jpg

  • Create two states:
    1. STANDBY
    2. WALK
  • Add global transition START to STANDBY state
  • Add transition run to STANDBY state
  • Add transition idle to WALK state
  • Connect run transition to STANDBY state
  • Connect idle transition to WALK state
  • Select STANDBY state
  • In the anim tool tab select standby animation and write 100 in the CrossFadeTime inputboxù
    gameMachine38.jpg

  • Select WALK state
  • In the anim tool tab select walk animation and write 100 in the CrossFadeTime inputbox
    gameMachine39.jpg

  • Add new Layer and call it Aim
  • Select BodyTop mask
  • Create two states:
    1. AIM_STAND
    2. AIM
  • Add global transition START to AIM_STAND state
  • Add transition aim to AIM_STAND state
  • Add transition draw_gun to AIM state
  • Connect aim transition to AIM state
  • Connect draw_gun transition to AIM_STAND state
  • Select AIM_STAND state
  • In the anim tool tab write 200 in the FadeOutTime inputbox of the channel group.
    gameMachine40.jpg
    Note
    There is no animation into the AIM_STAND state. This state is just used for Fading Out the animation channel of the aim layer. Also note the channel isn't enabled.

  • Select AIM state
  • In the anim tool tab Check Enable in the channel group box
  • In the anim tool tab write 200 in the FadeInTime inputbox of the channel group.
    gameMachine41.jpg
    Note
    The channel is enabled. The AIM state is used for smoothly fade in the aim animation channel of the aim layer onto the underlying baseLayer channel.
    Now you can test the Finite State Machine by sending the aim, idle, run and draw_gun events:
  • Open the ClassInspector
  • Select the event you want to send from events roll-up
  • Click on Send button
    gameMachine42.jpg

Runtime Testing

Action Classes

Scripted

AICreateCharacter

AICreateObject

AISetSteering

AIGetFurthestTacticalPoint

AISetCharacterMaxSpeed

AISetReaching

AIGetNearestTacticalPoint

AIGetNearestCoverPoint

AIGetFurthestCoverPoint

AIGetNearestAllowedPoint

AIEnableSense

AIObjectIsViewed

AIObjectIsHeard

AIFindPath