| 79 | } |
| 80 | |
| 81 | void Behavior::UpdateAsync() |
| 82 | { |
| 83 | if (_result != BehaviorUpdateResult::Running) |
| 84 | return; |
| 85 | const BehaviorTree* tree = Tree.Get(); |
| 86 | if (!tree || !tree->Graph.Root) |
| 87 | { |
| 88 | _result = BehaviorUpdateResult::Failed; |
| 89 | Finished(); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Update timer |
| 94 | _accumulatedTime += Time::Update.DeltaTime.GetTotalSeconds(); |
| 95 | const float updateDeltaTime = 1.0f / Math::Max(tree->Graph.Root->UpdateFPS * UpdateRateScale, ZeroTolerance); |
| 96 | if (_accumulatedTime < updateDeltaTime) |
| 97 | return; |
| 98 | _accumulatedTime -= updateDeltaTime; |
| 99 | _totalTime += updateDeltaTime; |
| 100 | |
| 101 | // Update tree |
| 102 | BehaviorUpdateContext context; |
| 103 | context.Behavior = this; |
| 104 | context.Knowledge = &_knowledge; |
| 105 | context.Memory = _knowledge.Memory; |
| 106 | context.RelevantNodes = &_knowledge.RelevantNodes; |
| 107 | context.DeltaTime = updateDeltaTime; |
| 108 | context.Time = _totalTime; |
| 109 | const BehaviorUpdateResult result = tree->Graph.Root->InvokeUpdate(context); |
| 110 | if (result != BehaviorUpdateResult::Running) |
| 111 | _result = result; |
| 112 | if (_result != BehaviorUpdateResult::Running && tree->Graph.Root->Loop) |
| 113 | { |
| 114 | // Reset State |
| 115 | _result = BehaviorUpdateResult::Running; |
| 116 | } |
| 117 | else if (_result != BehaviorUpdateResult::Running) |
| 118 | { |
| 119 | Finished(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void Behavior::StartLogic() |
| 124 | { |
no test coverage detected