User Tools

Site Tools


dragengine:modules:dragonscript:behavior_skinswitcher

ECBehaviorSkinSwitcher

Behavior element behavior adding support to switch component skins.

If a texture name is specified the matching component texture skin is switched instead of switching the skin for the entire component.

Switching skins can be used for example with monitors which show an offline skin and if used by the player switch to one or more online skins. Another use case is to create damageable objects which change skin the more they are damaged.

To use thie behavior assign one or more skins to it. The first skin is used as the starting skin. By calling setActive() the active skin can be selected using the index of the skin in the list of defined skins. Furthermore activateNext() and activatePrevious() can be used to cycle through skins.

Optionally this behavior can be attached to various other behaviors supporting BooleanBehaviorListener. By doing so cycling though skins is attached to listener events called by other behaviors.

Skin switcher is set by default to wrap index around at the begin and end of the skin list. This allows to indefinitely cycle through all skins. By setting the “clamp” element class property to true the skins can cycle only once not wrapping around.

Instance Counts

Multiple instances of ECBehaviorSkinSwitcher can be used for example to switch skins of multiple component textures. Use the behavior identifier to tell them apart.

Element Class Properties

Element class properties have the prefix skinSwitcher. or skinSwitcher(id). if id is not empty.

texture

Name of texture in component to switch skin. If empty string switch skin of component itself.

  • Full name: skinSwitcher.texture or skinSwitcher(id).texture
  • Type: string
  • Default Value: empty string
  • Example (*.deeclass):
    <string name='skinSwitcher.texture'>locked</string>

skins

List of skins to cycle through. At least one skin is required to be defined for this property to be valid.

  • Full name: skinSwitcher.skins or skinSwitcher(id).skins
  • Type: List of string
  • Default value: empty list
  • File Pattern: *.deskin (all skin modules)
  • Example (*.deeclass):
    <list name='skinSwitcher.skins'>
      <string>/content/skins/locked.deskin</string>
      <string>/content/skins/unlocked.deskin</string>
    </list>

clamp

If the skin index reaches either end of the list it wraps around to the other end (from last to first and vice versa). If clamp is true wrapping around is disabled keeping the index at the last respectively first index.

  • Full name: skinSwitcher.clamp or skinSwitcher(id).clamp
  • Type: boolean
  • Default value: false
  • Example (*.deeclass):
    <boolean name='skinSwitcher.clamp'>true</boolean>

activeSkin

Index of first skin from skins property to show. Index has to be in the range from 0 to skins.count-1.

  • Full name: skinSwitcher.activeSkin or skinSwitcher(id).activeSkin
  • Type: integer
  • Default Value: 0
  • Example (*.deeclass):
    <integer name='skinSwitcher.activeSkin'>1</integer>

Behavior Tree Actions

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

skinSwitcher.set

Set one or more skin switcher parameters.

ParameterValueDescription
activenext, previous, first, last, integerSet active skin to next, previous, first, last or index

This is an example of using this action:

<action name='skinSwitcher.set'>
  <parameter name='active'>next</parameter>
</action>

skinSwitcher.check

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

ParameterValueDescription
activefirst, last, integerActive skin is the first, last or matches index
active.notfirst, last, integerActive skin is not the first, last or matches index
active.lessintegerIndex of active skin is less than value
active.greaterintegerIndex of active skin is greater than value
countintegerSkin count matches value
count.lessintegerSkin count is less than value
count.greaterintegerSkin count is greater than value
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='skinSwitcher.check'>
    <parameter name='active'>last</parameter>
  </action>
  <!-- actions here run only if skin switcher is enabled -->
</sequence>

Behavior Tree Conditions

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

skinSwitcher.check

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

ParameterValueDescription
skinSwitcher.activefirst, last, integerActive skin is the first, last or matches index
skinSwitcher.active.notfirst, last, integerActive skin is not the first, last or matches index
skinSwitcher.active.lessintegerIndex of active skin is less than value
skinSwitcher.active.greaterintegerIndex of active skin is greater than value
skinSwitcher.countintegerSkin count matches value
skinSwitcher.count.lessintegerSkin count is less than value
skinSwitcher.count.greaterintegerSkin count is greater than value

This is an example of using this condition:

<action name='myAction' id='doing something'>
  <parameter name='skinSwitcher.active'>last</parameter>
  <condition>skinSwitcher.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 skinSwitcher with skinSwitcher(id).

skinSwitcher.changed

Active skin changed.

Required Behaviors

Optional Behaviors

Persistency

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

API Documentation

ECBehaviorSkinSwitcher.

Since DragonScript Module Version 1.3

Use Cases

  • Create monitors which show an offline skin and if used by the player switch to one or more online skins.
  • Create damageable objects which change skin the more they are damaged.
  • Create information panels for puzzles showing the player the current state he needs to solve.

Element Class Example

The example below switches the skin of a texture if the animated state become activate:

class MyElement extends BehaviorElementClass
   public var ECBehaviorComponent component
   public var ECBehaviorCollider collider
   public var ECBehaviorTwoStateAnimated animated
   public var ECBehaviorSkinSwitcher skinSwitcher
   
   public func new()
      // Create component to switch texture
      component = ECBehaviorComponent.new(this)
      
      collider = ECBehaviorCollider.new(this, component)
      
      // Create animated behavior which is used as trigger for the skin switcher
      animated = ECBehaviorTwoStateAnimated.new(this, component)
      
      // Create skin switcher for texture "display". If empty string is used instead
      // the skin is switched for the entire component
      skinSwitcher = ECBehaviorSkinSwitcher.new(this, component)
      skinSwitcher.getTexture().setValue("display")
      
      // Add skins to load. This example uses an "off" and an "on" skin. Other examples
      // could use multiple skins to cycle through. For switching multiple skins more
      // complex logic is usually required so it is better if you add your own listeners
      // do the appropriate switching. The provided listeners are suited for the simple
      // case of switching between two skins
      skinSwitcher.getSkins().add("/content/skins/displayOff.deskin")
      skinSwitcher.getSkins().add("/content/skins/displayOn.deskin")
      
      // Add listener factory to ECBehaviorTwoStateAnimated. This makes the skin switch
      // whenever the animated behavior enters or leaves "activated" state. This is only
      // one possible way to connect to the ECBehaviorTwoStateAnimated but a common one.
      skinSwitcher.listen(animated)
   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='ECBehaviorTwoStateAnimated'/>
 
  <behavior type='ECBehaviorSkinSwitcher'>
    <!-- optional: use component with id instead of empty string -->
    <string name='component'>second</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 -->
    <string name='.texture'>locked</string>
  </behavior>
 
  <!-- for adding multiple behaviors use unique identifiers -->
  <behavior type='ECBehaviorSkinSwitcher' id='second'/>
</elementClass>

Live Examples

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