{{tag>graphic skin texture fileformat}}
[[:start|Start Page]] >> [[:gamedev|Game Development with the Drag[en]gine]] >> **Behavior Tree File Format**
====== Behavior Trees ======
Behavior trees are structured graphics describing the AI logic using a tree of rules. The behavior tree is at all times located at one of the rules in the tree. During each simulation step (typically during frame update) the behavior tree moves along the tree until an rule returns either ''BTResult.running'' . The behavior tree keeps on running the active rule each simulation step until the rule either returns ''BTResult.success'' or ''BTResult.failure''. The AI logic is formed by the logic behind each rule.
Each can also have one or more conditions assigned. Before each run of the rule the conditions are evaluated. If any of the conditions evaluates to false the rule fails. Only the conditions of the active rule are evaluated. The conditions of the parent rule are only evaluated if the active node returns anything else but ''BTResult.running''. In this case the behavior tree wants to move to the next action. To do this the parent rule is run which then automatically evaluates the parent rule conditions.
Conditions can be paramtrized by adding one or more parameters to the rule. The conditions define themselves which parameters they understand and how they react to them. The name of parameters is free but it is recommended to use a naming scheme like this: ''.''. This groups parameters and reduces the chance of name conflicts. This is especially important since parameters are also reused for action rules. And while actions already define the ''source'' part rather well for conditions this is unknown.
The following rules can be used:
===== Action =====
This is the main working rule connecting game logic to the behavior tree. BTAction subclasses are created by the game developer and added to the behavior tree. If the rule is run the action is looked up and run. The return value of the action becomes the return value of the rule.
For performance reasons the action is looked up once then stored in the rule. This is correct behavior as long as the rules are not changed at run-time. If you need changing actions at run-time implement a BTAction forwarding to the desired BTAction. This way the cached action stays valid while the actual BTAction called inside can change. This is though an elaborate situation. Usually actions are added to the behavior tree once and then never change.
If the named action is not found the rule fails with ''BTResult.failure''.
There exists convenience implementations of the BTAction interface linking to action sources present in the game engine:
^Script Class^Description^
|BTActionParameterTable|Updates the value of a ParameterTree entry.|
|BTActionTrigger|Fire or reset a trigger target.|
|BTActionSendGlobalEvent|Send a global even using GlobalEvents class, typically BaseGameApp.getApp().getGlobalEvents().|
|BTBlockAction|Action running a script block like this:
new BTBlockAction(block BTContext context, Dictionary parameters
// do something
return BTResult.running // or .success or .failure
end)
This allows to quickly add actions without needing to write a new script class.|
Actions can be paramtrized by adding one or more parameters to the rule. The actions define themselves which parameters they understand and how they react to them. The name of parameters is free but it is recommended to use a naming scheme like this: ''.''. This groups parameters and reduces the chance of name conflicts. This is especially important since parameters are also reused for rule conditions. And while actions already define the ''source'' part rather well for conditions this is unknown.
===== Sequence =====
Runs all actions in sequence. Returns ''BTResult.success'' if all actions returned ''BTResult.success'' . If an action return ''BTResult.failure'' processing of the rule stops and ''BTResult.failure'' is returned. If no actions are present ''BTResult.failure'' is returned.
If looping is set to true the sequence restarts at the beginning if all actions returns ''BTResult.success'' . This allows to keep looping a sequence until the first time any action returns ''BTResult.failure'' .
===== Choice =====
Runs all actions in sequence. Returns ''BTResult.failure'' if all actions returned ''BTResult.failure'' . If an action returns ''BTResult.success'' processing of the rule stops and ''BTResult.success'' is returned. If no actions are present ''BTResult.failure'' is returned.
If looping is set to true the choice restarts at the beginning if the first action returns ''BTResult.success'' . This allows to keep looping a choice until the first time all actions return ''BTResult.failure'' .
===== Success =====
Returns always ''BTResult.success'' unless a rule condition evaluates to false.
The main use for this rule is to test rule conditions without needing to attach them to some other rule.
===== Failure =====
Returns always ''BTResult.failure''.
The main use for this rule is to fail a sequence rule by placing this last.
===== Running =====
Returns always ''BTResult.running'' unless a rule condition evaluates to false.
The main use for this rule is to force waiting until a rule conditions fails.
====== File Format (*.debtree) ======
The behavior tree file format is recognized by the LoadBehaviorTree script class. The file is an XML file with a simple structure to define a behavior tree using XML.
@startuml
object "behaviorTree" as behaviorTree {
<#transparent,#transparent>|action||
|sequence||
|choice||
|success||
|failure||
|running||
|subtree| string|
}
object "action" as action {
<#transparent,#transparent>|parameter| string|
|condition| string|
|conditionMode| enum|
}
behaviorTree --> action
object "sequence" as sequence {
<#transparent,#transparent>|parameter| string|
|condition| string|
|conditionMode| enum|
|action||
|sequence||
|choice||
|success||
|failure||
|running||
|subtree| string|
}
behaviorTree --> sequence
object "choice" as choice {
<#transparent,#transparent>|parameter| string|
|condition| string|
|conditionMode| enum|
|action||
|sequence||
|choice||
|success||
|failure||
|running||
|subtree| string|
}
behaviorTree --> choice
object "success" as success {
<#transparent,#transparent>|parameter| string|
|condition| string|
|conditionMode| enum|
}
behaviorTree --> success
object "failure" as failure {
<#transparent,#transparent>|parameter| string|
|condition| string|
|conditionMode| enum|
}
behaviorTree --> failure
object "running" as running {
<#transparent,#transparent>|parameter| string|
|condition| string|
|conditionMode| enum|
}
behaviorTree --> running
@enduml
====== Tags ======
===== behaviorTree =====
^Tag^Description^Occurance^Default^
|action|Add action rule|0..N|-|
|sequence|Add sequence rule|0..N|-|
|choice|Add choice rule|0..N|-|
|success|Add success rule|0..N|-|
|failure|Add failure rule|0..N|-|
|running|Add running rule|0..N|-|
|subtree|Load behavior tree using path from tag text content. Subtree is added to the behavior tree as a sequence rule containing the loaded behavior tree|0..N|-|
===== action =====
Adds an action rule.
^Attribute^Description^Occurance^Default^
|name|Name of action to run.|Required|-|
^Tag^Description^Occurance^Default^
|parameter|Add parameter. Parameter value is text content of node which can be empty. Attributes:
* ''name'': Name of parameter. Required.
|0..N|-|
|condition|Add condition. Condition name is text content of node|0..N|-|
|conditionMode|How conditions are evaluated. Valid values:
* ''allTrue'': All conditions have to evaluate to true.
* ''anyTrue'': At least one condition has to evaluate to true.
* ''anyFalse'': At least one condition has to evaluate to false.
* ''allFalse'': All conditions have to evaluate to false.
|0..1|''allTrue''|
===== sequence =====
Adds a sequence rule.
^Attribute^Description^Occurance^Default^
|loop|Loop sequence.|Optional|''false''|
|doNotFail|If sequence fails return ''BTResult.success''. Useful to optional sequences running as many rules in a sequence as possible without failing the parent rule.|Optional|''false''|
^Tag^Description^Occurance^Default^
|parameter|Add parameter. Parameter value is text content of node which can be empty. Attributes:
* ''name'': Name of parameter. Required.
|0..N|-|
|condition|Add condition. Condition name is text content of node|0..N|-|
|conditionMode|How conditions are evaluated. Valid values:
* ''allTrue'': All conditions have to evaluate to true.
* ''anyTrue'': At least one condition has to evaluate to true.
* ''anyFalse'': At least one condition has to evaluate to false.
* ''allFalse'': All conditions have to evaluate to false.
|0..1|''allTrue''|
|action|Add action rule|0..N|-|
|sequence|Add sequence rule|0..N|-|
|choice|Add choice rule|0..N|-|
|success|Add success rule|0..N|-|
|failure|Add failure rule|0..N|-|
|running|Add running rule|0..N|-|
|subtree|Load behavior tree using path from tag text content. Subtree is added to the behavior tree as a sequence rule containing the loaded behavior tree|0..N|-|
===== choice =====
Adds a choice rule.
^Attribute^Description^Occurance^Default^
|loop|Loop choice.|Optional|''false''|
|doNotFail|If choice fails return ''BTResult.success''. Useful for optional choices there failing the choice should not fail the parent rule.|Optional|''false''|
^Tag^Description^Occurance^Default^
|parameter|Add parameter. Parameter value is text content of node which can be empty. Attributes:
* ''name'': Name of parameter. Required.
|0..N|-|
|condition|Add condition. Condition name is text content of node|0..N|-|
|conditionMode|How conditions are evaluated. Valid values:
* ''allTrue'': All conditions have to evaluate to true.
* ''anyTrue'': At least one condition has to evaluate to true.
* ''anyFalse'': At least one condition has to evaluate to false.
* ''allFalse'': All conditions have to evaluate to false.
|0..1|''allTrue''|
|action|Add action rule|0..N|-|
|sequence|Add sequence rule|0..N|-|
|choice|Add choice rule|0..N|-|
|success|Add success rule|0..N|-|
|failure|Add failure rule|0..N|-|
|running|Add running rule|0..N|-|
|subtree|Load behavior tree using path from tag text content. Subtree is added to the behavior tree as a sequence rule containing the loaded behavior tree|0..N|-|
===== success =====
Adds a success rule.
^Tag^Description^Occurance^Default^
|parameter|Add parameter. Parameter value is text content of node which can be empty. Attributes:
* ''name'': Name of parameter. Required.
|0..N|-|
|condition|Add condition. Condition name is text content of node|0..N|-|
|conditionMode|How conditions are evaluated. Valid values:
* ''allTrue'': All conditions have to evaluate to true.
* ''anyTrue'': At least one condition has to evaluate to true.
* ''anyFalse'': At least one condition has to evaluate to false.
* ''allFalse'': All conditions have to evaluate to false.
|0..1|''allTrue''|
===== failure =====
Adds a failure rule.
^Tag^Description^Occurance^Default^
|parameter|Add parameter. Parameter value is text content of node which can be empty. Attributes:
* ''name'': Name of parameter. Required.
|0..N|-|
|condition|Add condition. Condition name is text content of node|0..N|-|
|conditionMode|How conditions are evaluated. Valid values:
* ''allTrue'': All conditions have to evaluate to true.
* ''anyTrue'': At least one condition has to evaluate to true.
* ''anyFalse'': At least one condition has to evaluate to false.
* ''allFalse'': All conditions have to evaluate to false.
|0..1|''allTrue''|
===== running =====
Adds a running rule.
^Tag^Description^Occurance^Default^
|parameter|Add parameter. Parameter value is text content of node which can be empty. Attributes:
* ''name'': Name of parameter. Required.
|0..N|-|
|condition|Add condition. Condition name is text content of node|0..N|-|
|conditionMode|How conditions are evaluated. Valid values:
* ''allTrue'': All conditions have to evaluate to true.
* ''anyTrue'': At least one condition has to evaluate to true.
* ''anyFalse'': At least one condition has to evaluate to false.
* ''allFalse'': All conditions have to evaluate to false.
|0..1|''allTrue''|
====== Examples ======
Example from the ExampleApp. A simple behavior tree making the actor wander around and if the player gets close tries to stay away from him. If the player is moving fast the actor gets jumpy.
This uses the basic behavior tree system from the game engine with some ready made actions.
0.25allFalseplayerNearby0.35playerNotNearby1playerNotNearbyplayerNearbyplayerNearby