User Tools

Site Tools


dragengine:modules:dragonscript:behavior_hearsounds

This is an old revision of the document!


ECBehaviorHearSounds

Behavior element behavior adding hearing sounds support.

Uses a SoundLevelMeter resources to hear what sounds are playing in the audible range of the element.

SoundLevelMeter resources take sound properties of the environment into account like occlusion and how sound propagates through the world geometry. This allows game entities to hear the world similar to how a player hears the world what goes for sound volumes.

The SoundLevelMeter resource can be queried for the speakers currently in audible range and the volume they exhibit. Advanced effects audible to players like frequency band specific gains or special effects are disregarded. Furthermore speakers entering and leaving trigger a notification allowing elements to track elements of interest.

If the ECBehaviorCollider is present in the behavior element the SoundLevelMeter is attached to the collider.

The behavior retrieves the owner of Speakers inside the audible range. If these owners are elements they are stored in a list of elements heared by the owner element. This list is updated when elements enter or leave the audible range. The volume of an element can be queried if required but is not stored.

The owner behavior element as well as other ECBehavior subclasses can add a listener to be notified if elements enter and exit the audible range.

Instance Counts

This behavior can be used only once on an element.

Element Class Properties

Element class properties have the prefix hearSound. .

audibleDistance

Audible distance in meters.

  • Full name: hearSound.audibleDistance
  • Type: float
  • Default Value: 100
  • Restriction: At least 1
  • Example (*.deeclass)
    <float name='hearSound.audibleDistance'>50</float>

volumeThreshold

Volume threshold required for elements to be considered audible.

  • Full name: hearSound.volumeThreshold
  • Type: float
  • Default Value: 0.01
  • Restriction: At least 1
  • Example (*.deeclass)
    <float name='hearSound.volumeThreshold'>0.02</float>

position

Position to attach resource to collider.

  • Full name: hearSound.position or hearSound({id}).position
  • Type: 3-component float vector
  • Default Value: (0,0,0)
  • Example (*.deeclass)
    <vector name='hearSound.position' x='0' y='0' z='0.1'/>

orientation

Orientation to attach resource to collider in degrees.

  • Full name: hearSound.orientation or hearSound({id}).orientation
  • Type: 3-component float vector
  • Default Value: (0,0,0)
  • Example (*.deeclass)
    <vector name='hearSound.orientation' x='30' y='0' z='0'/>

bone

Bone to attach resource to. If empty string attach to collider.

  • Full name: hearSound.bone or hearSound({id}).bone
  • Type: string
  • Default Value: empty string
  • Example (*.deeclass)
    <string name='hearSound.bone'>attach</string>

Events

elementAudible

Element become audible.

elementInaudible

Element became inaudible.

Behavior Tree Actions

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

hearSound.check

Check one or more hear sound parameters. Action succeeds if all parameter value matches their respective hear sound parameter otherwise action fails. This action is typically used as first action in a sequence to run the sequence only if a hear sound parameter matches (or not).

ParameterValueDescription
countintegerCount of audible elements equals integer value
count.notintegerCount of audible elements does not equal integer value
count.lessintegerCount of audible elements is less than integer value
count.greaterintegerCount of audible elements is greater than integer value
playertrue, falsePlayer is audible

This is an example of using this action:

<sequence>
  <action name='hearSound.check'>
    <parameter name='player'>true</parameter>
  </action>
  <!-- actions here run only if player is audible by element -->
</sequence>

Behavior Tree Conditions

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

hearSound.check

Check one or more hear sound parameters. Conditions returns true if all parameter value match their respective hear sound parameter. This condition is typically used to run an action or sequence of actions as long as hear sound conditions are true.

ParameterValueDescription
hearSound.countintegerCount of audible elements equals integer value
hearSound.count.notintegerCount of audible elements does not equal integer value
hearSound.count.lessintegerCount of audible elements is less than integer value
hearSound.count.greaterintegerCount of audible elements is greater than integer value
hearSound.playertrue, falsePlayer is audible

This is an example of using this condition:

<action name='myAction' id='doing something'>
  <parameter name='hearSound.player'>true</parameter>
  <condition>hearSound.check</condition>
</action>

State Machine Actions

State Machine Conditions

State Machine Events

This behavior sends these state machine events.

hearSound.audible

Element become audible.

hearSound.inaudible

Element became inaudible.

Required Behaviors

This behavior requires no other behaviors.

Optional Behaviors

Persistency

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

API Documentation

ECBehaviorHearSounds.

Since DragonScript Module Version 1.0

Use Cases

  • Detect if element is hearing sounds.

Element Class Example

This example defines an element which can detect hearing sounds.

class MyElement extends BehaviorElementClass
  public var ECBehaviorComponent component
  public var ECBehaviorCollider collider
  public var ECBehaviorHearSounds hearSounds
  func new()
    component = ECBehaviorComponent.new(this, null)
    collider = ECBehaviorCollider.new(this, component)
    headSounds = ECBehaviorHearSounds.new(this, collider)
  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='ECBehaviorComponent'/>
  <behavior type='ECBehaviorCollider'/>
 
  <behavior type='ECBehaviorHearSounds'>
    <!-- optional: use behavior tree with id instead of empty string -->
    <string name='behaviorTree'>second</string>
 
    <!-- optional: use state machine with id instead of empty string -->
    <string name='stateMachine'>second</string>
 
    <!-- set element properties. omit property prefix if used inside behavior tag -->
    <float name='.audibleDistance'>50</float>
  </behavior>
</elementClass>

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_hearsounds.1746224929.txt.gz · Last modified: 2025/05/02 22:28 by dragonlord