User Tools

Site Tools


dragengine:modules:dragonscript:behavior_composed

ECBehaviorComposed

Behavior for element allowing to create and add child elements.

This behavior is useful to create element classes which automatically create and attach child elements without using the world editor.

This behavior defines a list of element classes to use. For each element class an optional list of property values can be defined that are used to create an instance of this class.

Instance Counts

This behavior can be used only once on an element.

Element Class Properties

Element class properties have the prefix composed..

children

Set child elements to create. This is a list of map entries for each child element. Children are created in the order they are defined. The map entry requires at least a string value with name class indicating the element class to use. Optionally a list value with name properties can be used containing stub element properties to use for creating the element. Values in this list are string values where the name is the property key and the string value is the property value.

  • Full name: composed.children
  • Type: list
  • Default Value: empty list
  • Example (*.deeclass)

    <list name='composed.children'>
      <map>
        <string key='class'>Backpack</string>
        <list key='properties'>
          <string key='attachToParent.attachSlot'>backpack</string>
        </map>
      </map>
    </list>

Events

This behavior has no events.

Required Behaviors

This behavior requires no other behaviors.

Optional Behaviors

This behavior does not support optional behaviors.

Persistency

This behavior does support element class to be persistable (setPersistable). Saves if children elements have been already created.

API Documentation

ECBehaviorComposed.

Since DragonScript Module Version 1.0

Use Cases

  • Add child elements to actor on creating it which the player can acquire, or break off.
  • Create complex elements composed of different elements with little work.

Element Class Example

This example defines an element which contains a single child element which is a backpack.

class MyElement extends BehaviorElementClass
  public var ECBehaviorComponent component
  public var ECBehaviorCollider collider
  public var ECBehaviorComposed composed
  func new()
    // add component which contains a rig with the bone 'backpack'
    component = ECBehaviorComponent.new(this, null)
    
    // add collider to have something to attach children to
    collider = ECBehaviorCollider.new(this, component)
    
    // add composed behavior and add a child definition to it
    composed = ECBehaviorComposed.new(this)
    
    var Dictionary child = Dictionary.new()
    child.setAt("class", "MyBackpackClass")
    
    var Dictionary properties = Dictionary.new()
    properties.setAt("attachToParent.attachSlot", "backpack")
    child.setAt("properties", properties)
    
    composed.getChildren().add(child)
  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'>
  <!-- add component which contains a rig with the bone 'backpack' -->
  <behavior type='ECBehaviorComponent'/>
 
  <!-- add collider to attach children to -->
  <behavior type='ECBehaviorCollider'/>
 
  <!-- add composed behavior -->
  <behavior type='ECBehaviorComposed'>
    <!-- set element properties. omit property prefix if used inside behavior tag -->
    <list name='.children'>
      <!-- define a backpack element to be added -->
      <map>
        <!-- the class name of the element to add -->
        <string key='class'>Backpack</string>
 
        <!-- list of element properties to set for the added element -->
        <list key='properties'>
          <!-- attach element to component bone named 'backpack' -->
          <string key='attachToParent.attachSlot'>backpack</string>
        </map>
      </map>
    </list>
  </behavior>
 
  <!-- for adding multiple behaviors use unique identifiers -->
  <behavior type='ECBehaviorComposed' id='second'/>
</elementClass>

Live Examples

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