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

Assets Active Logic - Behaviour Trees & Game Logic for Unity 2018/9 and C# 7

Discussion in 'Works In Progress - Archive' started by eelstork, Sep 4, 2019.

  1. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Hello everybody, and a warm greeting to dedicated C# programmers!

    Active Logic is a library for everyday case logic. It is born from wanting to have a lean and mean, yet comprehensive BT library that brings productive fun to game AI and game logic.

    This is not a visual/node based system, so who is this library for?
    • Heard of BT or Blueprints, but visual systems do not appeal.
    • Familiar with BT assets, but wish you could go back to writing more code.
    • Tried BT libraries but can't shake a feeling that the special sauce is missing.
    • Hardene logic programmer willing to try something new, on condition that the benefits become obvious in less than 1 hour of tinkering.
    Let's get to the meat. The highlight of this library is a tiny struct living on the stack, status; status is failing, succeeding or running. With status, you can write BT-styled sequences and selectors:

    status Step() => Attack() || Defend() || Retreat();

    Above is a simple selector aka fallback strategy; usually partaking an update loop. The crux is that status, unlike bool, understands time. Let's pick apart the above sample.

    - Assume Attack is failing. If so, Defend evaluates; then:
    - if Defend is running, return running.
    - Otherwise, evaluate Retreat and return its status.

    You can think of the running state as performing a function similar to the `yield` statement.

    Likewise, the && operator is for sequences:

    status Enter(Transform door) => Unlock(door) && Open(door) && StepForward();

    Here, each subtask must complete before the previous subtask can start. Similar to an action queue, but more resilient (because, if a previous step becomes undone, it will re-iterate).

    Concurrent task groups and other specialized constructs are also provided, along with advanced decorators:

    status Attack() => Cooldown(0.5f)?[ Strike(target) ];

    NOTE: There isn't a requirement here for using the => notation or restricting yourself to one liners. The library just lends itself to this coding style.

    Active Logic is more than syntactic sugar. With readability, you also get reduced memory overheads and good performance.

    Also:
    • Low overhead logging API.
    • Editor window to inspect the runtime behavior of your AIs.
    • Breakpoints, stepper.
    • History feature
    • Sample game demonstrating common APIs
    • Comprehensive, online documentation.
    Availability:
    • The Unity package is in review; ETA ~mid September.
    • A Github repo is available (GPL-Affero; Unity compatible but no editor windows / mono behaviors)
    Thanks for your interest! Please follow the thread for updates, and ask away.
     
  2. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,231
    Hey there, I swore this sounded familiar when I read it. I went back through some old ideas I had for systems and turns out I had an idea for something similar to this about a year ago but just never got around to actually doing it.

    My notes were "Type executions out using syntax, ex. for an NPC basic attack: "if {combat} attack {aggrotarget} with {ability} every {3000, ms }"
    Then another line could be "if {self} health < 30% cast {heal} on {self} every {5000, ms} => resume {last}"

    Not exactly the same, but probably better than what I would have came up with anyways, lol. Glad to see you actually did it as it felt like it had some potential. Hopefully people get some good use out of it!
     
    eelstork likes this.
  3. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    This asset is now released. I will make an announcement in the assets thread soon; summoning courage for an intro video : )

    @MostHated I think I understand what you are suggesting, which echoes a comment I had over on Github, about DSLs (domain specific languages).

    I had mediocre experiences with EDSLs - where the domain language is 'embedded' into the host language, and I think think NUnit is a (harmless) example...
    Assert.That( myString, Is.EqualTo("Hello") );
    ?

    Anyway. I designed a library in this style once (in Obj-C) and it was frankly horrible to debug, because the code was easy to read, but there was a huge amount of re-shuffling and re-arranging happening behind the scenes.

    If you ever go this route (and active logic doesn't help) I would encourage you to write a proper DSL instead (aka make your own language - time consuming, I know!)

    With Active Logic fortunately there is nothing like it happening. Although it does produce concise logic and look neat, it is also very type safe and debugger friendly (the bitter pill was designing the patterns supporting this coding style; but oh the joy of discovering how far we can go with C#7).
     
    MostHated likes this.
  4. pjccccc

    pjccccc

    Joined:
    Oct 7, 2015
    Posts:
    43
    Wow, this project reminds me of my old library (not published finally)
    https://github.com/pjc0247/FxUnity

    I also wanted to extend C# language itself with some operators.
     
    eelstork and MostHated like this.