Drag[en]gine Script Module DragonScript 1.35
Loading...
Searching...
No Matches
Dragengine.BehaviorTreeSystem.BTContext Class Reference

Behavior tree context. More...

Inheritance diagram for Dragengine.BehaviorTreeSystem.BTContext:
Dragengine.Scenery.BAAIBehaviorTree.BTContextAI

Classes

class  StateBackup
 Behavior tree state backup. More...

Public Member Functions

StateBackup createBackupState ()
 Back up state.
String debugGetCurrentRule ()
 Get string path describing current rule for debug purpose.
String debugGetRulePath (BTRule rule)
 Get string path describing rule for debug purpose.
void forEachAction (Block ablock)
 Visit actions using block with parameters String (name and BTAction (action).
void forEachCondition (Block ablock)
 Visit conditions using block with parameters String (name and BTCondition (condition).
BTAction getAction (String name)
 Action for name or null if absent.
int getActionCount ()
 Number of actions.
bool getActionFirstRun ()
 Action runs for the first time after the current rule changed.
BehaviorTree getBehaviorTree ()
 Behavior tree.
BTCondition getCondition (String name)
 Condition for name or null if absent.
int getConditionCount ()
 Number of conditions.
Console getConsole ()
 Console or null if not wishing to log anything.
BTRule getCurrentRule ()
 Current rule.
BTResult getLastResult ()
 Last result.
String getLoggingName ()
 Logging name to tell apart logs.
bool getLogRuleChanges ()
 Log behavior tree rule changes.
bool getLogSteps ()
 Log behavior tree steps.
int getRunAwayLimit ()
 Run away limit.
bool hasAction (String name)
 Action is present.
bool hasCondition (String name)
 Condition is present.
BTContext new (BehaviorTree behaviorTree)
 Create behavior tree context.
BTContext new (BTContext context)
 Create copy of behavior tree context.
BTContext new (BTContext context, BehaviorTree behaviorTree)
 Create copy of behavior tree context but using a different behavior tree.
void readFromFile (PersistencyEnvironment env, FileReader reader)
 Read state from file.
void removeAction (String name)
 Remove action.
void removeAllActions ()
 Remove all actions.
void removeAllConditions ()
 Remove all conditions.
void removeCondition (String name)
 Remove condition.
void reset ()
 Reset to initial state.
void restoreBackupState (StateBackup state)
 Restore state from backup.
void setAction (String name, Block action)
 Set action for name using block.
void setAction (String name, BTAction action)
 Set action for name.
void setActionFirstRun (bool firstRun)
 Set action runs for the first time after the current rule changed.
void setBehaviorTree (BehaviorTree behaviorTree)
 Set behavior tree and reset state.
void setCondition (String name, Block condition)
 Set condition for name using block.
void setCondition (String name, BTCondition condition)
 Set condition for name.
void setConsole (Console console)
 Set console or null if not wishing to log anything.
void setCurrentRule (BTRule rule)
 Set current rule.
void setCurrentRuleByID (String id)
 Set current rule by identifier.
void setLoggingName (String name)
 Set logging name to tell apart logs.
void setLogRuleChanges (bool logRuleChanges)
 Set log behavior tree rule changes.
void setLogSteps (bool logSteps)
 Set log behavior tree steps.
void setRunAwayLimit (int limit)
 Set run away limit.
bool stepBehaviorTree ()
 Step behavior tree.
void writeToFile (PersistencyEnvironment env, FileWriter writer)
 Write state to file.

Public Attributes

bool pActionFirstRun
Dictionary pActions
BehaviorTree pBehaviorTree
Dictionary pConditions
Console pConsole
BTRule pCurrentRule
BTResult pLastResult
String pLoggingName
bool pLogRuleChanges
bool pLogSteps
Array pRunAwayList

Protected Member Functions

void onCurrentRuleChanged ()
 Hook method called if setCurrentRule() is called.

Detailed Description

Behavior tree context.

Used to run a behavior tree instance. The behavior tree itself is shared across contexts while the individual state is stored in the context. Context has to provide the BTAction instances for the behavior tree rules to call. Subclass the context to provide the required data for your BTAction subclasses to work properly.

The context can be written to and restored from file readers. This class saves only the current rule identifier. Subclass has to save its own states by overwriting readFromFile() and writeToFile().

Since version 1.19 the behavior tree can be changed after creating the context. If you change the behavior tree during runtime you have to make sure it is also set before readFromFile() is called otherwise an exception can be thrown.

Member Function Documentation

◆ createBackupState()

StateBackup Dragengine.BehaviorTreeSystem.BTContext.createBackupState ( )

Back up state.

Version
1.30

Allows to properly restore the behavior tree to this state. Restores current rule and action first run flag.

You can also call

StateBackup.new(context)
Behavior tree state backup.
Definition BTContext.ds:55
StateBackup new(BTContext context)
Create state backup.

directly.

References pActions, and setAction().

Referenced by writeToFile().

◆ debugGetCurrentRule()

String Dragengine.BehaviorTreeSystem.BTContext.debugGetCurrentRule ( )

Get string path describing current rule for debug purpose.

Version
1.26

References onCurrentRuleChanged(), pConsole, pLoggingName, and pLogRuleChanges.

Referenced by writeToFile().

◆ debugGetRulePath()

String Dragengine.BehaviorTreeSystem.BTContext.debugGetRulePath ( BTRule rule)

Get string path describing rule for debug purpose.

Version
1.30

Referenced by writeToFile().

◆ forEachAction()

void Dragengine.BehaviorTreeSystem.BTContext.forEachAction ( Block ablock)

Visit actions using block with parameters String (name and BTAction (action).

References pConsole, pLoggingName, and pLogSteps.

Referenced by setLogRuleChanges().

◆ forEachCondition()

void Dragengine.BehaviorTreeSystem.BTContext.forEachCondition ( Block ablock)

Visit conditions using block with parameters String (name and BTCondition (condition).

Referenced by getActionFirstRun().

◆ getAction()

BTAction Dragengine.BehaviorTreeSystem.BTContext.getAction ( String name)

Action for name or null if absent.

References pActions, pConditions, and pCurrentRule.

Referenced by getLogRuleChanges().

◆ getActionCount()

int Dragengine.BehaviorTreeSystem.BTContext.getActionCount ( )

Number of actions.

References pBehaviorTree.

Referenced by setLogSteps().

◆ getActionFirstRun()

bool Dragengine.BehaviorTreeSystem.BTContext.getActionFirstRun ( )

Action runs for the first time after the current rule changed.

After calling setCurrentRule() this flag is set to true. BTAction can use this flag to know if they run for the first time after a rule change. This can be used to set up tasks the first time an action runs and check back later on the state.

Note
Rules are responsible to clear this flag after running if they use it.

References forEachCondition(), and pConditions.

Referenced by writeToFile().

◆ getBehaviorTree()

BehaviorTree Dragengine.BehaviorTreeSystem.BTContext.getBehaviorTree ( )

Behavior tree.

References pBehaviorTree, and pConditions.

Referenced by removeAction().

◆ getCondition()

BTCondition Dragengine.BehaviorTreeSystem.BTContext.getCondition ( String name)

Condition for name or null if absent.

Referenced by setCurrentRuleByID().

◆ getConditionCount()

int Dragengine.BehaviorTreeSystem.BTContext.getConditionCount ( )

Number of conditions.

References pLogRuleChanges, and pLogSteps.

Referenced by setCurrentRuleByID().

◆ getConsole()

Console Dragengine.BehaviorTreeSystem.BTContext.getConsole ( )

Console or null if not wishing to log anything.

Referenced by writeToFile().

◆ getCurrentRule()

BTRule Dragengine.BehaviorTreeSystem.BTContext.getCurrentRule ( )

Current rule.

References setBehaviorTree().

Referenced by reset().

◆ getLastResult()

BTResult Dragengine.BehaviorTreeSystem.BTContext.getLastResult ( )

Last result.

References pConditions.

Referenced by writeToFile().

◆ getLoggingName()

String Dragengine.BehaviorTreeSystem.BTContext.getLoggingName ( )

Logging name to tell apart logs.

References removeAllConditions().

Referenced by writeToFile().

◆ getLogRuleChanges()

bool Dragengine.BehaviorTreeSystem.BTContext.getLogRuleChanges ( )

Log behavior tree rule changes.

References getAction().

Referenced by writeToFile().

◆ getLogSteps()

bool Dragengine.BehaviorTreeSystem.BTContext.getLogSteps ( )

Log behavior tree steps.

Referenced by writeToFile().

◆ getRunAwayLimit()

int Dragengine.BehaviorTreeSystem.BTContext.getRunAwayLimit ( )

Run away limit.

Referenced by writeToFile().

◆ hasAction()

bool Dragengine.BehaviorTreeSystem.BTContext.hasAction ( String name)

Action is present.

References pActionFirstRun, and pRunAwayList.

Referenced by setLogRuleChanges().

◆ hasCondition()

bool Dragengine.BehaviorTreeSystem.BTContext.hasCondition ( String name)

Condition is present.

Referenced by setCurrentRuleByID().

◆ new() [1/3]

BTContext Dragengine.BehaviorTreeSystem.BTContext.new ( BehaviorTree behaviorTree)

Create behavior tree context.

References pActionFirstRun.

◆ new() [2/3]

BTContext Dragengine.BehaviorTreeSystem.BTContext.new ( BTContext context)

Create copy of behavior tree context.

Actions are copied as shallow copy. If you need a deep set all actions anew. The state of the context is the same as without creating a copy.

References pActionFirstRun, pActions, pBehaviorTree, pConditions, pCurrentRule, pLastResult, and pRunAwayList.

◆ new() [3/3]

BTContext Dragengine.BehaviorTreeSystem.BTContext.new ( BTContext context,
BehaviorTree behaviorTree )

Create copy of behavior tree context but using a different behavior tree.

Actions are copied as shallow copy. If you need a deep set all actions anew. The state of the context is the same as without creating a copy.

This method is useful to use an initialized base context with a fallback behavior tree. Whenever a new context is required the base context is copied. This avoids the need to assign actions and conditions since they are copied from the base context.

References pConsole, pLoggingName, pLogRuleChanges, and pLogSteps.

◆ onCurrentRuleChanged()

void Dragengine.BehaviorTreeSystem.BTContext.onCurrentRuleChanged ( )
protected

Hook method called if setCurrentRule() is called.

Allows subclasses to react to current rule changes. This might be required for certain BTAction to reset states if rules change.

References pActionFirstRun, pCurrentRule, and setCurrentRule().

Referenced by debugGetCurrentRule(), and writeToFile().

◆ readFromFile()

void Dragengine.BehaviorTreeSystem.BTContext.readFromFile ( PersistencyEnvironment env,
FileReader reader )

Read state from file.

Referenced by writeToFile().

◆ removeAction()

void Dragengine.BehaviorTreeSystem.BTContext.removeAction ( String name)

Remove action.

References getBehaviorTree(), and pBehaviorTree.

Referenced by restoreBackupState().

◆ removeAllActions()

void Dragengine.BehaviorTreeSystem.BTContext.removeAllActions ( )

Remove all actions.

Referenced by stepBehaviorTree().

◆ removeAllConditions()

void Dragengine.BehaviorTreeSystem.BTContext.removeAllConditions ( )

Remove all conditions.

Referenced by getLoggingName().

◆ removeCondition()

void Dragengine.BehaviorTreeSystem.BTContext.removeCondition ( String name)

Remove condition.

Referenced by setConsole().

◆ reset()

void Dragengine.BehaviorTreeSystem.BTContext.reset ( )

Reset to initial state.

Sets the behavior tree root rule as current rule.

References getCurrentRule(), and pCurrentRule.

Referenced by setCurrentRule(), and writeToFile().

◆ restoreBackupState()

void Dragengine.BehaviorTreeSystem.BTContext.restoreBackupState ( StateBackup state)

Restore state from backup.

Version
1.30

Works only if the state has been created from this context with the same behavior tree.

References pActions, removeAction(), and setAction().

Referenced by writeToFile().

◆ setAction() [1/2]

void Dragengine.BehaviorTreeSystem.BTContext.setAction ( String name,
Block action )

Set action for name using block.

◆ setAction() [2/2]

void Dragengine.BehaviorTreeSystem.BTContext.setAction ( String name,
BTAction action )

Set action for name.

References pLogRuleChanges.

Referenced by createBackupState(), and restoreBackupState().

◆ setActionFirstRun()

void Dragengine.BehaviorTreeSystem.BTContext.setActionFirstRun ( bool firstRun)

Set action runs for the first time after the current rule changed.

Called by BTRuleAction.run() and BTResultYield. Call this if your rule needs to know if it is run for the first time after setCurrentRule() is called.

References setCondition().

Referenced by writeToFile().

◆ setBehaviorTree()

void Dragengine.BehaviorTreeSystem.BTContext.setBehaviorTree ( BehaviorTree behaviorTree)

Set behavior tree and reset state.

Version
1.19

Sets the behavior tree then calls reset(). If you change the behavior tree during runtime you have to make sure it is also set before readFromFile() is called otherwise an exception can be thrown.

References pActionFirstRun, pActions, pCurrentRule, pLoggingName, and pRunAwayList.

Referenced by getCurrentRule().

◆ setCondition() [1/2]

void Dragengine.BehaviorTreeSystem.BTContext.setCondition ( String name,
Block condition )

Set condition for name using block.

◆ setCondition() [2/2]

void Dragengine.BehaviorTreeSystem.BTContext.setCondition ( String name,
BTCondition condition )

Set condition for name.

Referenced by setActionFirstRun(), and setRunAwayLimit().

◆ setConsole()

void Dragengine.BehaviorTreeSystem.BTContext.setConsole ( Console console)

Set console or null if not wishing to log anything.

References pConditions, and removeCondition().

Referenced by writeToFile().

◆ setCurrentRule()

void Dragengine.BehaviorTreeSystem.BTContext.setCurrentRule ( BTRule rule)

Set current rule.

Sets action first run to true and calls onCurrentRuleChanged().

References pBehaviorTree, and reset().

Referenced by onCurrentRuleChanged(), and writeToFile().

◆ setCurrentRuleByID()

void Dragengine.BehaviorTreeSystem.BTContext.setCurrentRuleByID ( String id)

Set current rule by identifier.

Sets action first run to true and calls onCurrentRuleChanged().

Exceptions
EInvalidParamRule with identifier not found.

References getCondition(), getConditionCount(), hasCondition(), and pConditions.

Referenced by writeToFile().

◆ setLoggingName()

void Dragengine.BehaviorTreeSystem.BTContext.setLoggingName ( String name)

Set logging name to tell apart logs.

References pConditions.

Referenced by writeToFile().

◆ setLogRuleChanges()

void Dragengine.BehaviorTreeSystem.BTContext.setLogRuleChanges ( bool logRuleChanges)

Set log behavior tree rule changes.

References forEachAction(), hasAction(), and pActions.

Referenced by writeToFile().

◆ setLogSteps()

void Dragengine.BehaviorTreeSystem.BTContext.setLogSteps ( bool logSteps)

Set log behavior tree steps.

References getActionCount(), and pActions.

Referenced by writeToFile().

◆ setRunAwayLimit()

void Dragengine.BehaviorTreeSystem.BTContext.setRunAwayLimit ( int limit)

Set run away limit.

References pConditions, and setCondition().

Referenced by writeToFile().

◆ stepBehaviorTree()

bool Dragengine.BehaviorTreeSystem.BTContext.stepBehaviorTree ( )

Step behavior tree.

Multiple actions can run before this method returns.

Exceptions
BTRunAwayExceptionRun-away condition detected.
Returns
true if tree continues running or false if root rule returned BTResult.failure .

References pActions, and removeAllActions().

Referenced by writeToFile().

◆ writeToFile()

Member Data Documentation

◆ pActionFirstRun

bool Dragengine.BehaviorTreeSystem.BTContext.pActionFirstRun

◆ pActions

Dictionary Dragengine.BehaviorTreeSystem.BTContext.pActions

◆ pBehaviorTree

BehaviorTree Dragengine.BehaviorTreeSystem.BTContext.pBehaviorTree

◆ pConditions

Dictionary Dragengine.BehaviorTreeSystem.BTContext.pConditions

◆ pConsole

Console Dragengine.BehaviorTreeSystem.BTContext.pConsole

◆ pCurrentRule

BTRule Dragengine.BehaviorTreeSystem.BTContext.pCurrentRule

◆ pLastResult

BTResult Dragengine.BehaviorTreeSystem.BTContext.pLastResult

Referenced by new(), and writeToFile().

◆ pLoggingName

String Dragengine.BehaviorTreeSystem.BTContext.pLoggingName

◆ pLogRuleChanges

bool Dragengine.BehaviorTreeSystem.BTContext.pLogRuleChanges

◆ pLogSteps

bool Dragengine.BehaviorTreeSystem.BTContext.pLogSteps

◆ pRunAwayList

Array Dragengine.BehaviorTreeSystem.BTContext.pRunAwayList

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