User Tools

Site Tools


dragengine:modules:dragonscript:behavior_playerlookatinteractprompt

ECBehaviorPlayerLookAtInteractPrompt

Example usage of interact prompt behavior.

Behavior element behavior adding look-at interact prompt support to player actor.

If the actor is the player controlled actor creates a UI panel overlayed on the HUD showing interaction prompts. If the actor is not the active player actor destroys the UI panel.

If the actor looks an an element supporting ECBehaviorInteractPrompt the UI panel is shown and updated with the prompt information. Otherwise the UI panel is cleared and hidden.

The widget is shown inside a container Panel created by the behavior. The default layout of the panel is CentrizedBoxLayout. Change the layout instance to use your own.

While player is the active actor this behavior polls every frame update for changes in prompts and updates the prompt display. This ensures dynamic changes on prompts can be detected. While the player is not the active actor this behavior disables per-frame updates to not waste performance.

If the look-at behavior is disabled this behavior is showing no interact prompts. If is though possible to temporarily disabled this behavior no matter if the look-at behavior is enabled or disabled. This allows to hide interact prompts for example if the player is performing an action where he is not able to interact with anything, for example being in a conversation or playing a cutscene.

This behavior is used for player actors only.

Prompt Widget

To display the prompt a prompt widget is required. This widget is created at the appropriate time by the behavior. For this to work you have to assign a Prompt Controller Factory while adding the behavior. THe Prompt Controller Factory is an interface knowing how to create an instance of a Prompt Controller. The Prompt Controller itself knows how to create the prompt widget, how to update the widget to reflect the visible prompts as well as how to apply transparency and visibility changes.

The PanelInteractPromptController is the default prompt controller provided by the DragonScript module. It creates and manages an instance of PanelInteractPrompt which shows a horizontal list of prompts centered in the middle of the parent container. By subclassing PanelInteractPromptController you can change what prompt widget is created.

See Element Class Example for example code of a prompt controller.

Container Widget

You have to specify where the prompt controller created prompt widget has to be shown in. Requires a Container instance with an optional layout settings instance. Layout settings instances are only required for certain layouts like BorderLayout. In general this can be null.

BaseGameApp.getApp().getWindowGameWorld() is suitable as container UI since it uses StackLayout which works well with PanelInteractPrompt. You can though use any subclass of Container as long as you can provide the container instances early enough.

Behavior Factory

The behavior factory script class is an abstract class. To use this behavior create a factory subclass and implement these functions:

FunctionDescription
createPromptControllerFactoryCreate prompt controller factory.
getUiContainerGet UI container.
getUiContainerAddSettingsGet UI container add settings.

This is required since these functions are so game specific that no sane default value can be provided.

Instance Counts

This behavior can be used only once on an element.

Element Class Properties

Element class properties have the prefix playerLookAtInteractPrompt. .

bindingIconHeight

Size in pixels to use for bindings to display. This is used to pick from the Input Module the device button or axis display image with the best matching size.

  • Full name: playerLookAtInteractPrompt.bindingIconHeight
  • Type: integer
  • Default Value: 32
  • Restrictions: At least 1
  • Example (*.deeclass):
    <integer name='playerLookAtInteractPrompt.bindingIconHeight'>32</integer>

transparency

Transparency of the prompt widget. The used prompt controller decides how the transparency is applied. The default prompt widget applies the transparency to the panel containing all displayed prompts.

  • Full name: playerLookAtInteractPrompt.transparency
  • Type: integer
  • Default Value: 1
  • Restrictions: At least 0 and at most 1
  • Example (*.deeclass):
    <float name='playerLookAtInteractPrompt.transparency'>0.75</float>

visibleIfEmpty

Show prompt widget if no prompts are present.

  • Full name: playerLookAtInteractPrompt.visibleIfEmpty
  • Type: boolean
  • Default Value: false
  • Example (*.deeclass):
    <boolean name='playerLookAtInteractPrompt.visibleIfEmpty'>true</boolean>

Events

This behavior has no events.

Behavior Tree Actions

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

playerLookAtInteractPrompt.set

Set one or more player look at interact prompt parameters.

ParameterValueDescription
enabledtrue, falseEnable player look at interact prompt

This is an example of using this action:

<action name='playerLookAtInteractPrompt.set'>
  <parameter name='enabled'>true</parameter>
</action>

playerLookAtInteractPrompt.check

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

ParameterValueDescription
enabledtrue, falsePlayer look at interact prompt is enabled or not
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='playerLookAtInteractPrompt.check'>
    <parameter name='enabled'>true</parameter>
  </action>
  <!-- actions here run only if player look at interact prompt is enabled -->
</sequence>

Behavior Tree Conditions

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

playerLookAtInteractPrompt.check

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

ParameterValueDescription
playerLookAtInteractPrompt.enabledtrue, falsePlayer look at interact prompt is enabled or not

This is an example of using this condition:

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

State Machine Actions

State Machine Conditions

State Machine Events

This behavior send no events to state machine.

Required Behaviors

Optional Behaviors

Persistency

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

API Documentation

ECBehaviorInteractPrompt.

Since DragonScript Module Version 1.1

Use Cases

Element Class Example

class ObjectElementClass extends BehaviorElementClass
   public var ECBehaviorInteractPrompt interactPrompt
   
   public func new() super("ExampleObject")
     // create interact prompt behavior. shows an icon with the verb "Interact" underneath.
     // the player has to use the input he assigned to the "primaryAction" command.
     // also the prompt is only shown if the player looks at the bone named "bigBadButton".
     interactPrompt = ECBehaviorInteractPrompt.new(this)
     interactPrompt.getImage().setPath("interact.png")
     interactPrompt.getVerb().setValue(UnicodeString.newFromUTF8("Interact"))
     interactPrompt.getCommand().setValue("primaryAction")
     interactPrompt.getBones().add("bigBadButton")
   end
end

class PlayerElementClass extends BaseActorClass
   public var ECBehaviorLookAt lookAt
   public var ECBehaviorPlayerLookAtInteractPrompt playerLookAtInteractPrompt
   
   public func new() super("PlayerActor")
     // allow actor to figure out what the player is looking at
     lookAt = ECBehaviorLookAt.new(this)
     
     // show an interact prompt widget on screen. the behavior requires a couple of
     // parameters to work properly.
     // 
     // you have to create a controller factory instance which allows the behavior
     // to create the prompt widget.
     // 
     // then you also have to specify where the newly created prompt widget will be
     // added to. in this example the panel is added to the current WindowGameWorld.
     // this window uses a StackLayout which works best with PanelInteractPrompt.
     // the last parameter is the object layout settings to use while adding the
     // widget to the container. this is only required to be non-null if you use
     // layouts requiring extra information, for example BorderLayout.
     playerLookAtInteractPrompt = ECBehaviorPlayerLookAtInteractPrompt.new(this, \
        getPlayerControlled(), lookAt, PanelInteractPromptController.ControllerFactory.new(), \
        BaseGameApp.getApp().getWindowGameWorld(), null)
   end
end

Example of a prompt controller:

// create prompt controller based on PanelInteractPromptController. this is the
// fastest way to get started. otherwise you can implement
// ECBehaviorPlayerLookAtInteractPrompt.PromptController to customize everything
class CustomPromptController extends PanelInteractPromptController
  // factory creating instances of CustomPromptController
  class ControllerFactory implements ECBehaviorPlayerLookAtInteractPrompt.PromptControllerFactory
    // create instance of prompt controller
    public func new()
    end
    
    // create prompt controller
    public func ECBehaviorPlayerLookAtInteractPrompt.PromptController \
    createPromptController(ECBehaviorPlayerLookAtInteractPrompt.Instance instance)
      return CustomPromptController.new(instance.getECBehavior().getBindingManager(), instance.getBindingIconHeight())
    end
  end
  
  // create prompt controller instance
  public func new(BindingManager bindingManager, int bindingIconHeight) super(bindingManager, bindingIconHeight)
  end
  
  // create prompt widget. this solution here just changes how the prompt panel
  // content is created to place the prompt underneath the screen center. this is
  // a typical solution for first-person handling. another solution would be to
  // use BorderLayout to place the prompts at the top or bottom of the screen
  protected func void createContent(PanelInteractPrompt panelInteractPrompt)
    this.runWhileBlockingLayout(block
      // split screen into 2 equally sized cells along the y axis
      setLayout(GridLayout.new(1, 2)) // columns=1, rows=2
      
      // put a filler widget in upper half
      addWidget(Widget.new())
      
      // add prompt panel widget to a centrized box layout in the lower half.
      // the 0.5 centers the box horizontally while 0 places the box at the top.
      addWidget(Panel.new(CentrizedBoxLayout.new(LayoutAxis.x, 0.5, 0), block Panel p
        p.addWidget(panelInteractPrompt)
      end))
    end)
    
    // runWhileBlockingLayout blocks layout calls for performance reasons
    doLayoutIfBlocked()
  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='ECBehaviorPlayerLookAtInteractPrompt'>
    <!-- optional: use BaseGameApp command manager. game can add more supported values.
                   default is 'default' -->
    <string name='commandManager'>default</string>
 
    <!-- optional: use BaseGameApp binding manager. game can add more supported values.
                   default is 'default' -->
    <string name='bindingManager'>default</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 -->
    <integer name='.bindingIconHeight'>32</integer>
  </behavior>
</elementClass>

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_playerlookatinteractprompt.txt · Last modified: 2025/05/07 14:30 by dragonlord