User Tools

Site Tools


dragengine:modules:dragonscript:behavior_behaviortreetimer

ECBehaviorBehaviorTreeTimer

Behavior element behavior adding timer support to behavior tree support.

Behavior adds behavior tree actions and conditions to an ECBehaviorBehaviorTree allowing behavior trees to start and wait for multiple independent timers.

Instance Counts

This behavior can be added multiple times to an element for each ECBehaviorBehaviorTree. Each instance creates an own set of behavior tree action and conditions and timers.

Element Class Properties

Element class properties have the prefix behaviorTreeTimers. or behaviorTreeTimers({id}). if id is not empty.

Events

This behavior has no events.

Behavior Tree Actions

This behavior adds these behavior tree actions.

timer.start

Starts a timer. As parameter use timeout to set the seconds until the timer elapses or timeout.lower and timeout.upper to randomly select seconds between a lower and upper limit (inclusive). Optionally use parameter timer to select the timer. timer is a string identifier you can freely choose. If absent empty string is used as timer identifier. The timer is created if absent. This action immediately returns and does not require a rule identifier.

This is an example of using this action:

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

This is an example of using this action with random timeout:

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

timer.stop

Stops a timer. Optionally as parameter use timer to select the timer matching an identifier used in a previous action timer.start. This action immediately returns and does not require a rule identifier.

This is an example of using this action:

<action name='timer.stop'>
  <parameter name='timer'>myTimer</parameter>
</action>

timer.wait

Waits for a timer to elapse. Optionally as parameter use timer to select the timer matching an identifier used in a previous action timer.start. The action keeps running until the timer elapsed. If the timer is absent the action immediately returns.

This is an example of using this action:

<action name='timer.stop' id='waiting'>
  <parameter name='timer'>myTimer</parameter>
</action>

Behavior Tree Conditions

This behavior adds these behavior tree conditions.

timer.running

Determines if timer is running and has not elapsed yet. Optionally as parameter use timer to select the timer matching an identifier used in a previous action timer.start.

This is an example of an action that can only run if the timer myTimer is running:

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

timer.finished

Determines if timer is not running or has elapsed. Optionally as parameter use timer to select the timer matching an identifier used in a previous action timer.start.

This is an example of an action that can only run if the timer myTimer is running:

<action name='myAction' id='doing something'>
  <parameter name='timer'>myTimer</parameter>
  <condition>timer.finished</condition>
</action>

Required Behaviors

Optional Behaviors

This behavior does not support optional behaviors.

Persistency

This behavior does support element class to be persistable (setPersistable).

API Documentation

ECBehaviorBehaviorTreeTimer.

Since DragonScript Module Version 1.0

Use Cases

  • Allow timers to be used in behavior trees.

Element Class Example

This example defines an element which can use timers in behavior trees.

class MyElement extends BehaviorElementClass
  public var ECBehaviorBehaviorTree behaviorTree
  public var ECBehaviorBehaviorTreeTimer behaviorTreeTimer
  func new()
    behaviorTree = ECBehaviorBehaviorTree.new(this)
    behaviorTreeTimer = ECBehaviorBehaviorTreeTimer.new(this, behaviorTree)
  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='ECBehaviorBehaviorTree'/>
 
  <behavior type='ECBehaviorBehaviorTreeTimer'>
    <!-- optional: use behavior tree with id instead of empty string -->
    <string name='behaviorTree'>second</string>
 
    <!-- set element properties. omit property prefix if used inside behavior tag -->
    <string name='.name'>value</string>
  </behavior>
 
  <!-- for adding multiple behaviors use unique identifiers -->
  <behavior type='ECBehaviorBehaviorTreeTimer' id='second'/>
</elementClass>

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_behaviortreetimer.txt · Last modified: 2025/03/12 19:41 by dragonlord