User Tools

Site Tools


dragengine:modules:dragonscript:gabehavior_statemachine

This is an old revision of the document!


GABehaviorStateMachine

Game app behavior running a state machine (*.desm) on the BaseGameApp. The state machine is loaded from the application configuration and runs for the whole lifetime of the application. Other game app behaviors and state machines can add actions and conditions to it to control the application flow, for example showing logos, changing input processors or quitting the engine.

The class works similar to ECBehaviorStateMachine.

The state machine is configured in the application configuration file like this example:

<parameterTree>
    <string path='appBehavior/stateMachine/path'>/content/statemachines/game.desm</string>
    <string path='appBehavior/stateMachine/debug'>SM-Game</string>
</parameterTree>

Multiple instances can be added to the application using an identifier. If the path is not set or the file is missing an empty state machine is used.

To access this behavior from other application behaviors use GABehaviorStateMachine.getBehaviorInApp(). If more than one state machine is used use GABehaviorStateMachine.getBehaviorInApp(id) with the identifier of the desired state machine. Add actions and conditions in the initGame() function.

Configuration

The state machine is configured in the application configuration file using these keys:

appBehavior/stateMachine/path

Set path to state machine file to load.

  • Full name: appBehavior/stateMachine/path or appBehavior/stateMachine({id})/path
  • Type: string
  • Default Value: empty string
  • Expected File Type: *.desm
  • Example (*.deeclass)
    <string path='appBehavior/stateMachine/path'>/content/statemachines/game/game.desm</string>

appBehavior/stateMachine/debug

Enable debug logging for the state machine using the given logging name.

  • Full name: appBehavior/stateMachine/debug or appBehavior/stateMachine({id})/debug
  • Type: string
  • Default Value: <null>
  • Example (*.deeclass)
    <string path='appBehavior/stateMachine/debug'>SM-Game</string>

Events

This behavior runs these state machine events.

initGame

Run after all game app behaviors have initialized.

servicesInitialized

Run once when all game service initialization has finished.

update

Run every frame. The event parameter is the elapsed time in seconds.

appActivated

Run when the application window has been activated.

appDeactivated

Run when the application window has been deactivated.

Actions

This behavior adds these state machine actions.

engine.update

Update game engine parameters and run game engine actions.

ParameterValueDescription
resetTimers Reset engine timers. Calling this is useful after initial game loading to ensure next frame update starts with huge elapsed time accumulated.
restartstringRestart engine with the given value. Allows to return to the same state after restart.
logstringLog message to console.
quit Quit game immediately.

This is an example of using this action to reset timers after lengthy loading:

<action name='engine.update'>
  <parameter name='resetTimers'/>
</action>

app.update

Update application parameters and run application actions.

ParameterValueDescription
inputProcessordesktop, bindings, none

Set input processor.

  • desktop: Input is processed by BaseGameApp.getDesktop(). Use this for main menus where mouse and keyboard should work like in regular applications.
  • bindings: Input is processed by BindingManager. This runs game related commands bound to input devices. Use this for typical game use.
  • none: Input is not processed at all. Useful to prevent user input for a short time.
desktop.guiThemestringLoad Gui Theme and assign it to BaseGameApp desktop.
settings.save Save settings.
bindings.save Save bindings.
loadGameWorld.cancel Cancel loading game world.
loadGameWorld.finish Finish loading game world.

This is an example of using this action to set input processing to bindings:

<action name='app.update'>
  <parameter name='inputProcessor'>bindings</parameter>
</action>

windowLogo.update

Update window logo parameters and run window logo actions. Allows to show logos as typically done on game start up. The logo window is faded in, stays a short time, then fades out again.

ParameterValueDescription
showdragengine, image, videoLogo to show.
imagestringPath to image to show. Required if show is set with image as value.
soundstringPath to sound/music to play. Optional if show is set.
videostringPath to video to show. Required if show is set with video as value.
eventstringEvent to run on the state machine once the logo finished (faded out). Required if show is set.

This is an example of using this action to show an image logo:

<action name='windowLogo.update'>
  <parameter name='show'>image</parameter>
  <parameter name='image'>/content/logo.avif</parameter>
  <parameter name='sound'>/content/logo.ogg</parameter>
  <parameter name='event'>logo finished</parameter>
</action>

windowGameWorld.set

Set WindowGameWorld parameters.

ParameterValueDescription
visibletrue, falseShow/hide WindowGameWorld.

This is an example of using this action to hide the WindowGameWorld:

<action name='windowGameWorld.set'>
  <parameter name='visible'>false</parameter>
</action>

windowGameWorld.update

Update WindowGameWorld parameters and run WindowGameWorld actions.

ParameterValueDescription
runEventstringRun event on GWBehaviorBTSM in the active game world. This allows application state machine to interact with element behavior state machines inside the game world.

This is an example of using this action:

<action name='windowGameWorld.update'>
  <parameter name='runEvent'>inventory closed</parameter>
</action>

Conditions

This behavior adds these state machine conditions.

engine.check

Evaluate engine parameters.

ParameterValueDescription
engine.appActivetrue, falseApplication window is active.
engine.editModetrue, falseEngine is in edit mode. For regular games this is false.
engine.supportsServicestringEngine supports named service.
engine.supportsService.notstringEngine does not support named service.

This is an example of using this condition to check if a game service is supported and can be used (in this example modding using Mod.io):

<action name='myAction'>
  <condition>engine.check</condition>
  <parameter name='engine.supportsService'>Mod.io</parameter>
</action>

app.check

Evaluate application parameters. app.config. checks BaseGameApp.getConfiguration(). app.setting. checks BaseGameApp.getSettings(). Configuration and setting values can be various data types. These comparisons require boolean and integer values. Other values fail the test if their toString() implementation does not produce a value convertible to an integer value or boolean value (true, false). If you just want to test for equality you can use string type too.

ParameterValueDescription
app.configstringConfiguration key to compare. Used together with the app.config.* comparisons.
app.config.equalstringConfiguration value equals value.
app.config.notEqualstringConfiguration value does not equal value.
app.config.lessintegerConfiguration value is less than value. Requires int value.
app.config.lessOrEqualintegerConfiguration value is less than or equal to value. Requires int value.
app.config.greaterintegerConfiguration value is greater than value. Requires int value.
app.config.greaterOrEqualintegerConfiguration value is greater than or equal to value. Requires int value.
app.settingstringSetting key to compare. Used together with the app.setting.* comparisons.
app.setting.equalstringSetting value equals value.
app.setting.notEqualstringSetting value does not equal value.
app.setting.lessintegerSetting value is less than value. Requires int value.
app.setting.lessOrEqualintegerSetting value is less than or equal to value. Requires int value.
app.setting.greaterintegerSetting value is greater than value. Requires int value.
app.setting.greaterOrEqualintegerSetting value is greater than or equal to value. Requires int value.
services.initializedtrue, falseAll initializing services have finished.

This is an example of using this condition to check a string configuration value:

<action name='myAction'>
  <condition>app.check</condition>
  <parameter name='app.config'>game/mode</parameter>
  <parameter name='app.config.equal'>multiplayer</parameter>
</action>

This is an example of using this condition to check an integer configuration value:

<action name='myAction'>
  <condition>app.check</condition>
  <parameter name='app.config'>game/maxPlayers</parameter>
  <parameter name='app.config.lessOrEqual'>2</parameter>
</action>

windowGameWorld.check

Evaluate WindowGameWorld parmaeters.

ParameterValueDescription
visibletrue, falseWindowGameWorld is visible.

This is an example of using this condition:

<action name='myAction'>
  <condition>windowGameWorld.check</condition>
  <parameter name='visible'>true</parameter>
</action>

Required Behaviors

This behavior requires no other behaviors.

Optional Behaviors

This behavior does not support optional behaviors.

API Documentation

GABehaviorStateMachine.

Since DragonScript Module Version 1.35

Use Cases

  • Drive higher level application logic using state machine.
  • Drive UI interaction using state machine.

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/gabehavior_statemachine.1792095556.txt.gz · Last modified: by dragonlord