User Tools

Site Tools


dragengine:modules:dragonscript:behavior_hitscan

ECBehaviorHitScan

Behavior adding hit-scan support to elements.

Behavior supports using one or more collider collision tests to do one or more hit scans for example to check for weapon projectile impacts. Hit scans are done once then the result can be examine. To use this behavior add ECBehaviorCollider then this behavior. The collider is used to carry the collision tests.

Instance Counts

This behavior can be used multiple times on an element to add multiple independent hit scans. Use the behavior identifier to tell them apart.

Element Class Properties

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

range

Set test range.

  • Full name: hitScan.range or hitScan({id}).range
  • Type: float
  • Default Value: 20
  • Restriction: At least 0
  • Example (*.deeclass)
    <float name='hitScan.range'>0.5</float>

offset

Set test origin offset.

  • Full name: hitScan.offset or hitScan({id}).offset
  • Type: float
  • Default Value: 0
  • Restriction: At least 0
  • Example (*.deeclass)
    <float name='hitScan.offset'>0.5</float>

origin

Set test origin.

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

direction

Set test direction.

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

orientation

Set test orientation.

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

bone

Set test bone.

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

Events

This behavior has no events

Behavior Tree Actions

This behavior adds these behavior tree actions if behavior tree is present. If behavior has non-empty identifier replace hitScan with hitScan(id).

hitScan.start

Start hit scans.

ParameterValueDescription
typecircular, cone, rectangular, rectangular.uniform

Distribution shape of tests.

  • circular: Tests are arranged in a circular pattern. Set angle using angle
  • cone: Tests are arranged randomly in a small cone cap. Set count using count. Set angle using angle. Useful for example for weapons with multiple projectiles like buck shots
  • rectangular: Tests are arranged randomly across a small sphere segment. Set count using count. Set angle using angle.horizontal and angle.vertical. Useful for example for weapons with rectangular spreads like a duckbill modification
  • rectangular.uniform: Tests are arranged uniformly across a small sphere segment. Set count using count.horizontal and count.vertical. Set angle using angle.horizontal and angle.vertical. Useful for example for weapons with rectangular spreads like a duckbill modification
countintegerCount of collision tests to start
count.horizontalintegerHorizontal count of collision tests to start
count.verticalintegerVertical count of collision tests to start
anglefloatOpening angle in degrees (angle across diameter)
angle.horizontalfloatHorizontal opening angle in degrees (angle across width)
angle.verticalfloatVertical opening angle in degrees (angle across height)

This is an example of using this action:

<action name='hitScan.start'>
  <parameter name='type'>cone</parameter>
  <parameter name='count'>10</parameter>
  <parameter name='angle'>5</parameter>
</action>

hitScan.stop

Stop hit scans.

ParameterValueDescription
clear Clear hit results as if no hit has been done

This is an example of using this action:

<action name='hitScan.stop'>
  <parameter name='clear'/>
</action>

hitScan.check

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

ParameterValueDescription
runningtrue, falseHit scans are running
hitsintegerCount of hits equals integer value
hits.notintegerCount of hits does not equal integer value
hits.lessintegerCount of hits is less than integer value
hits.greaterintegerCount of hits is greater than integer value
hits.percentage.lessintegerPercentage of hits is less than value in the range from 0 to 1
hits.percentage.greaterintegerPercentage of hits is greater than value in the range from 0 to 1
wait If present action returns BTResult.running instead of BTResult.failed to wait until the checks are all fulfilled

This is an example of using this action:

<sequence>
  <action name='hitScan.check'>
    <parameter name='running'>false</parameter>
    <parameter name='hits.percentage.greater'>0.5</parameter>
  </action>
  <!-- actions here run only if hit scans finished and more than 50% hits have been found -->
</sequence>

Behavior Tree Conditions

This behavior adds these behavior tree conditions if behavior tree is present. If behavior has non-empty identifier replace hitScan with hitScan(id).

hitScan.check

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

ParameterValueDescription
hitScan.runningtrue, falseHit scans are running
hitScan.hitsintegerCount of hits equals integer value
hitScan.hits.notintegerCount of hits does not equal integer value
hitScan.hits.lessintegerCount of hits is less than integer value
hitScan.hits.greaterintegerCount of hits is greater than integer value
hitScan.hits.percentage.lessintegerPercentage of hits is less than value in the range from 0 to 1
hitScan.hits.percentage.greaterintegerPercentage of hits is greater than value in the range from 0 to 1

This is an example of using this condition:

<action name='myAction' id='doing something'>
  <parameter name='hitScan.running'>false</parameter>
  <parameter name='hitScan.hits.percentage.greater'>0.5</parameter>
  <condition>hitScan.check</condition>
</action>

State Machine Actions

State Machine Conditions

State Machine Events

This behavior sends these state machine events. If behavior has non-empty identifier replace hitScan with hitScan(id).

hitScan.done

Hit scan finished and the results can be examine now.

Required Behaviors

Optional Behaviors

Persistency

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

API Documentation

ECBehaviorHitScan.

Since DragonScript Module Version 1.0

Use Cases

  • Line of sight tests.
  • Test if a weapon hits a target.

Element Class Example

This example defines an element which contains a hit scan.

class MyElement extends BehaviorElementClass
  public var ECBehaviorComponent component
  public var ECBehaviorCollider collider
  public var ECBehaviorHitScan hitScan
  func new()
    component = ECBehaviorComponent.new(this, null)
    collider = ECBehaviorCollider.new(this, component)
    hitScan = ECBehaviorHitScan.new(this, collider, component)
    hitScan.getRange().setValue(50)
  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='ECBehaviorHitScan'>
    <!-- optional: use component with id instead of empty string -->
    <string name='component'>second</string>
 
    <!-- optional: set collision filter. default value '1:0 1 2' which means
                   category BaseGameApp.CollisionFilterBit.dynamic
                   filter BaseGameApp.CollisionFilterBit.geometry,
                          BaseGameApp.CollisionFilterBit.dynamic,
                          BaseGameApp.CollisionFilterBit.actor.
                   format is '', 'category' or 'category:filter' where category and filter
                   are a list of bits to set. -->
    <string name='collisionFilter'>1:0 1 2</string>
 
    <!-- 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='.range'>50</float>
  </behavior>
 
  <!-- for adding multiple behaviors use unique identifiers -->
  <behavior type='ECBehaviorHitScan' id='second'/>
</elementClass>

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_hitscan.txt · Last modified: 2025/05/04 13:40 by dragonlord