Advertisement
Guest User

Change to TreeProto:run

a guest
Apr 8th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | None | 0 0
  1. function TreeProto:run(dt)
  2.     if self.running then
  3.         -- warn(debug.traceback("Tried to run BehaviorTree while it was already running"))
  4.         return
  5.     end
  6.        
  7.     local nodes = self.nodes
  8.     local obj = self.object
  9.     local i = self.index
  10.     local nodeCount = #nodes
  11.        
  12.     local didResume = self.paused
  13.     self.paused = false
  14.     self.running = true
  15.        
  16.     local nodeResult -- MY EDIT *******************************************************************************
  17.        
  18.     while i <= nodeCount do
  19.         local node = nodes[i]
  20.            
  21.         if node.type == "task" then
  22.             local task = node.task
  23.                
  24.             if didResume then
  25.                 didResume = false
  26.             elseif node.start then
  27.                 node.start(task, obj, dt)
  28.             end
  29.                
  30.             node.status = nil
  31.             node.run(task, obj, dt)
  32.                
  33.             if not node.status then
  34.                 warn("node.run did not call success, running or fail, acting as fail")
  35.                 node.status = "fail"
  36.             end
  37.                
  38.             if node.status == "running" then
  39.                 self.paused = true
  40.                 break
  41.             elseif node.status == "success" then
  42.                 if node.finish then
  43.                     node.finish(task, obj, dt)
  44.                 end
  45.                 i = node.onsuccess
  46.                    
  47.                 nodeResult = "success" -- MY EDIT *******************************************************************************
  48.             elseif node.status == "fail" then
  49.                 if node.finish then
  50.                     node.finish(task, obj, dt)
  51.                 end
  52.                 i = node.onfail
  53.                    
  54.                 nodeResult = "fail" -- MY EDIT *******************************************************************************
  55.             else
  56.                 error("bad node.status")
  57.             end
  58.         elseif node.type == "random" then
  59.             i = node.indices[math.random(1, #node.indices)]
  60.         else
  61.             error("bad node.type")
  62.         end
  63.     end
  64.        
  65.     self.index = i <= nodeCount and i or 1
  66.     self.running = false
  67.        
  68.     -- MY EDIT *******************************************************************************
  69.     if i == nodeCount+1 then
  70.         return nodeResult
  71.     else
  72.         return "running"
  73.     end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement