Conditional node and Behavior node, the design of the two nodes themselves is relatively simple, the project to write the behavior tree node is generally the extension of the two nodes, and the decorator and composite nodes only need to use the built-in is enough.
Their inheritance relationships are as follows:
Conditional->task
Action->task
The code is as follows:
Btaction.lua
1Btaction =bttask:new ();2 3 Localthis =btaction;4 5 functionthis:new ()6 Localn \ {};7 setmetatable(o, self);8Self.__index =Self ;9 returno;Ten End
Btconditional.lua
1Btconditional =bttask:new ();2 3 Localthis =btconditional;4 5 functionthis:new ()6 Localo = {};7 setmetatable(o, self);8Self.__index =Self ;9 returno;Ten End
Btlog.lua
1 --[[2 Reference Behaviordesigner-action-log3 --]]4Btlog =btaction:new ();5 6 Localthis =Btlog;7 8 functionthis:new (text)9 Localo = {};Ten setmetatable(o, self); OneSelf.__index =Self ; AO.text =text; - returno; - End the - functionthis:onupdate () - Print(self.text); - returnbttaskstatus.success; + End
Btisnullorempty.lua
1 --[[2 Reference Behaviordesigner-conditional-isnullorempty3 --]]4Btisnullorempty =btconditional:new ();5 6 Localthis =Btisnullorempty;7 8 functionthis:new (text)9 Localo = {};Ten setmetatable(o, self); OneSelf.__index =Self ; AO.text =text; - returno; - End the - functionthis:onupdate () - if( notSelf.textorSelf.text = ="") Then - returnbttaskstatus.success; + Else - returnbttaskstatus.failure; + End A End
Testbehaviortree.lua
1Testbehaviortree =btbehaviortree:new ();2 3 Localthis =Testbehaviortree;4 5 functionthis:new ()6 Localo = {};7 setmetatable(o, self);8Self.__index =Self ;9 this:init ();Ten returno; One End A - functionThis:init () - Localsequence =btsequence:new (); the LocalIsNullOrEmpty = Btisnullorempty:new (""); - LocalLog = Btlog:new ("This is a empty string"); - Sequence:addchild (isnullorempty); - sequence:addchild (log); + This:pushtask (sequence); - End
The output is as follows:
[Unity plugin] Lua Behavior Tree (iv): conditional nodes and Behavior nodes