Add Selector node to BT

This commit is contained in:
Wojtek Figat
2023-08-21 00:07:25 +02:00
parent fce82247ab
commit a6e503d21b
2 changed files with 54 additions and 0 deletions

View File

@@ -99,6 +99,40 @@ BehaviorUpdateResult BehaviorTreeSequenceNode::Update(BehaviorUpdateContext cont
return result;
}
int32 BehaviorTreeSelectorNode::GetStateSize() const
{
return sizeof(State);
}
void BehaviorTreeSelectorNode::InitState(Behavior* behavior, void* memory)
{
auto state = GetState<State>(memory);
new(state)State();
}
BehaviorUpdateResult BehaviorTreeSelectorNode::Update(BehaviorUpdateContext context)
{
auto state = GetState<State>(context.Memory);
if (state->CurrentChildIndex >= Children.Count())
return BehaviorUpdateResult::Failed;
auto result = Children[state->CurrentChildIndex]->InvokeUpdate(context);
switch (result)
{
case BehaviorUpdateResult::Success:
return BehaviorUpdateResult::Success;
case BehaviorUpdateResult::Failed:
state->CurrentChildIndex++; // Move to the next node
if (state->CurrentChildIndex < Children.Count())
result = BehaviorUpdateResult::Running; // Keep on running to the next child on the next update
break;
}
return result;
}
int32 BehaviorTreeDelayNode::GetStateSize() const
{
return sizeof(State);

View File

@@ -45,6 +45,26 @@ private:
};
};
/// <summary>
/// Selector node updates all its children (from left to right) until one of them succeeds. If all children fail, the selector fails.
/// </summary>
API_CLASS() class FLAXENGINE_API BehaviorTreeSelectorNode : public BehaviorTreeCompoundNode
{
DECLARE_SCRIPTING_TYPE_WITH_CONSTRUCTOR_IMPL(BehaviorTreeSelectorNode, BehaviorTreeCompoundNode);
public:
// [BehaviorTreeNode]
int32 GetStateSize() const override;
void InitState(Behavior* behavior, void* memory) override;
BehaviorUpdateResult Update(BehaviorUpdateContext context) override;
private:
struct State
{
int32 CurrentChildIndex = 0;
};
};
/// <summary>
/// Root node of the behavior tree. Contains logic properties and definitions for the runtime.
/// </summary>