Drag[en]gine Script Module DragonScript  1.23
Dragengine.Scenery.ECBehaviorStateMachine Class Reference

Behavior element behavior adding StateMachine support. More...

Inheritance diagram for Dragengine.Scenery.ECBehaviorStateMachine:
Dragengine.Scenery.DefaultECBehavior Dragengine.Scenery.ECBehavior Dragengine.Scenery.BehaviorCompatiblePersistency

Classes

interface  ActionConditionFactory
 Factory assigining actions and conditions to state machine. More...
 
class  BlockActionConditionFactory
 Factory assigining actions and conditions to state machine using a block. More...
 
class  Instance
 Behavior instance. More...
 

Public Member Functions

void addActionConditionFactory (ActionConditionFactory factory)
 Add action and condition factory. More...
 
void addActionConditionFactory (Block factory)
 Add action and condition factory using block receiving Instance as parameter. More...
 
ECBehaviorInstance createInstance (BehaviorElement element)
 Create Behavior instance. More...
 
void dispose ()
 Dispose of behavior. More...
 
void forEachActionConditionFactory (Block ablock)
 Visit listener factories with block with argument ActionConditionFactory. More...
 
ECPStateMachine getStateMachine ()
 State machine. More...
 
Instance instance (BehaviorElement element)
 Get instance in element from owner element class. More...
 
ECBehaviorStateMachine new (BehaviorElementClass eclass)
 Create behavior element class. More...
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, Loaders loaders)
 Create behavior element class. More...
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, Loaders loaders, String id)
 Create behavior element class. More...
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, Loaders loaders, String id, String subID)
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, Loaders loaders, String id, String subID, String prefix)
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, String id)
 Create behavior element class. More...
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, String id, String prefix)
 Create behavior element class. More...
 
- Public Member Functions inherited from Dragengine.Scenery.DefaultECBehavior
void assignInstanceIndex (int instanceIndex)
 Assign instance index. More...
 
String getBehaviorID ()
 Unique identifier of behavior. More...
 
String getID ()
 Identifier. More...
 
int getInstanceIndex ()
 Instance index. More...
 
void loadSupportedData (String identifier, PersistencyEnvironment env, FileReader reader, ECBehaviorInstance instance)
 Load instance data of another behavior. More...
 
bool supportsBehaviorID (String identifier)
 Behavior supports loading instance data of another behavior. More...
 

Static Public Member Functions

static Array getAllBehaviorsIn (BehaviorElementClass eclass)
 Get list of all behaviors in element. More...
 
static Array getAllInstancesIn (BehaviorElement element)
 Get list of all instances in element. More...
 
static ECBehaviorStateMachine getBehaviorIn (BehaviorElementClass eclass)
 Get behavior in element class or null if absent. More...
 
static ECBehaviorStateMachine getBehaviorIn (BehaviorElementClass eclass, String id)
 Get behavior with id in element class or null if absent. More...
 
static Instance getInstanceIn (BehaviorElement element)
 Get instance in element or null if absent. More...
 
static Instance getInstanceIn (BehaviorElement element, String id)
 Get instance with id in element or null if absent. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Dragengine.Scenery.DefaultECBehavior
void setBehaviorID (String identifier)
 Set behavior identifier. More...
 
void useClassNameAsBehaviorID ()
 Set behavior identifier to "<class-name>:<identifier>". More...
 
void useFullyQualifiedClassNameAsBehaviorID ()
 Set behavior identifier to "<fully-qualified-class-name>:<identifier>". More...
 

Detailed Description

Behavior element behavior adding StateMachine support.

Loads state machine from file and creates a SMContext. Other behaviors can add actions and conditions to this state machine allowing to compose state machine actions and conditions easily.

This behavior can be used in different ways depending on the needs.

Single State Machine Usage

This is the most simple solution where only one state machine is used. For many situations this is enough and is the most simple solution to use. Add one ECBehaviorStateMachine with no ID to the element and as many other behaviors adding actions and conditions as required. Set the "stateMachine.path" property to define the state machine to load. Then use behavior listeners to run events on the state machine at the appropriate time. This is an example code for such a setup:

class MyElementClass extends BehaviorElementClass
public func new()
var ECBehaviorStateMachine smbehavior = ECBehaviorStateMachine.new(this)
smbehavior.getPath().setValue("/content/myBehavior.desm")
// here you can add now behaviors assigining actions and conditions for
// the state machine to use
...
// add listeners to behaviors to send events to the state machine
var ECBehaviorTwoStateAnimated animated = ...
animated.addActionConditionFactory(MyEventListener.Factory.new(smbehavior))
end
// two state listeners used to send events
class MyEventListener extends ECBehaviorTwoStateAnimated.DefaultListener
class Factory implements ECBehaviorTwoStateAnimated.ActionConditionFactory
public var ECBehaviorStateMachine stateMachine
public func new(ECBehaviorStateMachine stateMachine)
this.stateMachine = stateMachine
end
public func ECBehaviorTwoStateAnimated.Listener createListener(ECBehaviorTwoStateAnimated.Instance instance)
return MyEventListener.new(stateMachine.instance(instance.getElement()))
end
end
public var ECBehaviorStateMachine.Instance stateMachine
public func new(ECBehaviorStateMachine.Instance stateMachine)
this.stateMachine = stateMachine
end
public func void stopActivate(ECBehaviorTwoStateAnimated.Instance instance)
stateMachine.runEvent("animated activated")
end
public func void stopDeactivate(ECBehaviorTwoStateAnimated.Instance instance)
stateMachine.runEvent("animated deactivated")
end
end
end
void addActionConditionFactory(ActionConditionFactory factory)
Add action and condition factory.
ECBehaviorStateMachine new(BehaviorElementClass eclass)
Create behavior element class.
Multiple State Machine Usage

You can add multiple instances of ECBehaviorStateMachine to the element to load and use multiple state machines. This can be for using multiple state machines together to model complex logic. In this situation behaviors assigining actions and conditions do add them to all instances of ECBehaviorStateMachine. This is usually not a problem since the state machines are written to use only the actions and conditions making sense to be used. This is an example code for such a setup:

class MyElementClass extends BehaviorElementClass
public func new()
var ECBehaviorStateMachine smSpecific = ECBehaviorStateMachine.new(this, "specific")
smSpecific.getPath().setValue("/content/mySpecificBehavior.desm")
var ECBehaviorStateMachine smGeneric = ECBehaviorStateMachine.new(this, "idle")
smGeneric.getPath().setValue("/content/myGenericBehavior.desm")
// here you can add now behaviors assigining actions and conditions for both
// state machines to use
...
// add listeners to behaviors to send events to the state machine. the same
// MyEventListener is used as in the single state machine example. the example
// here uses two animated behaviors each sending events to one of the state
// machines. in general using multiple state machines the events should be
// disjoint but it is fully valid to send the same event to both if the
// state machines are written to deal with this properly.
var ECBehaviorTwoStateAnimated animated1 = ...
animated1.addActionConditionFactory(MyEventListener.Factory.new(smSpecific))
var ECBehaviorTwoStateAnimated animated2 = ...
animated2.addActionConditionFactory(MyEventListener.Factory.new(smGeneric))
end
end

The above example loads and runs two state machines. The first one is running specific state machine. The second state machine provides two separate state machines able to handle multiple logic at the same time. By writing the state machines properly the two state machienes do not interfere with each other and allow to split up state machines into multiple ones which can help create complex logic more simple and stable.

Action and Condition Factory

You can assign actions and conditions without using other behaviors by using one or more factories. The factories are added to the behavior. Upon creating the behavior instances the factories are asked to create and assign SMAction and SMCondition. The example below shows how to use a factory to add a condition checking for a trigger and an action manipulating a trigger.

class MyElementClass extends BehaviorElementClass
public func new()
var ECBehaviorStateMachine smbehavior = ECBehaviorStateMachine.new(this)
smbehavior.getPath().setValue("/content/mySpecificBehavior.desm")
// add factory to create actions and conditions later on. this example uses
// a code block for simplicity. using classes implementing ActionConditionFactory
// is preferred since it allows to reuse factories across element classes.
smbehavior.setActionConditionFactory( block ECBehaviorStateMachine.Instance instance
// add condition evaluating to true if "player.insideZone" trigger is in fired state.
// the condition can be used in the state machine using the name "player is inside zone".
instance.setCondition("player is inside zone", SMConditionTrigger.new( \
"player.insideZone", SMConditionTrigger.TestMode.fired))
// add action pulsing (fire and immediately reset) "player.damage" trigger.
// the action can be run in the state machine using the name "hurt playe".
instance.setAction("hurt player", SMActionTrigger.new("player.damage", SMActionTrigger.Action.pulse))
end )
end
end

This behavior does require the element class to be persistable (setPersistable) only if the context is run by the behavior itself.

Member Function Documentation

◆ addActionConditionFactory() [1/2]

void Dragengine.Scenery.ECBehaviorStateMachine.addActionConditionFactory ( ActionConditionFactory  factory)

Add action and condition factory.

◆ addActionConditionFactory() [2/2]

void Dragengine.Scenery.ECBehaviorStateMachine.addActionConditionFactory ( Block  factory)

Add action and condition factory using block receiving Instance as parameter.

This is a convenience method for using BlockActionConditionFactory.

◆ createInstance()

ECBehaviorInstance Dragengine.Scenery.ECBehaviorStateMachine.createInstance ( BehaviorElement  element)

Create Behavior instance.

Implements Dragengine.Scenery.ECBehavior.

◆ dispose()

void Dragengine.Scenery.ECBehaviorStateMachine.dispose ( )

Dispose of behavior.

Reimplemented from Dragengine.Scenery.DefaultECBehavior.

◆ forEachActionConditionFactory()

void Dragengine.Scenery.ECBehaviorStateMachine.forEachActionConditionFactory ( Block  ablock)

Visit listener factories with block with argument ActionConditionFactory.

◆ getAllBehaviorsIn()

static Array Dragengine.Scenery.ECBehaviorStateMachine.getAllBehaviorsIn ( BehaviorElementClass  eclass)
static

Get list of all behaviors in element.

Returns list contains ECBehaviorStateMachine behaviors in the order they have been created in the element.

◆ getAllInstancesIn()

static Array Dragengine.Scenery.ECBehaviorStateMachine.getAllInstancesIn ( BehaviorElement  element)
static

Get list of all instances in element.

Returns list contains ECBehaviorStateMachine.Instance instances in the order they have been created in the element.

◆ getBehaviorIn() [1/2]

static ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.getBehaviorIn ( BehaviorElementClass  eclass)
static

Get behavior in element class or null if absent.

Use this method to check if a particular BehaviorElementClass contains a behavior of type ECBehaviorCollider.

◆ getBehaviorIn() [2/2]

static ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.getBehaviorIn ( BehaviorElementClass  eclass,
String  id 
)
static

Get behavior with id in element class or null if absent.

Use this method to check if a particular BehaviorElementClass contains a behavior of type ECBehaviorStateMachine with specific identifier.

◆ getInstanceIn() [1/2]

static Instance Dragengine.Scenery.ECBehaviorStateMachine.getInstanceIn ( BehaviorElement  element)
static

Get instance in element or null if absent.

Use this method to check if a particular BehaviorElement contains a behavior instance of type ECBehaviorStateMachine.Instance . If more than one instance is present returns the first instance.

◆ getInstanceIn() [2/2]

static Instance Dragengine.Scenery.ECBehaviorStateMachine.getInstanceIn ( BehaviorElement  element,
String  id 
)
static

Get instance with id in element or null if absent.

Use this method to check if a particular BehaviorElement contains a behavior instance of type ECBehaviorStateMachine.Instance with specific identifier.

◆ getStateMachine()

ECPStateMachine Dragengine.Scenery.ECBehaviorStateMachine.getStateMachine ( )

State machine.

◆ instance()

Instance Dragengine.Scenery.ECBehaviorStateMachine.instance ( BehaviorElement  element)

Get instance in element from owner element class.

◆ new() [1/7]

ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.new ( BehaviorElementClass  eclass)

Create behavior element class.

Reimplemented from Dragengine.Scenery.DefaultECBehavior.

◆ new() [2/7]

ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.new ( BehaviorElementClass  eclass,
Loaders  loaders 
)

Create behavior element class.

◆ new() [3/7]

ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.new ( BehaviorElementClass  eclass,
Loaders  loaders,
String  id 
)

Create behavior element class.

◆ new() [4/7]

ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.new ( BehaviorElementClass  eclass,
Loaders  loaders,
String  id,
String  subID 
)

◆ new() [5/7]

ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.new ( BehaviorElementClass  eclass,
Loaders  loaders,
String  id,
String  subID,
String  prefix 
)

◆ new() [6/7]

ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.new ( BehaviorElementClass  eclass,
String  id 
)

Create behavior element class.

Reimplemented from Dragengine.Scenery.DefaultECBehavior.

◆ new() [7/7]

ECBehaviorStateMachine Dragengine.Scenery.ECBehaviorStateMachine.new ( BehaviorElementClass  eclass,
String  id,
String  prefix 
)

Create behavior element class.


The documentation for this class was generated from the following file: