User Tools

Site Tools



behaviortree

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
behaviortree [2016/12/01 15:41] fxfirebehaviortree [2022/07/18 09:27] (current) fxfire
Line 1: Line 1:
 =====Behavior Tree Framwork===== =====Behavior Tree Framwork=====
-Behavior Trees are a wonderful thing when it comes to writing any kind of AI logic. It is currently in its "first working version" and there are still improvements coming in the future.  +Behavior Trees are a wonderful thing when it comes to writing any kind of AI logic. It is currently in its "first working version" and there are still improvements coming in the future. 
-GW2Minion is completely written with this framework. FFXIVMinion is not making use of this, but it *could* be used as well.+  
 +**FFXIVMinion** is not making use of this, but it *could* be used as well. Instead, it uses the 'older' Cause&Effect framework. Example Code how to build your own addon : {{ ::newaddon.zip |}}
  
 **BT Basics, How they work:** [[http://www.gamasutra.com/blogs/ChrisSimpson/20140717/221339/Behavior_trees_for_AI_How_they_work.php|Behavior trees for AI]] **BT Basics, How they work:** [[http://www.gamasutra.com/blogs/ChrisSimpson/20140717/221339/Behavior_trees_for_AI_How_they_work.php|Behavior trees for AI]]
Line 41: Line 42:
   
  -- Load all our local "bot/addon" BTree files  -- Load all our local "bot/addon" BTree files
- BehaviorManager:LoadBehaviorFromFolder(GetLuaModsPath()  .. "\\\GW2Minion\\\SillyJumper")+ BehaviorManager:LoadBehaviorFromFolder(GetLuaModsPath()  .. "\\\SillyJumper")
   
 end end
Line 69: Line 70:
 {{youtube>MMASYe_FEuU?medium}} {{youtube>MMASYe_FEuU?medium}}
  
 +EDIT: Above the lines where you add the Checkbox, you need to make sure the used variable is NOT nil, so put this above: 
 +<code lua> 
 +if (not Settings.sillyjumper.jump ) then Settings.sillyjumper.jump = false end 
 +</code>
 \\ \\
  
Line 112: Line 116:
  Reload = function() sillyjumper.LoadBehaviorFiles() end,   Reload = function() sillyjumper.LoadBehaviorFiles() end,
  -- if set, it will be treated as a private addon, loadable from the addon store  -- if set, it will be treated as a private addon, loadable from the addon store
- private = true,+ private = false,
   
  }  }
Line 201: Line 205:
  Reload = function() sillyjumper.LoadBehaviorFiles() end,   Reload = function() sillyjumper.LoadBehaviorFiles() end,
  -- if set, it will be treated as a private addon, loadable from the addon store  -- if set, it will be treated as a private addon, loadable from the addon store
- private = true,+ private = false,
                    
                         -- (optional) Callback function, is called by the BTree Framework when the BTree is started. Allows us to supply a custom "context" table to the BTree:                         -- (optional) Callback function, is called by the BTree Framework when the BTree is started. Allows us to supply a custom "context" table to the BTree:
  LoadContext = function() return sillyjumper.GetContext() end,  LoadContext = function() return sillyjumper.GetContext() end,
 +
 +                        -- (optional) Function to draw the menu elements that always appears on the "Main menu" interface.
 +                        DrawMenuCode = function(btree) end,
 +                                               
 +                        -- (optional) Function to draw the menu elements that appears on the "Main menu" interface when the btree is selected.
 +                        -- This locks the edit field inside the btree editor.
 +                        DrawMainMenuCode = function(btree) end,
 +                        
 +                        -- (optional) Function to draw the menu elements that appears on the subtree node. This is also used for drawing custom properties in the taskmanager.
 +                        -- This locks the edit field inside the btree editor.
 +                        DrawSubMenuCode = function(btree) end
  }  }
 </code> </code>
Line 226: Line 241:
 </code> </code>
  
-+  * And now you can access this function in any node in your BTree with //context.Jump()// 
 +{{::bttutorial_ingame2.png?direct&200|}}
  
 \\ \\
-\\ 
-\\ 
-Dont mind the stuff below...just things I dont want to forget when finising this tutorial ;) 
-======== 
-Please be aware that this GUI 
  
- -- The "variablestable is always the same for all "instances" of the node.  +=== 8. Creating Private SubTrees === 
- -- The "settingstable holds values which can be different for each "instanceof node.+In order to create and use a private subtree, you will need to extend the loading code again by a new function: 
 +<code lua> 
 +function sillyjumper.LoadBehaviorFiles() 
 +  
 + -- The BehaviorTree Framework requires specific information in order to use an "external  private BTree"
 + local btreeinfo = { 
 + -- Our Main Behaviortree filename: 
 + filename = "sillyjumper.bt", 
 + -- Our Foldername and a mandatory subfolder where our "sillyjumper.btwill be loaded from: 
 + filepath = GetLuaModsPath()  .. "\\\SillyJumper\\data",  
 + -- Callback function, when the BTree was changed and saved in the BT-Editor. Reload all addon bt files here, including private subtrees. 
 + Reload = function() sillyjumper.LoadBehaviorFiles() end,  
 + -- if set, it will be treated as private addon, loadable from the addon store 
 + private = false, 
 + -- (optional) Callback function, is called by the BTree Framework when the BTree is startedAllows us to supply a custom "context" table to the BTree: 
 + LoadContext = function() return sillyjumper.GetContext() end, 
 +  
 + -- Required for private addons with additional private sub-behavior trees 
 + LoadSubTree = function(filename) return sillyjumper.LoadSubtreeData(filename) end, 
 + }
  
 +</code>
  
 +  * And then define this new Callback function (in my case, sillyjumper.LoadSubtreeData(filename)). This will load the requested file from your private addon folder.
  
- +<code lua> 
---- +-- Required for private addons with additional private sub-behavior trees 
-common errors in Node Code: +function sillyjumper.LoadSubtreeData(filename) 
-if ( Player.alive ) then + if ( FileExists(GetLuaModsPath()  .. "\\SillyJumper\\data\\"..filename)) then  
-    Inventory:DepositCollectables() + return FileLoad(GetLuaModsPath()  .. "\\SillyJumper\\data\\"..filename) 
-    self:success()+  
 + else 
 +  
 + local files = sillyjumper.modulefunctions.GetModuleFiles() 
 + if(table.valid(files)) then 
 + for _,filedata in pairs(files) do 
 + if( btreeinfo.filename == filedata.f) then 
 + local fileString = sillyjumper.modulefunctions.ReadModuleFile(filedata
 + if(fileStringthen 
 + local fileFunction, errorMessage = loadstring(fileString) 
 + if (fileFunction) then 
 + return fileFunction() 
 + end 
 + end 
 + break 
 + end 
 + end 
 + end 
 + end
 end end
-self:fail()+</code>
  
-must be +  * You should now be all setup for creating and loading private SubTrees and building your own Addon.
-if ( Player.alive ) then +
-    Inventory:DepositCollectables() +
-    self:success() +
-else +
-    self:fail() +
-end+
  
 +\\
  
  
 +=== 9. Creating a private Subtree ===
  
- To start an external btree that takes control of the bot +  * Todo ...it works, but this is a larger time consuming part I'll do when I have time :D 
- if(ml_bt_mgr.running and not ml_bt_mgr.paused) then +  
- ml_bt_mgr.paused = true +
- my_btree:start() +
- end +
-  +
- if(my_btree and ml_bt_mgr.paused) then my_btree:run() end +
-  +
- The bot main pulse will continue to run, but the main btree will not. +
-  +
- When your task is completed remember to set ml_bt_mgr.paused to false+
  
 +=== 10. Common Errors and Infos ===
 +  * You ALWAYS need to be aware that if you save for example a "target", which is a metatable and directly linked to the game memory, in a context-table variable or anywhere else, you __cannot access__ or use that in a later call / pulse of the Behaviortree. You always need to make sure that the metatable you try to access still exists, else you will experiement the weirdest crashes.
 +  * You ALWAYS need to return a self:success(), self:fail() or self:running() in every if-else-end case inside your action nodes. If you miss even one case, your whole Tree will not work.
 +  * You can access each node's data by using self.settings, self.variables, self.info and ofcourse through the shared context table.
 +  * There are additional BehaviorManager functions that might be helpful to you:
 +
 +=== 11. BTreeinfo ===
 +<code lua>
 +local btreeinfo = {
 + -- Our Main Behaviortree filename:
 + filename = string,
 +
 + -- Our Foldername and a mandatory subfolder where our btree will be loaded from:
 + filepath = string,
 +
 + -- Callback function, when the BTree was changed and saved in the BT-Editor. Reload all addon bt files here, including private subtrees.
 + Reload = function() end,
 +
 + -- if set, it will be treated as a private addon, loadable from the addon store
 + private = false,
 +
 + -- if set, the btree will not appear in the botmode dropdown
 +        internal = false,
 +        
 +        -- Preloaded task data. Important when using a private addon.
 +        data = table,
 +        
 + -- (optional) Callback function, is called by the BTree Framework when the BTree is started. Allows us to supply a custom "context" table to the BTree:
 + LoadContext = function() end,
 +
 + -- (optional) Function to draw the menu elements that always appears on the "Main menu" interface. (Requires internal)
 + DrawMenuCode = function(btree) end,
 +    
 + -- (optional) Function to draw the menu elements that appears on the "Main menu" interface when the btree is selected.
 + -- This locks the edit field inside the btree editor.
 + DrawMainMenuCode = function(btree) end,
 +
 + -- (optional) Function to draw the menu elements that appears on the subtree node. This is also used for drawing custom properties in the taskmanager.
 + -- This locks the edit field inside the btree editor.
 + DrawSubMenuCode = function(btree) end,
 +
 + -- Required for private addons with additional private sub-behavior trees
 + LoadSubTree = function(filename) end,
 +
 +}
 +</code>
 +
 +=== 12. BehaviorManager functions ===
 +<code lua>
 +BehaviorManager:ToggleMenu()
 +BehaviorManager:Ready()
 +BehaviorManager:Running()
 +BehaviorManager:Start()
 +BehaviorManager:Stop()
 +BehaviorManager:GetTicksThreshold()  -- How often (in ms) the BTree is being called.
 +BehaviorManager:SetTicksThreshold(milliseconds)
 +BehaviorManager:GetLastTick()
 +BehaviorManager:SetLastTick(ticks)
 +BehaviorManager:LoadBehaviorFromFolder(folderpath)
 +BehaviorManager:LoadBehavior(btreeinfo)
 +BehaviorManager:LoadSubBehavior(btreeinfo)
 +BehaviorManager:Paused(p)
 +BehaviorManager:CurrentBTreeName()
 +BehaviorManager:SetBtreeFile(botmode_name)
 +</code>
behaviortree.1480606915.txt.gz · Last modified: 2016/12/01 15:41 by fxfire