Code Monkey home page Code Monkey logo

behaviortreely's Introduction

behavortreely.py

A behavior tree implementation in Python. It runs behavior trees defined in JSON format and will trigger actions in your code.

Note: this is the first full working draft of behavortreely.py, the example, especially the JSON is very place holder. I intend to update this in the coming days and weeks to be better documented. Probably just cursed myself by by saying that. Ah well.

Overview

  1. Prepare your tree JSON.
  2. Instantiate a behavortreely BehaviorTree by loading your tree JSON.
  3. Register your action handlers, condition handlers, and selector handlers if needed.
  4. Run the tree: once, many times, ongoing, etc.
    • Run via start() activates the tree's ongoing tick
    • Or run via tick() run it once

Example

test_json = """
{
    "nodeName":"root of behavior XYZ",
    "goal":"to test treely",
    "description":"this is the root",
    "slug":"root",
    "type":"LimitTime",
    "limit":7,
    "weight":0,
    "time_started":null,
    "tries":null,
    "selectorHandler":null,
    "conditionHandler":null,
    "actionHandler":null,
    "children":[
        {
            "nodeName":"try to win",
            "slug":"try-your-best0",
            "description":"keep trying your best",
            "type":"Selector",
            "weight":0.5,
            "parameters":[],
            "children":[
                {
                    "nodeName":"try to win1",
                    "slug":"try-your-best1",
                    "description":"keep trying your best1",
                    "type":"Action",
                    "weight":0.6,
                    "parameters":[],
                    "children":[],
                    "successHandler":null,
                    "failHandler":null,
                    "selectorHandler":null,
                    "conditionHandler":null,
                    "actionHandler":"do a test action",
                    "parentNodeSlug":"root"

                },
                {
                    "nodeName":"try to win2",
                    "slug":"try-your-best2",
                    "description":"keep trying your best2",
                    "type":"Action",
                    "weight":0.5,
                    "parameters":[],
                    "children":[],
                    "successHandler":null,
                    "failHandler":null,
                    "selectorHandler":null,
                    "conditionHandler":null,
                    "actionHandler":"do a test action",
                    "parentNodeSlug":"root"

                },
                {
                    "nodeName":"try to win3",
                    "slug":"try-your-best3",
                    "description":"keep trying your best3",
                    "type":"Action",
                    "weight":0.9,
                    "parameters":[],
                    "children":[],
                    "successHandler":null,
                    "failHandler":null,
                    "selectorHandler":null,
                    "conditionHandler":null,
                    "actionHandler":"do a test action",
                    "parentNodeSlug":"root"

                }
            ],
            "successHandler":null,
            "failHandler":null,
            "selectorHandler":"do a test selector",
            "conditionHandler":null,
            "actionHandler":null,
            "parentNodeSlug":"root"

        }
    ]
}
    """

    def condition_test():
        print("running a condition")
        return True

    def action_test():
        print("running an action")
        return True

    def selector_test(choices, blackboard, tree):
        # could do a lot more than this to provide proper slug from choices
        return choices[0]

    print("starting as script...")

    # create the behavior tree instance
    bt = BehaviorTree(test_json)

    # register any needed actions, conditions, or selectors referred to in the JSON
    bt.actions["do a test action"] = action_test
    bt.conditions["do a test condition"] = condition_test
    bt.selectors["do a test selector"] = selector_test

    # use start() instead of tick() to keep the behavior tree running
    # bt.start()

    # use tick() to run the tree once
    bt.tick()

behaviortreely's People

Contributors

robstenzinger avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.