Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AI: Behaviour trees in Unity: Behave 1.1 released

Discussion in 'Assets and Asset Store' started by AngryAnt, Sep 29, 2010.

  1. samim

    samim

    Joined:
    Jan 4, 2009
    Posts:
    13
    @bryanleister what u have there looks good but i think you have to additionally implement the basic tick handeling in your script for it to work. something like this:

    Code (csharp):
    1.        
    2.        public BehaveResult Tick (Tree sender, bool init)
    3.     {      
    4.                return BehaveResult.Success;
    5.     }
    6.    
    7.    
    8.     public void Reset (Tree sender)
    9.     {  
    10.        
    11.     }
    12.    
    13.     public int SelectTopPriority (Tree sender, params int[] IDs)
    14.     {
    15.         return IDs[0];
    16.     }
    17.  
     
  2. tridodinh

    tridodinh

    Joined:
    Apr 30, 2011
    Posts:
    1
    Hi,
    I'm trying to figure out how the Reset methods work since it isn't fully explained in the documentation. When exactly is the generic Reset method called? It seems to be multiple times once an action finishes. And when are the specific Reset{Name}{Action|Decorator} methods being called? Somehow I can't get them to run. Do I need to do anything else besides implementing them?
     
  3. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    @samim Thanks! I don't know why I didn't just watch the video all the way through, kept thinking that I should test my syntax earlier, it was explained in the video.

    I am trying to fix the Kitten demo to run with the current version of Unity (3.3) and Behave 1.3. If I get it up and running, I'll post it so that others can learn from it. It definitely helps me to have an example of working code that I can look at to understand what is happening with the BT.

    As it stands now, I understand how to make a tree, and the basic functions of each node but without a working example that includes scripts it's hard to start understanding how to use something like this in a game design.

    Edit (Again) : OK, I was able to find an old mac, reinstall Unity 2.5 and copy the naming and tree structure from the original Kitten demo. Whew! It mostly works, I think the only thing I am missing is understanding how to Tick the kittens properly.

    This is some of the code that no longer works from the 2.5 demo:

    Code (csharp):
    1.  
    2.         tree = BLNewBehaveLibrary0.Instance.Tree( ( int )BLNewBehaveLibrary0.TreeType.Kitten_Default, this );
    3.         tree.SetTickForward( ( int )BLNewBehaveLibrary0.ActionType.TargetKitten, TickTargetActions );
    4.         tree.SetTickForward( ( int )BLNewBehaveLibrary0.ActionType.TargetFood, TickTargetActions );
    5.         tree.SetTickForward( ( int )BLNewBehaveLibrary0.ActionType.TargetRandom, TickTargetActions );
    6.             // Setting up the target actions to be handled by the same method since the success criteria is
    7.             // the same and we might want to alter it later
    8.     }
    9.    
    I changed it to this, and it does work now. The kittens are running around, doing their thing.

    Code (csharp):
    1.  
    2.  
    3.         tree = BLNewBehaveLibrary0.InstantiateTree(
    4.             BLNewBehaveLibrary0.TreeType.Kitten_Default,
    5.             this
    6.         );
    7.  
    8.         tree.SetTickForward( ( int )BLNewBehaveLibrary0.ActionType.TargetKitten, TickTargetActions );
    9.         tree.SetTickForward( ( int )BLNewBehaveLibrary0.ActionType.TargetFood, TickTargetActions );
    10.         tree.SetTickForward( ( int )BLNewBehaveLibrary0.ActionType.TargetRandom, TickTargetActions );
    11.             // Setting up the target actions to be handled by the same method since the success criteria is
    12.             // the same and we might want to alter it later
    13.     }
    14.    
    15.  
    I had to delete some of the parameters being passed into the functions, so I don't think it's totally working right yet. I changed this:

    Code (csharp):
    1.     public BehaveResult TickTargetActions( bool init, Tree sender )
    to this:
    Code (csharp):
    1.     public BehaveResult TickTargetActions( Tree sender )

    AngryAnt, I would be glad to upload the entire Kitten demo if it is OK once I get it working so that others can use the scene again to learn how to use your most excellent tool!
     
    Last edited: May 2, 2011
  4. ggblake

    ggblake

    Joined:
    Oct 1, 2010
    Posts:
    11
    How does one use blue agents ??
     
  5. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @tridodinh
    Reset is indeed being called too often at the moment - a fix for it is on my TODO, but has seemed less critical than other fixes and improvements so far.

    The general reset works just like the general tick. It's called when any action or decorator is reset and the component responsible for the reset can be accessed from the Tree sender argument .ActiveID.

    A component is reset when it returns non-running or it is interrupted. Notice that when a node with child nodes is reset, it will reset its child nodes as well.

    @ggblake
    Agent blueprints are generated abstract classes implementing IAgent. When doing a debug build you can see the agent blueprint names in the generated debug txt file.
     
  6. ggblake

    ggblake

    Joined:
    Oct 1, 2010
    Posts:
    11
    so what can I do with them and where are they generated
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @ggblake:
    Agent blueprints can be used as an alternative to simply directly implement IAgent in your class and relying on reflection to connect handlers.

    When inheriting your agent from a blueprint, you save the reflection cost and you get code completion on your handlers.

    I would recommend you fire up monodevelop, try creating a class inheriting from an agent blueprint and check out the available virtual methods within it.

    The abstract class itself lives within the generated assembly - same as the trees.
     
  8. raffi

    raffi

    Joined:
    Aug 1, 2009
    Posts:
    15
    hello,

    I'm getting the same error that some had on page 2 of this thread.

    This is when I try to 'Build library debug' but it also happens for release. The tree is the same one from your video 'from tree to code'.

    I'm using Unity free/iphone basic (3.3.0f4) on Mac os 10.6.7.

    Is there anything I can do to fix this?

    EDIT: I'm going to install the mono runtime like you suggested in a prior post and see if that works. I'll update here with the results.

    After installing the mono runtime, I now get a popup error that says "Internal compiler error." It shows no errors in the console, however.
     
    Last edited: May 10, 2011
  9. pimthegreat

    pimthegreat

    Joined:
    Aug 15, 2010
    Posts:
    7
    Hi, I'm trying out using Agent Blueprints and the word completion is very nice to have. However as far as I can figure out a C# script can't have 2 base classes and I have my AI implemented inheriting from MonoBehavior.
    I wan't this:
    Code (csharp):
    1.  
    2. public class MyAI : MonoBehaviour, BAMyLibrary_MyBlueprint{
    3. //MonoBehaviour and IAgent methods
    4. }
    But it doesn't work and I'm forced to do this:
    Code (csharp):
    1.  
    2. public class MyAI : BAMyLibrary_MyBlueprint{
    3. //IAgent methods
    4. }
    Should i just use the IAgent interface instead or is there some other design pattern that is recomended?
     
  10. Gajan

    Gajan

    Joined:
    May 16, 2011
    Posts:
    3
    Hi Emil,

    I posted the following message in the "Behavioural AI in unity: Behave 1.0 is out!" in unity forum. I tried to post in your website but I could not as it always say "Invalid data"

    I am trying to build and run the unity demo on behav, which I found on the Asset store. I also tried the project I got from this website, which I could not build.

    When I tried to build the demo project in the unity editor, I got the following compile errors (see below).

    Configuration I am using:

    Windows 7
    Unity 3.1.0f4
    Microsoft Visual Studio 2010


    I would like to know if anyone successfully built and run the unity behav demo in the above configuration?

    - Using Behave Library, Is it possible to generate the behavior tree automatically from an external application and use the behavior tree logic in a game like scenario further?

    Thank you in advance.

    Best
    Gajan


    Assets/Scripts/Kitten.cs(7,38): error CS0246: The type or namespace name `IAgent' could not be found. Are you missing a using directive or an assembly reference?

    Assets/Scripts/NewBehaveLibrary0Test.cs(5,53): error CS0246: The type or namespace name `IAgent' could not be found. Are you missing a using directive or an assembly reference?

    Error building Player because scripts had compiler errors
    UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()


    Error building Player because scripts had compiler errors
    UnityEditor.HostView:OnGUI()

    Assets/Scripts/Kitten.cs(7,38): error CS0246: The type or namespace name `IAgent' could not be found. Are you missing a using directive or an assembly reference?


    Assets/Scripts/NewBehaveLibrary0Test.cs(5,53): error CS0246: The type or namespace name `IAgent' could not be found. Are you missing a using directive or an assembly reference?
     
  11. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @Gajan:
    I did not upload any Behave demo to the asset store. Please supply a link to the asset and I'll look into it. I sincerely hope you didn't pay anything for the asset.

    @pimthegreat:
    Unfortunately those are your options, yes. In the next version of Behave you'll have an "inherits from MonoBehaviour" toggle in the setup of blueprints. Until then you'll have to choose between a non-MonoBehaviour agent or an IAgent implementing MonoBehaviour.
     
  12. pimthegreat

    pimthegreat

    Joined:
    Aug 15, 2010
    Posts:
    7
    @AngryAnt:
    Alright thanks for the reply. The blueprints are a very nice idea. I look forward to future uppdates like this one making it even more awesome, especialy with the teaser of that little trick you're planning. Keep up the good work :)
     
  13. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    Does this work with javascript?
     
  14. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Yes.

    Read back earlier in the thread for example usage.
     
  15. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124
    I get a broken interface in the debugger window at runtime and thus can't really debug what's going on.
    I'm I doing something wrong?

    $behave_debugger.jpg
     
  16. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    That's really interesting. Is it broken all the time or only when diving into subtrees? Or only when diving into specific subtrees?
     
  17. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124
    It seems to occur only on specific subtrees as it works nice and smooth on another (much simpler) project.
     
  18. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Could you look into what makes these subtrees different layout wise and try fiddling around with them a bit? I don't really have much to go on here.
     
  19. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124
    I'll try to debug it and set a minimal test scene.

    Meanwhile, is there a way to get the name of the current subtree on an action/decorator tick?
     
  20. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    I don't have the Behave source with me right now, so I'm not sure if the sender parameter passed to the tick method is overridden with a reference to the original tree of the tick or if it is the subtree.

    Try logging out sender.ToString ();
     
  21. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124
    Thanks.
    Indeed, the trick is to check sender.ActiveReferenceTree:
    if sender.ActiveReferenceTree is not null, you're in a subtree and sender.ActiveReferenceTree.ToString() returns "<collection name>.<subtree name>".
     
  22. psionic81

    psionic81

    Joined:
    Mar 3, 2011
    Posts:
    22
    Hi there,

    I'm wondering if it's possible to disable the debug logging from the release library builds? I'm getting too much "AngryAnt Behave version 1.3 loading ..." in my console output. It's not really that performance intensive on PC but on my iOS builds its like 30x3x2=180 messages at once for all my enemies to spawn, and that spawning happens every 30 seconds.


    Chris.
     
  23. Erick

    Erick

    Joined:
    Mar 21, 2011
    Posts:
    14
    I'm having the same problem psionic81 is having. Using blueprints I was able to save like 15ms(spawning 4 of my enemies), but there are still 36ms just for Debug.Log stuff and it causes serious lag spikes even on builds.
     
  24. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124
    Could you please tell me how you managed to get a "Behave" submenu when you right click on a game object in project view?
    Is it a basic feature of editor scripting? Couldn't find anything about it in reference doc :(
     
  25. dilly123

    dilly123

    Joined:
    Nov 1, 2009
    Posts:
    23
    thank you for the update
     
  26. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124
    Never mind, I eventually found out by myself when I saw it was in Assets menu too :)

    The solution is to tag your method with the MenuItem attribute with a path starting by "Assets/".

    Example:
    Code (csharp):
    1.  
    2. [MenuItem("[B]Assets[/B]/YourSubMenuName/YourItemName")]
    3. public static void YourMethod()
    4. {
    5.   // your code ([URL="http://unity3d.com/support/documentation/ScriptReference/Selection-activeObject.html"]Selection.activeObject[/URL] should help you there)
    6. }
    7.  
     
  27. Eurico

    Eurico

    Joined:
    Jun 18, 2011
    Posts:
    2
    Hi there :)

    First of all thanks a lot for the great software.
    I read through almost all the documentation I could find about Behave and tried to look through the information contained in this thread but I just want to make sure I understand something clearly:
    In the editor's description itself, you describe a sequence as a node that executes from left to right. If one nodes returns Running, it gets retickeds. If it returns success we move to the next node inline.

    I did not find any information on what happens when one child returns false so I tested it. It seems that the next child to be executed is still the next one in line (to the right), just as when we return success. I was kind of expecting that after a failure this sequence node would restart from the beginning.

    Is that behavior the one implemented? Am I perhaps missing something? If it is their a way to 'reset' the state of that single selector?


    Thanks a lot.

    Best regards,
    Eurico
     
  28. Gajan

    Gajan

    Joined:
    May 16, 2011
    Posts:
    3
    I am sorry for the late response, I meant that I got the the Behave library not the demo. Thank you for your reply anyway.
     
  29. Eurico

    Eurico

    Joined:
    Jun 18, 2011
    Posts:
    2
    In response to my own question, it seems that I misunderstood the usage of decorators. I assumed that returning failure within the decorator would return failure, but it returns success (task complete) as per its description in the editor. The sequence node works just as expected.
     
  30. samim

    samim

    Joined:
    Jan 4, 2009
    Posts:
    13
    @AngryAnt
    Kudos one more time for the awesome work you did on behave! Btrees and Behave have truly changed my year to the better ;-)

    Now i´m working every day with behave in a production environment, some key missing features are becoming even more needed:
    Copy&Past // Higher ZoomOut Levels // Not having to reload the tree editor each time i change my scripts

    Some other little desires i have to but those are the main ones. Hope some of it makes the next release maybe! ;-)
     
  31. samim

    samim

    Joined:
    Jan 4, 2009
    Posts:
    13
    It would be super useful to get a scrollbar in the behave library browser. With a ever growing tree these "little" things are becoming very important.
     
  32. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @samim

    Glad to hear it's making you more productive :)

    Whoops. Missing scrollbar in library view is clearly an oversight. Added to TODO.

    Regarding copy and paste, what is your use case for this and how are references not covering it?

    Zoom needs to be done right, I agree. Basically the zoom thats in there right now is a quick hack. For example labels remain unscaled. I'll look into doing proper zooming.

    I've got a feature branch going which combats Unitys script reloading. No promises yet, but the base concept seems to be working just fine.
     
  33. samim

    samim

    Joined:
    Jan 4, 2009
    Posts:
    13
    @angryant cool stuff, good to hear about your recent progress!

    - Regarding copy&past: I do use references extensively which are fantastic.
    But often i just need to copy a few blocks from one subtree to an other, for which i can´t justify creating a new tree.
    This then means: "Goto other subtree, find check block name/string/float values, go back to other subtree, type in name...". With lots of different blocks and many subtrees this just gets very cumbersome.

    - Besides copy&past, ideally all created/detected blocks should show up in some kind of "library pool" from where you could just drag stuff in, would help the communication with designers allot to.

    - A simple but very effective addition would be custom color coding of blocks. Especially key in the debugger for current block, the light gray really is hard to spot.

    - Regarding Zoom: the current "hack" seems to effect the debugger to, making it for lots of trees completely unusable as half the stuff will be rendered offscreen. Your developing on a 100inch screen? ;-)

    - far more critical is a strange bug i´ve noticed when the tree grows to large sizes (70 sub-trees, around 80 unique blocks):
    On certain systems (newest gen macbook pro) the automatic linking of handlers via reflectionwill choke the entire computer and then kill unity on game-start. This happens after i reached a certain connected blocks count threshold. In the profiler and my debugs i can clearly see behave jumping up to extrem values before unity dies. I´m assuming Apple changed some memory max-heap-size or what not in there newest systems - as this bug does not happen on PC´s so far. I haven´t tried blueprints or SetForward yet, you think that might be the solution? Anyhow, please let me know if i can assist in anyway tracing this bug.

    - and finally, removing those debugs from the release-build on game start would be very welcome.
     
    Last edited: Jul 12, 2011
  34. samim

    samim

    Joined:
    Jan 4, 2009
    Posts:
    13
    quick update regarding those on-game-start crashes i mentioned at the bottom of my last post here:
    - seems like behave/unity/osx does not like me trying to initialize 80 agents all at once with in a few frames on-game-start.
    - the solution for now is to space out the loading/tree-init over a few seconds with a yield random sec: crashes gone.
     
  35. Ender314

    Ender314

    Joined:
    Aug 12, 2011
    Posts:
    55
    I want to start playing around with behave, but have yet to find a sample that will run with Unity 3.4. Keep getting errors about conflicts with "Tree", or when i fix that "type or name space 'Instance' does not exist in namespace "TreeEditor". Anyone know of a sample that works out of box with Unity 3.4?
     
  36. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @Ender314:
    Put the following at the beginning of your script to resolve the name clash:
    using Tree = Behave.Runtime.Tree;

    Not sure about the second error. I'll need some more information to assist. I'm working on the next version of Behave currently and have experienced no 3.4 compatibility issues.
     
  37. Ender314

    Ender314

    Joined:
    Aug 12, 2011
    Posts:
    55
    So "using Tree = Behave.Runtime.Tree;" right after "using BehaveLibrary;" didnt seem to work but "using Tree = BehaveLibrary.Tree;" seemed to do the trick for that error.

    Now I get the error:
    Assets/Editor/BehaveComponentEditor.cs(11,32): error CS0234: The type or namespace name `Instance' does not exist in the namespace `TreeEditor'. Are you missing an assembly reference?

    I am trying to run the kitten example you have on you website, but with the latest Behave that you gave the download link for (to the asset store). Is there another example i could look at?
     
  38. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    The Kitten example is not compatible with the latest version of Behave - the download page should already be specifying compatibility information.

    Your issues stem from the mixing of two different versions.
     
  39. Ender314

    Ender314

    Joined:
    Aug 12, 2011
    Posts:
    55
    What is an example that is compatible with the latest behave?
     
  40. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    There is unfortunately not one available at this time.
     
  41. Ender314

    Ender314

    Joined:
    Aug 12, 2011
    Posts:
    55
    Is there a plan to add one or two in the near future? I have read alot about behavior trees but i have yet to find a concrete example that would provide a basic combat AI, utilizing sequences, selectors, and decorators.
     
  42. feanix

    feanix

    Joined:
    Sep 9, 2011
    Posts:
    2
    EDIT: If anyone's interested in seeing how this is all meant to work you can see it here. Super rough prototype, did not use decorators at all. All that logic was in the actions. Using the A* Pathfinding Project for pathfinding.

    I'm having some trouble trying to get decorators to work. I have the following tree:



    Both sides of the tree are fairly similar.

    - Find* : Look for a thing to go to. Find thing: Success, Don't find thing: Fail
    - Travel* : Pathfind to. Pathfinding: Running, At location = success
    - Do*: Do whatever needs to be done there. Doing= running, finished = Success

    I know i need to put in a few failures for catastrophic sitauations where things arent as they're meant to be, but I'm trying to keep it simple for now.

    Previously I had this working perfectly without decorators. The DoWork action would constantly monitor a character's bladder level and cause the action to fail when it got too high. Then the FindUrinal action would check to make sure the bladder level was high enough to need to go to the toilet.

    I felt like I should be abstracting this decision making logic into the behaviour tree. My approach has been to make a simple bladder level filter decorator. Here's the code:

    Code (csharp):
    1.  
    2. public float bladder = 0;       // bladder level. 0 = empty, 1 = full
    3. public float bladderThresh = 0.7F;  // point at which the character wants to go for a pee
    4.  
    5. // do we need to pee?
    6. public BehaveResult TickBladderDecorator (Tree sender) {
    7.    
    8.     Debug.Log("Ticking Bladder Decorator");
    9.        
    10.     if (bladder > bladderThresh) {
    11.         Debug.Log("BladDec Success. We need to pee.");
    12.         return BehaveResult.Success;
    13.     } else {
    14.         Debug.Log("BladDec Failure. We don't need to pee.");
    15.         return BehaveResult.Failure;
    16.     }
    17.        
    18. }
    The idea is that I can use the same decorator and just invert the decorator for the work tree. Here's what I though would be the sequence:


    1. Selector runs Inverted Bladder
    2. Inverted Bladder returns Success (bladder < bladderThresh)
    3. Run through all the actions in the work sequence, halting when at DoWork because DoWork returns running until bladder > bladderThresh
    4. DoWork fails, the work sequence fails, and the Inverted Bladder decorator returns Success
    5. The main sequence ticks Bladder, which returns a success (bladder > bladderThresh)
    6. This kicks off the urination tree, which ultimately ends when Bladder has been reduced to 0 and the UseUrinal action fails, causing the entire tree to fail.
    7. The whole thing starts over.

    What actually happens is that the process stalls at #2. It gets to the Inverted Bladder decorator and because to return Failure over and over and over:


    I'm not sure if it's me doing something wrong here or if there's something weird going on with the decorators. :/ Has anyone used decorators like this at all?
     
    Last edited: Sep 9, 2011
  43. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    The invert flag inverts the return value of the component - which for the action is equal to the return value of your handler, but for decorators this is not the case.

    Your handler code is utterly unaffected by the invert flag. Its signal is still transmitted to the component, however the return value of the component - once affected by the handler code - is inverted.

    I'd like to give decorators an overhaul, since many keep misunderstanding them in the same way. However it looks like doing to with full asset auto-update will be a tad difficult without breaking functionality. I'm therefore postponing that overhaul to a 2.0 version and doing another point release before that - including various fixes and additions which have been underway for a while.
     
  44. feanix

    feanix

    Joined:
    Sep 9, 2011
    Posts:
    2
    I'm not sure if I understood you there. Are you saying that:

    • Actors and decorators each have a component.
    • With actions, the component equals whatever the return of the tick method was.
    • With decorators the component does not necesarily equal what the return of the tick component was

    If that is so, do I need to filter on a condition, do I need one decorator to filter for true and and completely separate decorator to filter for false?

    Thanks for your help so far!
     
  45. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    177
    I've been reading up on Behaviour Trees and AngryAnt's behave library. I've bolted it into my code and installed it on my iPhone for the first time and it just sits there at "AngryAnt Behave version 1.3 loading ..." but never returns.

    Am I missing something? Do I need to copy a DLL anywhere? Is there a specific iOS version I must use/target?

    [EDIT] Okay panic over, it seems the debug build of my Behave library was not liked by the phone but the release lib works just fine!

    Thanks
    Ian H
     
    Last edited: Sep 18, 2011
  46. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    177
    Hi All

    I think I maybe have a similar problem to feanix - I was hoping to control which subtree to select by using decorators, an idea sparked after seeing them used in what I thought was this way on aigamedev.com in that invaluable master class (thanks for that!).

    So for instance I placed a decorator above my idle sub-tree of CanIdle, so I can interrupt that subtree when the player can no longer idle - perhaps because the player has entered a trigger collider. But I'm not sure what to return from the CanIdle's Tick method? It is working, but my Idle subtree starts again after each action, where it's top-level node is a selector.

    I'm new to behaviour trees so I'm still getting to grips with them, so for instance my ShouldContinueFight decorator could be expanded on to include all the factors that would need to be satisfied to continue fighting (i.e. the entity's health and power, some heuristic as to whether they think they can win the fight etc...) and I am sure there are other areas I could expand on, but here is my top-level tree and the idle subtree:

    Top Level Tree


    Idle Tree


    Any help/suggestions much appreciated!

    I am thinking of refactoring the tree to remove the decorators if they won't work like that, so that my sequences begin with conditions enforced by an action which will return failed if the condition cannot be met - would that make sense in lieu of any tweaking to what I currently have?

    [EDIT] Okay, this was all doing very, very weird stuff, or rather almost nothing at all because.... when I made my new trees and moved them around I was still picking up the Frequency as before but I hadn't realised that the Frequency hadn't been set and was currently 0, remarkably nothing like a divide by zero in the output pane but my action was called once (to init) and then nothing else happened!

    A very painful mistake to make, but a Frequency default of 1 might be handy or maybe make 0 an invalid frequency for design time entry? I sure won't be making the same mistake again however!

    Thanks
    Ian H
     
    Last edited: Sep 21, 2011
  47. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    177
    Hi All, Emil

    I'm getting a bit stumped with these BTs at present - what is the expected functionality of a decorator? I've been returning failure so that the decorator can interrupt the tree but I was expecting that it would return failure to the selector above it and the selector would try the next action, but it seems to fail all the way back to the root and keep trying the failing branch?

    Am I missing something?

    [Edit]

    I created a simple scene with a tree as follows:



    I've hooked up some switched in game so I can play with what the decorators return. If I look at the debugger I see that when Dec1 is returning Success, Action1 is executed. If I flip the switch so that Dec1 returns Failure, nothing gets executed - unless I am reading the debugger wrong? - and the debugger sits with Dec1 highlighted.

    As I say above, my expectation was that Dec1 returning false would interrupt Action1 and that the Selector would deal with the failure by continuing to the next branch, in this case Dec2, but Dec2, despite returning success does not seem to be being executed.

    Is this a bug or is it just that I'm not following how BTs are meant to work - I started using Decorators in this way after watching Emil's masterclass.

    [Edit2]
    So to me this feels broken - if I return Success from the Decorators but false from the actions then the actions just get executed one after another, which is pretty much as I'd expected :eek:/

    Starting to look like it's better to use an action as a condition before the real action, to check whether the tree needs interrupting. UNLESS of course I've got this all wrong... help! Of course it's Unite 11 week, so I guess I shall have to sit tight!

    Thanks
    Ian H
     
    Last edited: Sep 29, 2011
  48. scadieux

    scadieux

    Joined:
    Sep 14, 2011
    Posts:
    20
    I was wondering, is there any way to get the float or the string parameter w/out implementing the handlers functions?

    The reason why I don`t want to use handlers is because when I instantiate the tree I am automatically instantiating associated classes based on the BT node names.

    I am considering the use of dynamics methods http://msdn.microsoft.com/en-us/library/system.reflection.emit.dynamicmethod(v=VS.80).aspx but I was looking for a more elegant solution.

    [EDIT 1]
    Looking at classes structures it seems that my hope would be in using SetInit/TickForward
     
    Last edited: Oct 17, 2011
  49. scadieux

    scadieux

    Joined:
    Sep 14, 2011
    Posts:
    20
    @MooCow
    From what I read in the UI, the decorators will never return a failure.
    $Untitled.png

    So the selector will never change its execution branch. I did not tested this (I am building my very first BT), but from what I can read. Using sequences instead of selector would give you the expected result.
    $Utilted2.png

    Again I did not tested this yet, but from what I can read this wouldn't be a bug. Counterintuitive yes, but not a bug from the description. I guess Emil could could give us more details about this...

    [EDIT 1]
    Reading my post I realize that this is not equivalent, since in case of success the sequencer will execute next branch while we don't want the next branch to be executed. That would only work for failure cases.
     
    Last edited: Oct 16, 2011
  50. scadieux

    scadieux

    Joined:
    Sep 14, 2011
    Posts:
    20
    @MooCow
    I've figured out some work around. I'ts not a clean solution but it should give us the expected behaviour of decorators. Also this assume that all the decorator on the path to an action are ticked prior ticking that action (even when the action is running).

    What I am doing is whenever I call tick on a decorator I store its result into some variable and return success regardless of that result to Behave.

    Then when another decorator is met, if the stored result is failure I won't tick physically that decorator and still return success.

    Finally, when an action is met, the stored result is reset to success and prior ticking that action if the previously stored result was failure, the action won't be ticked and failure will be returned instead. Otherwise, the action result will be returned.

    I didn't tested this in details yet, but I will post some feedback when I got something working.