Search Unity

Active Logic - Behaviour Trees & Game Logic for C# Coders

Discussion in 'Assets and Asset Store' started by eelstork, Sep 16, 2019.

  1. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Active Logic is a behavior tree (BT) library written specifically for C# programmers.


    This is a tightly integrated, well optimized BT solution scaling from game logic (controllers, locomotion, input management) to game AI (smart NPCs).

    AL is designed to support your everyday workflow with a set of clean, easy to use constructs fitting right into a game programmer's toolkit.

    Easy to learn

    If you are familiar with update loops and C# programming, AL is perhaps the one BT library that is easiest to learn. Want a BT sequence? Here is how AL does it:

    Code (CSharp):
    1. status Attack(){
    2.     // BT sequences implemented via logical AND '&&'
    3.     // Reads "Equip weapon, reach foe and strike"
    4.     return EquipWeapon() && Reach(foe) && Strike(foe);
    5. }
    Too simple? There is a secret sauce: AL is the first library leveraging operator overloading to implement BT's main features.
    The above example works like a co-routine, except with a nice, readable syntax, and (as we shall see!) more power. Let's draft a 'Soldier' AI:

    Code (CSharp):
    1. public class Soldier : UTask  // extends MonoBehavior, drop in the inspector.
    2. {
    3.  
    4.     // Expression bodied notation is well supported!
    5.     // Using ||, a selector prioritizes tasks. Reads "Attack, or defend, or retreat"
    6.     status Step() => Attack() || Defend() || Retreat();
    7.  
    8.     // No messing with blackboards, just use variables
    9.     status Attack() => EquipWeapon() && Reach(foe) && Strike(foe);
    10.  
    11.     // Inline delay, cool-downs and timers
    12.     status Strike() => Play(slash) && After(0.75f)?[ Sound(clash) && target.Damage(power) ]
    13.  
    14.     status EquipWeapon() => ...
    15.  
    16.     // ...
    17.  
    18. }
    The power of BT lies in letting you express your design intuitively, but giving just the right amount of flexibility in how your code executes: a selector prioritizes tasks, whereas a sequence puts them in play order. In this case, if the soldier cannot equip a weapon they will switch to defense automatically. The status returned by a task (done, fail, cont) drives integration with sequences and selectors (aka "composites").

    Quick Start Guide

    Comprehensive

    Active Logic offers much more than a couple of handy overloads (this you get free, MIT licensed, here).
    • Stateless, ordered and mutable composites for sequential and parallel execution - Wow. Fortunately the library is well documented.
    • More operators - simple BT decorators implemented using unary operators. Want to invert the status of a task?
      !task
      .
    • MonoBehaviour integration. Tasks and Steppers (update loops) drop into your inspector.
    • 8 decorators of awesome (delay, cooldown, interval, in-out, latch, once, timeout, and re-initializer, and counting).
    • Low overhead logging - If you like annotating your code with debug information, Active Logic does it nicely; well optimized so there is no need to delete traces before shipping.
    • Sample game - Demonstrates most APIs offered by the library. Available in two flavors, for beginners and experienced C# programmers.
    • Visual logging, history browsing and soft breakpoints (see video, below)
    Powerful

    AL is optimized for best performance:
    • Core library is only marginally slower than equivalent code using if-elses.
    • Where other libraries require one tree instance per agent, ours are encoded in program memory. Stateful constructs (such as timers) generate minimal overheads. 0 GC possible (depending on selected features).
    • Our debugging/logging APIs are optimized to ensure traces need not be removed from production builds.
    Good to know:
    • Requires Unity 2018.3 or later (C#7.2 and up); runs on 2017 with the incremental compiler but not officially supported.
    • Offers, but does not require its own update loops. Can integrate with an existing visual solution (for implementing base tasks) and probably ECS (no singleton/central core, mostly thread safe).
    Hoping you will enjoy this library as much as I do! Feel free to post your questions here or drop a message on our Discord
     
    Last edited: Oct 13, 2020
    TeagansDad likes this.
  2. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    As promised, a video showing visual debugging in Active Logic

     
  3. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Active Logic 1.2 is now available; and since this library is now a year old, I am also happy to offer the library half price ($12.49), for a limited time. New features and fixes:
    • Fixed flakiness that would sometimes cause logs to incorrectly display.
    • Small improvements to help with running play mode tests
    • A utility for developers who prefer "Play(anim)" vs Mecanim's FSM oriented approach (beta)
    • A new feature (reset-on-resume) further simplifies using stateful decorators (such as timers). For short, stateful things need a reset from time to time. Reset-on-resume takes care of that for you.
    • A new API ("Push") for overriding a behavior tree with another; this can be used for optimization, or in certain situations where you want an agent to "focus" on a narrow range of activities.
    Sample usage for the Push API:

    Code (CSharp):
    1. if(overrideCondition){
    2.    Func<status> task = ...
    3.    Push(task);
    4. }
    (No 'Pop' function since the overriding tree will automatically de-stack when task is complete).
     
    Last edited: Oct 13, 2020
  4. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    In a recent Gamasutra featured post I discuss our focus on BT and make the case for stateless control as a good default. Also explaining key features of the Active Logic library.
     
  5. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Active Logic 1.3 will be available soon, with the following improvements:
    • Drive decorators (Tie/While) for running a task while another is also in the running state (such as with synchronizing media, or burning fuel while moving).
    • Reset blocks (complements the Reset-on-Resume feature).
    • New ordered composite syntax (fixes potential conflicts with Reset-on-Resume; older syntax will remain available until the first 2021 release).
    • Wait(seconds) task (easier than the After(seconds)?[ exp ] decorator, which remains available)
    • Less conversions and increased type safety with certainties.
    This release consolidates AL as an easy to use, professional library written specifically for coded BTs. No new features until 2021 as I am working on new demos and profiling new features to ensure best performance.

    Throughout 2021, AL will continue to improve:
    • XGOAP integration
    • Utility nodes
    • Dedicated features to help with play mode testing
     
  6. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    • Active Logic 1.3 is now available on the Asset Store.
    • Active-LT is a free version of AL, now in preview on Github. Active-LT features the Unity integration, so you can get started in no time.
      Stateless control is powerful. However, if Active-LT is not enough for you, a seamless upgrade path to Active Logic remains available.
      A Unity Asset Store version will be submitted soon :)
     
    Last edited: Nov 20, 2020
  7. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Active-LT has now been submitted to the Asset Store. The free version includes a new tutorial/demo which you can already view here.

    The free version is fully functional. Stateless BTs are productive, true to form (classic BTs are stateless), and easy to use.

    Much work has gone into visual/history tracing features, and this is also included.

    I will update when the asset is live, and the full featured version is also getting a minor release, perhaps today or tomorrow.
     
  8. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Good news!
    • Active-LT is now available (free) and there is also a new tutorial/demo to get you started.
    • Active Logic 1.3.1 released; mainly for improved test coverage (100%) and validating against Unity 2020 and 2019 LTS.
    Also, here is a 4 minutes read (via Hackernoon) thinking event frameworks vs behavior trees.
     
  9. slimshader

    slimshader

    Joined:
    Jun 11, 2013
    Posts:
    187
    Hey, finally found some time to experimet with AL and getting lots of errors when starting the demo scene in 2020.3:

    Code (csharp):
    1.  
    2. InvalidOperationException: Clear log data first
    3. Active.Core.Details.StatusRef.SetLogData (Active.Core.AbstractDecorator dec, System.Object target, System.String reason) (at Assets/ActiveLogic/Src/Core/Details/Stateful/StatusRef.cs:42)
    4.  
     
  10. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    @slimshader sorry for the slow reply here!
    I'm seeing the same in 2020.3, will have a closer look asap.
    On the positive side: demo still works, and not seeing issues in production (as we use this daily at work!)
     
  11. SpindizzyGames

    SpindizzyGames

    Joined:
    Jun 29, 2017
    Posts:
    108
    Interesting solution. Three questions:

    1) What are its weaknesses compared to other solutions, if any? I've read its strengths.
    2) Why isn't it more popular, having been out awhile?
    3) Have these stated features been completed ?

    • XGOAP integration
    • Utility nodes
    • Dedicated features to help with play mode testing
     
  12. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Some answers.

    1) I think it's pointless to expose this solution to people who need visual scripting. Initially I *assumed* that existing visual solutions filled the slot (likely not the case since most visual solutions apparently do not support stateless BTs) therefore I wasn't planning a visual solution.

    2) Lack of marketing? I haven't had time to evangelize much as I joined a busy game team and just used the library. Many game logic programmers are not even aware of BT's benefits.
    I read well meaning articles dismissing BTs as not useful to programmers. Problem made worse by 2 BT paradigms co-existing, and people confusing them.
    Programmers get XGOAP because they understand planning as something that needs a separate library. They underestimate BT's benefits because they mostly see technical designers using BT to visual spaghetti their way out of AI.

    3) XGOAP, Utility, no. If somebody brought me an actual use case, would make it easier. That said integrating path-finders is very similar to integrating GOAP, and isn't hard.

    A coming update will demonstrate a sensible Mecanim (and by extension FSM) integration. Here there is a strong case because Mecanim is popular, and it's hard to get around it even if doing away with it altogether should be the better way.

    AL has a big update coming; I'll need to focus on this before worrying about GOAP and Utility nodes.
     
    slimshader likes this.