This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
dragengine:modules:dragonscript:behavior_statemachine [2024/03/14 16:43] – dragonlord | dragengine:modules:dragonscript:behavior_statemachine [2025/05/09 19:04] (current) – dragonlord | ||
---|---|---|---|
Line 1: | Line 1: | ||
{{tag> | {{tag> | ||
<WRAP youarehere> | <WRAP youarehere> | ||
- | [[: | + | [[: |
</ | </ | ||
Line 16: | Line 16: | ||
</ | </ | ||
- | Behavior element behavior adding | + | Behavior element behavior adding StateMachine support. |
- | Loads state machine from file (*.desm) and creates | + | Loads state machine from file (*.desm) and creates |
+ | |||
+ | You can assign # | ||
This behavior can be used in different ways depending on the needs. | This behavior can be used in different ways depending on the needs. | ||
- | Single state machine 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. | + | //Single State Machine Usage// |
- | This is an example code for such a setup: | + | This is the most simple solution where only one state machine is used. For many situations |
- | < | + | |
- | class MyElementClass extends BehaviorElementClass | + | See [[# |
- | | + | |
- | var ECBehaviorStateMachine smbehavior = ECBehaviorStateMachine.new(this) | + | //Multiple State Machine Usage// |
- | smbehavior.getPath().setValue("/ | + | |
- | + | 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 | |
- | // here you can add now behaviors assigining actions and conditions | + | |
- | // the state machine | + | See [[# |
- | ... | + | |
- | + | ====== Instance Counts ====== | |
- | // | + | This behavior can be used multiple times on an element to add multiple custom colors |
- | var ECBehaviorTwoStateAnimated animated | + | |
- | | + | ====== Element Class Properties ====== |
- | end | + | Element class properties have the prefix '' |
- | | + | |
- | // two state listeners used to send events | + | ===== path ===== |
- | class MyEventListener extends ECBehaviorTwoStateAnimated.DefaultListener | + | Set path to state machine file to load. |
- | class Factory implements ECBehaviorTwoStateAnimated.ActionConditionFactory | + | * Full name: '' |
- | | + | * Type: string |
- | + | * Default Value: empty string | |
- | | + | * Expected File Type: '' |
- | this.stateMachine = stateMachine | + | * Example |
- | end | + | |
- | | + | ===== debug ===== |
- | public func ECBehaviorTwoStateAnimated.Listener createListener(ECBehaviorTwoStateAnimated.Instance instance) | + | Debug mode prefix. If not empty string enables debug mode with prefix to locate log messages easily. |
- | return MyEventListener.new(stateMachine.instance(instance.getElement())) | + | * Full name: '' |
- | end | + | * Type: string |
- | end | + | * Default Value: empty string |
- | | + | * Example |
- | public var ECBehaviorStateMachine.Instance | + | |
- | | + | ===== Events ===== |
- | public func new(ECBehaviorStateMachine.Instance stateMachine) | + | |
- | this.stateMachine | + | This behavior has no events. |
- | end | + | |
- | | + | ====== Behavior Tree Actions ====== |
- | public func void stopActivate(ECBehaviorTwoStateAnimated.Instance instance) | + | |
- | | + | This behavior adds these behavior tree actions if behavior tree is present. If behavior has non-empty identifier replace '' |
- | end | + | |
- | | + | ===== stateMachine.set ===== |
- | public func void stopDeactivate(ECBehaviorTwoStateAnimated.Instance instance) | + | |
- | stateMachine.runEvent(" | + | Set one or more state machine parameters. |
- | end | + | |
- | | + | ^Parameter^Value^Description^ |
- | end | + | |reset| |Set state machine root state as current state| |
+ | |state|string|Set state machine state to state with identifier matching string value| | ||
+ | |||
+ | This is an example of using this action: | ||
+ | <code xml> | ||
+ | <action name='stateMachine.set'> | ||
+ | | ||
+ | </ | ||
</ | </ | ||
- | **Action and Condition Factory** | + | ===== stateMachine.event ===== |
- | You can assign # | + | Run event on state machine. |
- | The example below shows how to use a factory to add a condition checking for a trigger and an action | + | ^Parameter^Value^Description^ |
- | < | + | |id|string|Identifier of event to run| |
- | class MyElementClass extends BehaviorElementClass | + | |
- | | + | This is an example of using this action: |
- | var ECBehaviorStateMachine smbehavior | + | < |
- | | + | <action name=' |
- | + | < | |
- | // add factory to create actions and conditions later on. this example uses | + | </action> |
- | // 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 " | + | |
- | // the condition can be used in the state machine using the name " | + | |
- | | + | |
- | " | + | |
- | + | ||
- | // add action pulsing (fire and immediately reset) " | + | |
- | | + | |
- | | + | |
- | end ) | + | |
- | end | + | |
- | end | + | |
</ | </ | ||
- | ====== Instance Counts ====== | + | ===== stateMachine.check |
- | You can add multiple instances of **ECBehaviorStateMachine** to the element to load and use multiple | + | Check one or more state machine parameters. Action succeeds if all parameter value matches their respective |
- | This is an example code for such a setup: | + | ^Parameter^Value^Description^ |
- | < | + | |state|string|Current state identifier matches string value| |
- | class MyElementClass extends BehaviorElementClass | + | |state.not|string|Current state identifier does not match string value| |
- | public func new() | + | |state.starts|string|Current state identifier starts with string value| |
- | var ECBehaviorStateMachine smSpecific = ECBehaviorStateMachine.new(this, " | + | |state.starts.not|string|Current state identifier does not start with string value| |
- | | + | |state.ends|string|Current state identifier ends with string value| |
- | + | |state.ends.not|string|Current state identifier does not end with string value| | |
- | var ECBehaviorStateMachine smGeneric = ECBehaviorStateMachine.new(this, " | + | |state.contains|string|Current state identifier contains string value| |
- | | + | |state.contains.not|string|Current state identifier does not contain string value| |
- | + | |wait| |If present action returns BTResult.running instead of BTResult.failed | |
- | // here you can add now behaviors assigining actions and conditions for both | + | |
- | // state machines to use | + | This is an example of using this action: |
- | ... | + | <code xml> |
- | + | < | |
- | // add listeners to behaviors to send events | + | < |
- | // MyEventListener | + | < |
- | // here uses two animated behaviors each sending events to one of the state | + | </ |
- | // machines. in general | + | < |
- | // 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 | + | |
- | | + | |
- | + | ||
- | var ECBehaviorTwoStateAnimated animated2 | + | |
- | | + | |
- | 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. | + | ====== Behavior Tree Conditions ====== |
- | ====== Element Class Properties ====== | + | This behavior adds these behavior tree conditions if behavior tree is present. If behavior has non-empty identifier replace '' |
- | Element class properties have the prefix **stateMachine.** or **stateMachine(id).** if id is not empty. | + | ===== stateMachine.check ===== |
- | ===== path ===== | + | Check one or more state machine |
- | Path to state machine | + | |
- | * Full name: "stateMachine.path" or "stateMachine(id).path" | + | ^Parameter^Value^Description^ |
- | * Type: Path (*.desm) | + | |stateMachine.state|string|Current state identifier matches string value| |
- | * Default Value "" | + | |stateMachine.state.not|string|Current state identifier does not match string value| |
- | * Example (*.deeclass) | + | |stateMachine.state.starts|string|Current state identifier starts with string value| |
+ | |stateMachine.state.starts.not|string|Current state identifier does not start with string value| | ||
+ | |stateMachine.state.ends|string|Current state identifier ends with string value| | ||
+ | |stateMachine.state.ends.not|string|Current state identifier does not end with string value| | ||
+ | |stateMachine.state.contains|string|Current state identifier contains string value| | ||
+ | |stateMachine.state.contains.not|string|Current state identifier does not contain string value| | ||
+ | |||
+ | This is an example of using this condition: | ||
+ | <code xml> | ||
+ | <action name=' | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
====== Required Behaviors ====== | ====== Required Behaviors ====== | ||
- | This behavior | + | This behavior |
====== Optional Behaviors ====== | ====== Optional Behaviors ====== | ||
- | This behavior | + | * [[behavior_behaviortree|ECBehaviorBehaviorTree]]: |
====== Persistency ====== | ====== Persistency ====== | ||
This behavior does support element class to be persistable (setPersistable). Saves state machine content (# | This behavior does support element class to be persistable (setPersistable). Saves state machine content (# | ||
+ | |||
====== API Documentation ====== | ====== API Documentation ====== | ||
Line 161: | Line 162: | ||
# | # | ||
- | Since DragonScript Module Version | + | Since DragonScript Module Version |
====== Use Cases ====== | ====== Use Cases ====== | ||
Line 171: | Line 172: | ||
====== Element Class Example ====== | ====== Element Class Example ====== | ||
- | Element class using a state machine loaded from " | + | Example of the //Single State Machine Usage//. |
< | < | ||
class MyElementClass extends BehaviorElementClass | class MyElementClass extends BehaviorElementClass | ||
- | | + | |
- | var ECBehaviorStateMachine smbehavior = ECBehaviorStateMachine.new(this) | + | var ECBehaviorStateMachine smbehavior = ECBehaviorStateMachine.new(this) |
- | smbehavior.getPath().setValue("/ | + | smbehavior.getPath().setValue("/ |
+ | |||
+ | // 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 | ||
| | ||
- | | + | func new(ECBehaviorStateMachine stateMachine) |
- | // 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 | + | |
- | | + | |
- | + | ||
- | | + | |
- | this.stateMachine = stateMachine | + | |
- | end | + | |
- | + | ||
- | | + | |
- | return MyEventListener.new(stateMachine.instance(instance.getElement())) | + | |
- | end | + | |
- | end | + | |
- | + | ||
- | | + | |
- | + | ||
- | | + | |
this.stateMachine = stateMachine | this.stateMachine = stateMachine | ||
- | end | + | end |
- | | + | |
- | | + | func ECBehaviorTwoStateAnimated.Listener createListener(ECBehaviorTwoStateAnimated.Instance instance) |
- | stateMachine.runEvent(" | + | return MyEventListener.new(stateMachine.instance(instance.getElement())) |
- | | + | end |
- | | + | end |
- | public | + | |
- | stateMachine.runEvent(" | + | |
- | | + | |
+ | func new(ECBehaviorStateMachine.Instance stateMachine) | ||
+ | this.stateMachine = stateMachine | ||
+ | end | ||
+ | |||
+ | | ||
+ | stateMachine.runEvent(" | ||
+ | end | ||
+ | |||
+ | func void stopDeactivate(ECBehaviorTwoStateAnimated.Instance instance) | ||
+ | stateMachine.runEvent(" | ||
+ | end | ||
end | end | ||
end | end | ||
+ | </ | ||
+ | |||
+ | Example of the //Multiple State Machine Usage//. | ||
+ | |||
+ | < | ||
+ | class MyElementClass extends BehaviorElementClass | ||
+ | func new() | ||
+ | var ECBehaviorStateMachine smSpecific = ECBehaviorStateMachine.new(this, | ||
+ | smSpecific.getPath().setValue("/ | ||
+ | | ||
+ | var ECBehaviorStateMachine smGeneric = ECBehaviorStateMachine.new(this, | ||
+ | smGeneric.getPath().setValue("/ | ||
+ | | ||
+ | // 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// | ||
+ | |||
+ | This example 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 ECBehaviorStateMachine smbehavior = ECBehaviorStateMachine.new(this) | ||
+ | smbehavior.getPath().setValue("/ | ||
+ | | ||
+ | // 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 " | ||
+ | // the condition can be used in the state machine using the name " | ||
+ | instance.setCondition(" | ||
+ | " | ||
+ | | ||
+ | // add action pulsing (fire and immediately reset) " | ||
+ | // the action can be run in the state machine using the name "hurt playe" | ||
+ | instance.setAction(" | ||
+ | end) | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | ====== Behavior Factory ====== | ||
+ | |||
+ | Using element class supporting adding behaviors the behavior can be added like this: | ||
+ | <code xml> | ||
+ | <?xml version=' | ||
+ | < | ||
+ | < | ||
+ | <!-- set element properties. omit property prefix if used inside behavior tag --> | ||
+ | <string name=' | ||
+ | </ | ||
+ | | ||
+ | <!-- for adding multiple behaviors use unique identifiers --> | ||
+ | < | ||
+ | </ | ||
</ | </ | ||