User Tools

Site Tools


dragengine:modules:dragonscript:behavior_lookedat

ECBehaviorLookedAt

Behavior element behavior adding support to track if actors look at element.

This behavior tracks if actors are looking at the element. If actors start looking at the element they are added to the list and listeners are notified. If the actors stop looking at the element they are removed from the list and listeners are notified. The actors can be an NPC or the player.

The behavior itself is passive. It requires to be triggered by the actor elements. The ECBehaviorLookAt behavior provides this functionality.

Instance Counts

This behavior can be used only once on an element. The behavior always has identifier empty string.

Element Class Properties

Element class properties have the prefix lookedAt..

Events

This behavior supports adding listeners. These events can be received:

actorStartsLookingAt

Actors starts looking at object.

actorStopsLookingAt

Actors stops looking at object.

Behavior Tree Actions

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

lookedAt.check

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

ParameterValueDescription
countintegerCount of actors looking at element equals integer value
count.notintegerCount of actors looking at element does not equal integer value
count.lessintegerCount of actors looking at element is less than integer value
count.greaterintegerCount of actors looking at element is greater than integer value
playertrue, falsePlayer is looking at element
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='lookedAt.check'>
    <parameter name='player'>true</parameter>
  </action>
  <!-- actions here run only if element is looked at by player -->
</sequence>

Behavior Tree Conditions

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

lookedAt.check

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

ParameterValueDescription
lookedAt.countintegerCount of actors looking at element equals integer value
lookedAt.count.notintegerCount of actors looking at element does not equal integer value
lookedAt.count.lessintegerCount of actors looking at element is less than integer value
lookedAt.count.greaterintegerCount of actors looking at element is greater than integer value
lookedAt.playertrue, falsePlayer is looking at element

This is an example of using this condition:

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

State Machine Actions

State Machine Conditions

State Machine Events

This behavior sends these state machine events.

lookedAt.start

Actors starts looking at object.

lookedAt.stop

Actors stops looking at object.

Required Behaviors

This behavior does not required other behaviors to be present.

Optional Behaviors

Persistency

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

API Documentation

ECBehaviorLookedAt.

Since DragonScript Module Version 1.1

Use Cases

  • Show outline and interact prompts if player looks at object.
  • For NPCs to react to player (or other actors) looking at them (for example looking back at player).
  • Show target information if player aims at objects.

Element Class Example

class ExampleElementClass extends BehaviorElementClass
   public var ECBehaviorLookedAt lookedAt
   
   public func new() super("ExampleElement")
     // add looked at behavior. by itself this does nothing yet. add listeners to the
     // behavior to react to looking-at changes. this is typically done by other
     // behaviors to provide their functionality
     lookedAt = ECBehaviorLookedAt.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='ECBehaviorLookedAt'>
    <!-- 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 -->
    <string name='.name'>value</string>
  </behavior>
</elementClass>

Live Examples

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