User Tools

Site Tools


dragengine:modules:dragonscript:behavior_btsmtimers

ECBehaviorBTSMTimers

Behavior element behavior adding timer support to behavior trees and state machines.

Adds actions, conditions and events allowing behavior trees and state machines to start, stop and wait for multiple independent named timers. Timers count down during think updates. A timer has elapsed once its remaining time reaches 0. Timers are persisted together with the element.

Instance Counts

This behavior can be added only once to an element class.

Element Class Properties

This behavior has no element class properties.

Behavior Tree Actions

This behavior adds these behavior tree actions if behavior tree is present.

timers.update

Start or stop named timers. Action always succeeds.

ParameterValueDescription
startstringStart named timer. The timer is added if absent. If the timer is already present it is restarted with the new timeout.
timeout0 or largerSeconds until the timer elapses. Used with start.
timeout.lower0 or largerMinimum seconds of random timeout. Used with start together with timeout.upper instead of timeout.
timeout.upper0 or largerMaximum seconds of random timeout. Used with start together with timeout.lower instead of timeout.
stopstringStop named timer.

This is an example of starting a timer:

<action name='timers.update'>
  <parameter name='start'>myTimer</parameter>
  <parameter name='timeout'>1.5</parameter>
</action>

This is an example of starting a timer using random timeout:

<action name='timers.update'>
  <parameter name='start'>myTimer</parameter>
  <parameter name='timeout.lower'>1.5</parameter>
  <parameter name='timeout.upper'>2.5</parameter>
</action>

This is an example of stopping a timer:

<action name='timers.update'>
  <parameter name='stop'>myTimer</parameter>
</action>

timers.check

Check one or more timer parameters. Not existing timers are considered stopped. Action succeeds if all parameter values match their respective timer state otherwise action fails. This action is typically used as first action in a sequence to run the sequence only if a timer condition matches (or not).

ParameterValueDescription
timerstringName of the timer to check.
runningtrue, falseTimer is running or not.
remaining.less0 or largerRemaining seconds is less than value.
remaining.greater0 or largerRemaining seconds is greater than value.
wait If present action returns BTResult.running instead of BTResult.failed to wait until the checks are all fulfilled.

This is an example of checking if a timer is running:

<action name='timers.check' id='waiting'>
  <parameter name='timer'>myTimer</parameter>
  <parameter name='running'>true</parameter>
</action>

This is an example of waiting until a timer has elapsed:

<action name='timers.check' id='waiting'>
  <parameter name='timer'>myTimer</parameter>
  <parameter name='running'>false</parameter>
  <parameter name='wait'/>
</action>

Behavior Tree Conditions

This behavior adds these behavior tree conditions if behavior tree is present.

timers.check

Check one or more timer parameters. Condition returns true if all parameter values match their respective timer state. This condition is typically used to run an action or sequence of actions as long as timer conditions are true.

ParameterValueDescription
timers.timerstringName of the timer to check.
timers.runningtrue, falseTimer is running or not.
timers.remaining.less0 or largerRemaining seconds is less than value.
timers.remaining.greater0 or largerRemaining seconds is greater than value.

This is an example of using this condition:

<action name='myAction' id='doing something'>
  <condition>timers.check</condition>
  <parameter name='timers.timer'>myTimer</parameter>
  <parameter name='timers.running'>true</parameter>
</action>

State Machine Actions

State Machine Conditions

State Machine Events

This behavior sends these state machine events.

timers.elapsed:<name>

Timer named name has elapsed. The event is sent once after the timer reached 0 remaining time. The name of the timer is appended to the event name.

This is an example of a transition that runs if the timer “myTimer” has elapsed:

<transition event='timers.elapsed:myTimer' state='nextState'>
</transition>

Required Behaviors

This behavior requires no other behaviors.

Optional Behaviors

Persistency

This behavior does support element class to be persistable (setPersistable). Running timers are persisted.

API Documentation

ECBehaviorBTSMTimers.

Since DragonScript Module Version 1.35

Use Cases

  • Delay an action, for example close a door a few seconds after it has been opened.
  • Let a state machine switch state after a timeout, for example leave an idle state after some time.
  • Add random delays using timeout.lower and timeout.upper.

Element Class Example

This example defines an element which contains timers.

class MyElement extends BehaviorElementClass
  public var ECBehaviorBTSMTimers timers
  func new()
    timers = ECBehaviorBTSMTimers.new(this)
  end
end

Behavior Factory

Using element class supporting adding behaviors the behavior can be added like this:

<?xml version='1.0' encoding='UTF-8'?>
<elementClass name='MyClass' class='GenericBehaviorElement'>
  <behavior type='ECBehaviorBTSMTimers'>
    <!-- optional: add behavior trees. default adds all behavior trees. -->
    <list name='behaviorTrees'>
      <string/> <!-- add behavior with empty identifier -->
      <string>default</string> <!-- add behavior with 'default' identifier -->
    </list>
 
    <!-- optional: add state machines. default adds all state machines. -->
    <list name='stateMachines'>
      <string/> <!-- add behavior with empty identifier -->
      <string>default</string> <!-- add behavior with 'default' identifier -->
    </list>
  </behavior>
</elementClass>

Live Examples

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