public void Set(BehaviorTreeContext context, BehaviorTask node, T value) { switch (MemoryScope) { case BlackboardMemoryScope.Global: context.Agent.Blackboard.Set(Key, value, null, null); break; case BlackboardMemoryScope.Tree: context.Agent.Blackboard.Set(Key, value, context.Tree.Id, null); break; case BlackboardMemoryScope.Node: context.Agent.Blackboard.Set(Key, value, context.Tree.Id, node.Id); break; } }
public T Get(BehaviorTreeContext context, BehaviorTask node, T defaultValue = default(T)) { switch (MemoryScope) { case BlackboardMemoryScope.Global: return(context.Agent.Blackboard.Get <T>(Key, null, null, defaultValue)); case BlackboardMemoryScope.Tree: return(context.Agent.Blackboard.Get <T>(Key, context.Tree.Id, null, defaultValue)); case BlackboardMemoryScope.Node: return(context.Agent.Blackboard.Get <T>(Key, context.Tree.Id, node.Id, defaultValue)); } return(defaultValue); }
public BehaviorStatus ExecuteOn(BehaviorAgent agent) { try { BehaviorTreeContext context = new BehaviorTreeContext { Agent = agent, Tree = this, OpenNodes = new List <Guid>() }; Status = Root.Behave(ref context); List <Guid> previousOpenNodes = agent.Blackboard.Get <List <Guid> >("openNodes", Id); List <Guid> currentOpenNodes = context.OpenNodes; int start = 0; for (int i = 0; i < Math.Min(previousOpenNodes.Count, currentOpenNodes.Count); i++) { start = i + 1; if (previousOpenNodes[i] != currentOpenNodes[i]) { break; } } for (int i = previousOpenNodes.Count - 1; i >= start; i--) { Root.GetTaskById(previousOpenNodes[i])?.CloseInternal(ref context); } agent.Blackboard.Set <List <Guid> >("openNodes", currentOpenNodes, Id); agent.Blackboard.Set <int>("nodeCount", context.NodeCount, Id); } catch (System.Exception ex) { Game.LogTrivial($"[{this.GetType().Name}] Exception thrown when performing behavior tree: {ex}"); Status = BehaviorStatus.Failure; } return(Status); }
public BehaviorStatus Behave(ref BehaviorTreeContext context) { EnterInternal(ref context); if (!context.Agent.Blackboard.Get <bool>("isOpen", context.Tree.Id, this.Id)) { OpenInternal(ref context); } BehaviorStatus status = BehaveInternal(ref context); if (status != BehaviorStatus.Running) { CloseInternal(ref context); } ExitInternal(ref context); return(status); }
internal void ExitInternal(ref BehaviorTreeContext context) { context.OnExitNode(this.Id); OnExit(ref context); }
internal void CloseInternal(ref BehaviorTreeContext context) { context.OnCloseNode(this.Id); context.Agent.Blackboard.Set <bool>("isOpen", false, context.Tree.Id, this.Id); OnClose(ref context); }
internal BehaviorStatus BehaveInternal(ref BehaviorTreeContext context) { context.OnBehaveNode(this.Id); return(OnBehave(ref context)); }
internal void OpenInternal(ref BehaviorTreeContext context) { context.OnOpenNode(this.Id); context.Agent.Blackboard.Set <bool>("isOpen", true, context.Tree.Id, this.Id); OnOpen(ref context); }
internal void EnterInternal(ref BehaviorTreeContext context) { context.OnEnterNode(this.Id); OnEnter(ref context); }
protected virtual void OnExit(ref BehaviorTreeContext context) { }
protected virtual void OnClose(ref BehaviorTreeContext context) { }
protected virtual BehaviorStatus OnBehave(ref BehaviorTreeContext context) => BehaviorStatus.Success;
protected virtual void OnOpen(ref BehaviorTreeContext context) { }