title: Behavior Tree: How/When to return "running" state on leaf nodes?
body: <p>I've implemented a simple Behavior Tree algorithm which I intend to use inside a Unity project. It's mostly working as I expected, except I'm having trouble making proper use of it, which might be caused by some misunderstanding of mine. My problem is as follows:</p>
  
  <p>If a leaf node takes a long time to finish, for instance, an action that makes a game unit follow a specific path, that means the leaf node is currently at the "running" state. However, if I return the state, I can no longer run the rest of the function, which means I can't reach my destination.</p>
  
  <p>So my question is: what is the proper way to tell my tree that a leaf node is running?</p>
  
  <p>Here's what I got on my ActionNode class:</p>
  
  <pre><code>public class ActionNode : BTNode
  {
      public delegate NodeStates ActionNodeDelegate();
      protected ActionNodeDelegate nodeAction;
  
      public ActionNode(ActionNodeDelegate action)
      {
          nodeAction = action;
      }
      public override NodeStates Evaluate()
      {
          switch (nodeAction())
          {
              case NodeStates.FAILURE:
                  currentNodeState = NodeStates.FAILURE;
                  return currentNodeState;
              case NodeStates.SUCESS:
                  currentNodeState = NodeStates.SUCESS;
                  return currentNodeState;
              case NodeStates.RUNNING:
                  currentNodeState = NodeStates.RUNNING;
                  return currentNodeState;
              default:
                  currentNodeState = NodeStates.FAILURE;
                  return currentNodeState;
          }
      }
  }
  </code></pre>
  
  <p>If you need to check the other classes, it's all based on <a href="https://hub.packtpub.com/building-your-own-basic-behavior-tree-tutorial/" rel="nofollow noreferrer">this article</a>. Linking it for the sake of keeping the thread cleaner.</p>
  
  <p>As for an action example, consider this:</p>
  
  <pre><code>private NodeStates FollowPath() {        
      bool targetReached;
      if (targetReachable)
          targetReached = WalkToTarget();
      if (targetReached)
          return NodeStates.SUCESS;
      else
          return NodeStates.FAILURE;
  }
  </code></pre>
  
  <p>What I'm wondering is, if WalkToTarget takes a long time, how do I return a "running" state?</p>
  
  <p>I've read many different articles on the subject, but I can't seem to grasp the exact concept behind these states. I understand how I can, for instance, tell that a Sequence or Selector node is running.</p>
  
  <p>The only idea that crossed my mind is that I'd store the remaining path inside a variable at my enemy class and call the method every game update tick, but I feel like that's just poor coding.</p>
  
  <p>Are leaf nodes supposed to return "running" at any given moment? If so, in which cases would that happen? Any examples would be appreciated!</p>