Drag[en]gine Script Module DragonScript 1.32.1
Loading...
Searching...
No Matches
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  Factory
 Behavior factory. More...
 
class  Instance
 Behavior instance. More...
 

Public Member Functions

void addActionConditionFactory (ActionConditionFactory factory)
 Add action and condition factory.
 
void addActionConditionFactory (Block factory)
 Add action and condition factory using block receiving Instance as parameter.
 
void dispose ()
 Dispose of behavior.
 
void forEachActionConditionFactory (Block ablock)
 Visit listener factories with block with argument ActionConditionFactory.
 
ECBehaviorBehaviorTree getBehaviorTree ()
 Behavior tree or null.
 
ECPString getDebug ()
 Debug mode prefix.
 
String getPrefix ()
 Prefix.
 
ECPStateMachine getStateMachine ()
 State machine.
 
ECBehaviorStateMachine new (BehaviorElementClass eclass)
 Create behavior element class.
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, Loaders loaders)
 Create behavior element class.
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, Loaders loaders, String id)
 Create behavior element class.
 
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.
 
ECBehaviorStateMachine new (BehaviorElementClass eclass, String id, String prefix)
 Create behavior element class.
 
void setActionsConditions (Instance instance)
 Assign actions and conditions to behavior instance.
 
void setBehaviorTree (ECBehaviorBehaviorTree behaviorTree)
 Set behavior tree or null.
 
- Public Member Functions inherited from Dragengine.Scenery.DefaultECBehavior
void assignInstanceIndex (int instanceIndex)
 Assign instance index.
 
String getBehaviorID ()
 Unique identifier of behavior.
 
String getID ()
 Identifier.
 
int getInstanceIndex ()
 Instance index.
 
void loadSupportedData (String identifier, PersistencyEnvironment env, FileReader reader, ECBehaviorInstance instance)
 Load instance data of another behavior.
 
bool supportsBehaviorID (String identifier)
 Behavior supports loading instance data of another behavior.
 
- Public Member Functions inherited from Dragengine.Scenery.ECBehavior
ECBehaviorInstance createInstance (BehaviorElement element)
 Create Behavior instance.
 

Static Public Member Functions

static ECBehaviorStateMachine getBehaviorIn (BehaviorElementClass eclass)
 Get behavior in element class or null if absent.
 

Public Attributes

Array pActionConditionFactories
 
ECBehaviorBehaviorTree pBehaviorTree
 
ECPString pDebug
 
String pPrefix
 
ECPStateMachine pStateMachine
 

Additional Inherited Members

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

Detailed Description

Behavior element behavior adding StateMachine support.

Version
1.3

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
func new()
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
public var ECBehaviorStateMachine stateMachine
func new(ECBehaviorStateMachine stateMachine)
stateMachine = stateMachine
end
func ECBehaviorTwoStateAnimated.Listener createListener(ECBehaviorTwoStateAnimated.Instance instance)
return MyEventListener.new(stateMachine.instance(instance.getElement()))
end
end
public var ECBehaviorStateMachine.Instance stateMachine
func new(ECBehaviorStateMachine.Instance stateMachine)
stateMachine = stateMachine
end
func void stopActivate(ECBehaviorTwoStateAnimated.Instance instance)
stateMachine.runEvent("animated activated")
end
func void stopDeactivate(ECBehaviorTwoStateAnimated.Instance instance)
stateMachine.runEvent("animated deactivated")
end
end
end
Behavior element element class.
Definition BehaviorElementClass.ds:54
Behavior factory.
Definition ECBehaviorStateMachine.ds:220
Behavior instance.
Definition ECBehaviorStateMachine.ds:228
Behavior element behavior adding StateMachine support.
Definition ECBehaviorStateMachine.ds:180
Behavior element behavior adding two state animation support.
Definition ECBehaviorTwoStateAnimated.ds:109
Factory assigining actions and conditions to state machine.
Definition ECBehaviorStateMachine.ds:182
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
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
func new()
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.

See also
https://developer.dragondreams.ch/wiki/doku.php/dragengine:modules:dragonscript:behavior_statemachine

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.

◆ 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.

◆ getBehaviorIn()

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.

◆ getBehaviorTree()

ECBehaviorBehaviorTree Dragengine.Scenery.ECBehaviorStateMachine.getBehaviorTree ( )

Behavior tree or null.

Version
1.26

◆ getDebug()

ECPString Dragengine.Scenery.ECBehaviorStateMachine.getDebug ( )

Debug mode prefix.

Version
1.26

If not empty string enables debug mode with prefix to locate log messages easily.

◆ getPrefix()

String Dragengine.Scenery.ECBehaviorStateMachine.getPrefix ( )

Prefix.

Version
1.26

◆ getStateMachine()

ECPStateMachine Dragengine.Scenery.ECBehaviorStateMachine.getStateMachine ( )

State machine.

◆ 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.

◆ setActionsConditions()

void Dragengine.Scenery.ECBehaviorStateMachine.setActionsConditions ( Instance  instance)

Assign actions and conditions to behavior instance.

◆ setBehaviorTree()

void Dragengine.Scenery.ECBehaviorStateMachine.setBehaviorTree ( ECBehaviorBehaviorTree  behaviorTree)

Set behavior tree or null.

Version
1.26

Member Data Documentation

◆ pActionConditionFactories

Array Dragengine.Scenery.ECBehaviorStateMachine.pActionConditionFactories

◆ pBehaviorTree

ECBehaviorBehaviorTree Dragengine.Scenery.ECBehaviorStateMachine.pBehaviorTree

◆ pDebug

ECPString Dragengine.Scenery.ECBehaviorStateMachine.pDebug

◆ pPrefix

String Dragengine.Scenery.ECBehaviorStateMachine.pPrefix

◆ pStateMachine

ECPStateMachine Dragengine.Scenery.ECBehaviorStateMachine.pStateMachine

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