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. To use multiple instances use code like this in your subclass constructor:

class MyElement extends BehaviorElementClass
   public var ECBehaviorComponent component
   public var ECBehaviorCollider collider
   public var ECBehaviorTwoStateAnimated animated
   public var ECBehaviorSkinSwitcher skinSwitcher
   public var ECBehaviorSkinSwitcher subSkinSwitcher
   
   public func new()
      component = ECBehaviorComponent.new(this)
      collider = ECBehaviorCollider.new(this, component)
      animated = ECBehaviorTwoStateAnimated.new(this, component)
      
      skinSwitcher = ECBehaviorSkinSwitcher.new(this, component)
      skinSwitcher.getTexture().setValue("display")
      skinSwitcher.getSkins().add("/content/skins/displayOff.deskin")
      skinSwitcher.getSkins().add("/content/skins/displayOn.deskin")
      skinSwitcher.listen(animated)
      
      // Add a second skin switcher affecting a different texture. This example attaches
      // to the same animated behavior but you could instead attach to a different one.
      subSkinSwitcher = ECBehaviorSkinSwitcher.new(this, component, "subSkinSwitcher)
      subSkinSwitcher.getTexture().setValue("subDisplay")
      subSkinSwitcher.getSkins().add("/content/skins/subDisplayOff.deskin")
      subSkinSwitcher.getSkins().add("/content/skins/subDisplayOn.deskin")
      subSkinSwitcher.listen(animated)
   end
end

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 (for example *.deskin) 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
  • 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>

Required Behaviors

Optional Behaviors

This behavior does not support optional behaviors.

Persistency

This behavior does support element class to be persistable (setPersistable). Saves the active skin index.

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
You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_skinswitcher.txt · Last modified: 2024/03/14 16:51 by dragonlord