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

Behavior element behavior adding BehaviorTree support. More...

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

Classes

interface  ActionConditionFactory
 Factory assigining actions and conditions to behavior tree. More...
 
class  BlockActionConditionFactory
 Factory assigining actions and conditions to behavior tree using a block. More...
 
class  BlockListenerFactory
 Factory creating listeners using block. More...
 
class  DefaultListener
 Default implementation of behavior instance listener. More...
 
class  Instance
 Behavior instance. More...
 
interface  Listener
 Listener for behavior instance events. More...
 
interface  ListenerFactory
 Factory creating listeners. 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...
 
void addListenerFactory (Block blockFactory)
 Add listener factory using block. More...
 
void createListeners (Instance instance)
 Create listeners from factories adding them to behavior instance. More...
 
void dispose ()
 Dispose of behavior. More...
 
void forEachActionConditionFactory (Block ablock)
 Visit listener factories with block with argument ActionConditionFactory. More...
 
void forEachListenerFactory (Block ablock)
 Visit listener factories with block with argument ListenerFactory. More...
 
ECPBehaviorTree getBehaviorTree ()
 Behavior tree. More...
 
ECPBoolean getRun ()
 Run behavior tree during element thinking. More...
 
ECBehaviorBehaviorTree new (BehaviorElementClass eclass)
 Create behavior element class. More...
 
ECBehaviorBehaviorTree new (BehaviorElementClass eclass, Loaders loaders)
 Create behavior element class. More...
 
ECBehaviorBehaviorTree new (BehaviorElementClass eclass, Loaders loaders, String id)
 Create behavior element class. More...
 
ECBehaviorBehaviorTree new (BehaviorElementClass eclass, Loaders loaders, String id, String subID)
 
ECBehaviorBehaviorTree new (BehaviorElementClass eclass, Loaders loaders, String id, String subID, String prefix)
 
ECBehaviorBehaviorTree new (BehaviorElementClass eclass, String id)
 Create behavior element class. More...
 
ECBehaviorBehaviorTree new (BehaviorElementClass eclass, String id, String prefix)
 Create behavior element class. More...
 
void setActionsConditions (Instance instance)
 Assign actions and conditions to behavior instance. 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...
 
- Public Member Functions inherited from Dragengine.Scenery.ECBehavior
ECBehaviorInstance createInstance (BehaviorElement element)
 Create Behavior instance. More...
 

Static Public Member Functions

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

Public Attributes

Array pActionConditionFactories
 
ECPBehaviorTree pBehaviorTree
 
Array pListenerFactories
 
ECPBoolean pRun
 

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 BehaviorTree support.

Loads a single behavior tree from file and creates a BTContext. Other behaviors can add actions and conditions to this behavior tree allowing to compose behavior tree actions and conditions easily.

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

Single Behavior Tree Usage

This is the most simple solution where only one behavior tree is used. For many AI situations this is enough and is the most simple solution to use. Add one ECBehaviorBehaviorTree with no ID to the element and as many other behaviors adding actions and conditions as required. Set the "behaviorTree.path" property to define the behavior tree to load. If you want to use this behavior for simple elements you can set the "behaviorTree.run" to "true". In this case the behavior will run the behavior tree on "think()" calls. For BaseActor use it is better to keep this feature disabled and using a BaseActorAI supporting this behavior. This is an example code for such a setup:

class MyElementClass extends BehaviorElementClass
func new()
var ECBehaviorBehaviorTree btbehavior = ECBehaviorBehaviorTree.new(this)
btbehavior.getPath().setValue("/content/myBehavior.debtree")
btbehavior.getRun().setValue(true)
// here you can add now behaviors adding actions and conditions for
// the behavior tree to use
...
end
end
ECBehaviorBehaviorTree new(BehaviorElementClass eclass)
Create behavior element class.
ECPBoolean getRun()
Run behavior tree during element thinking.
Multiple Behavior Tree Usage

You can add multiple instances of ECBehaviorBehaviorTree to the element to load and use multiple behavior trees. This can be either because you want to switch between behavior trees or using multiple trees together to model complex AI logic. In this situation behaviors adding actions and conditions do add them to all instances of ECBehaviorBehaviorTree. This is usually not a problem since the behavior trees are written to use only the actions and conditions making sense to be used. You can set any ECBehaviorBehaviorTree to run during "think()". If enabling more than one behavior like this make sure the individual behavior trees properly work together. This is an example code for such a setup:

class MyElementClass extends BehaviorElementClass
func new()
var ECBehaviorBehaviorTree btSpecific = ECBehaviorBehaviorTree.new(this, "specific")
btSpecific.getPath().setValue("/content/mySpecificBehavior.debtree")
btSpecific.getRun().setValue(true)
var ECBehaviorBehaviorTree btIdle = ECBehaviorBehaviorTree.new(this, "idle")
btIdle.getPath().setValue("/content/myIdleBehavior.debtree")
btIdle.getRun().setValue(true)
// here you can add now behaviors adding actions and conditions for both
// behavior trees to use
...
end
end

The above example loads and runs two behavior trees. The first one is running specific behavior tree with AI behavior like searching for the player or communicating with other actors. The second behavior tree provides idle actions used if the specific behavior tree is doing nothing. By choosing conditions properly the two trees do not interfere with each other and allow to split up behavior trees into multiple trees which can help create complex AI more simple and stable.

BaseActor Usage

This use case allows to use this behavior for BaseActor usage. BaseActor supports using BaseActorAI which can be exchanged to allow multiple different AI routines to be used without needing one large behavior tree. In this situation the behavior tree is not run by this behavior but by a BaseActorAI subclass. Both of the above scenarios are possible either using one behavior tree or multiple. Typically BaseActorAI does not use the BTContext from this behavior directly but instead create a copy. This has the advantage that each time a BaseActorAI is switched on it starts with a fresh context initialized with all actions and conditions added by other behaviors. Furthermore you can change the BehaviorTree to use during copy time opening up all kinds of possibilities to create actor AI easily. This is an example code for such a setup:

class MyElementClass extends BehaviorElementClass
func new()
var ECBehaviorBehaviorTree btree = ECBehaviorBehaviorTree.new(this)
btSpecific.getPath().setValue("/content/basic.debtree")
//btSpecific.getRun().setValue(false) // the default is false
// here you can add now behaviors adding actions and conditions for both
// behavior trees to use
...
end
end
class MyActorAI extends BAAIBehaviorTree
func new()
// load behavior tree to use. this is not required but allows to use different
// behavior trees for different AI routines. if not used the "/content/basic.debtree"
// would be used as defined in MyElementClass.
var BehaviorTree btree = BaseGameApp.getApp().getLoaders() \
.getBehaviorTree().loadFromFile("/content/aiRoutine1.debtree")
// create context. BAAIBehaviorTree requires a BTContextAI subclass hence we can
// use the ECBehaviorBehaviorTree context directly. if you write your own BaseActorAI
// subclasses this can be tough a viable option to use less script code. this copy
// here will ensure all actions and conditions are present so this is easy and quick
setContext(BTContextAI.new(this, ECBehaviorBehaviorTree.getInstanceIn(getActor()).getContext(), btree)
// run behavior tree during think()
setRunning(true)
end
end
ECPBehaviorTree getBehaviorTree()
Behavior tree.
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 BTAction and BTCondition. 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()
var ECBehaviorBehaviorTree btbehavior = ECBehaviorBehaviorTree.new(this)
btbehavior.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.
btbehavior.setActionConditionFactory(block ECBehaviorBehaviorTree.Instance instance
// add condition evaluating to true if "player.insideZone" trigger is in fired state.
// the condition can be used in the behavior tree using the name "player is inside zone".
instance.setCondition("player is inside zone", BTConditionTrigger.new(\
"player.insideZone", BTConditionTrigger.TestMode.fired))
// add action pulsing (fire and immediately reset) "player.damage" trigger.
// the action can be run in the behavior tree using the name "hurt playe".
instance.setAction("hurt player", BTActionTrigger.new("player.damage", BTActionTrigger.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.ECBehaviorBehaviorTree.addActionConditionFactory ( ActionConditionFactory  factory)

Add action and condition factory.

◆ addActionConditionFactory() [2/2]

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

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

This is a convenience method for using BlockActionConditionFactory.

◆ addListenerFactory()

void Dragengine.Scenery.ECBehaviorBehaviorTree.addListenerFactory ( Block  blockFactory)

Add listener factory using block.

Version
1.19

Block receives as parameter Instance and returns Listener.

◆ createListeners()

void Dragengine.Scenery.ECBehaviorBehaviorTree.createListeners ( Instance  instance)

Create listeners from factories adding them to behavior instance.

Version
1.19

◆ dispose()

void Dragengine.Scenery.ECBehaviorBehaviorTree.dispose ( )

Dispose of behavior.

Reimplemented from Dragengine.Scenery.DefaultECBehavior.

◆ forEachActionConditionFactory()

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

Visit listener factories with block with argument ActionConditionFactory.

◆ forEachListenerFactory()

void Dragengine.Scenery.ECBehaviorBehaviorTree.forEachListenerFactory ( Block  ablock)

Visit listener factories with block with argument ListenerFactory.

Version
1.19

◆ getBehaviorIn()

static ECBehaviorBehaviorTree Dragengine.Scenery.ECBehaviorBehaviorTree.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()

ECPBehaviorTree Dragengine.Scenery.ECBehaviorBehaviorTree.getBehaviorTree ( )

Behavior tree.

◆ getRun()

ECPBoolean Dragengine.Scenery.ECBehaviorBehaviorTree.getRun ( )

Run behavior tree during element thinking.

◆ new() [1/7]

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

Create behavior element class.

Reimplemented from Dragengine.Scenery.DefaultECBehavior.

◆ new() [2/7]

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

Create behavior element class.

◆ new() [3/7]

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

Create behavior element class.

◆ new() [4/7]

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

◆ new() [5/7]

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

◆ new() [6/7]

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

Create behavior element class.

Reimplemented from Dragengine.Scenery.DefaultECBehavior.

◆ new() [7/7]

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

Create behavior element class.

◆ setActionsConditions()

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

Assign actions and conditions to behavior instance.

Member Data Documentation

◆ pActionConditionFactories

Array Dragengine.Scenery.ECBehaviorBehaviorTree.pActionConditionFactories

◆ pBehaviorTree

ECPBehaviorTree Dragengine.Scenery.ECBehaviorBehaviorTree.pBehaviorTree

◆ pListenerFactories

Array Dragengine.Scenery.ECBehaviorBehaviorTree.pListenerFactories

◆ pRun

ECPBoolean Dragengine.Scenery.ECBehaviorBehaviorTree.pRun

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