Treesharp C# 行为树库 - 从哪里开始?

发布于 2024-10-03 17:51:12 字数 324 浏览 0 评论 0原文

最近,我一直在尝试使用apoc 发布的treesharp 库来实现一个强大的行为树。我一直在书中讨论迭代器和接口,但我仍然不知道如何测试,更不用说使用这个库了。接口如何相互连接以及如何实际执行测试/构建树让我感到困惑。

通常在这种情况下,我会寻找代码示例并从其他人的工作中获得启发,但是,对于这个库,似乎没有任何示例代码。

谁能帮我弄清楚如何开始使用这个库构建行为树?如果这个问题非常幼稚(我认为可能是),我很抱歉,但是接口中的枚举器和渐进式接口现在对我来说非常难以理解。

Recently I have been attempting to implement a robust behavior tree using the treesharp library posted by apoc. I have been going over iterators and interfaces in my books, but I still can't even figure out how to test let alone use this library. How the interfaces connect with eachother and how to actually perform a test/build a tree with them is confusing the heck out of me.

Usually in this situation, I would look for code examples and derive enlightenment from looking at other people's work, however, for this library, there does not seem to be any example code.

Could anyone help me figure out how I could start to build a behavior tree using this library? I am sorry if the question is very noobish (and I think it may be) but Enumerators and progressive interfaces within interfaces are extremely difficult for me to understand right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-10-10 17:51:12

我是 TreeSharp 的作者,如果你们有任何问题,请随时给我发一封电子邮件(它包含在标题中的每个源文件中)。

您首先需要了解行为树的概念(选择器、序列、装饰器、操作等之间的差异)。我还提供了一些“虚荣”复合材料,使事情变得稍微容易一些(例如等待)。

基于构造函数的 API 允许您完全通过构造函数来定义树(使用在运行时评估以提供决策等的委托)

不幸的是,我从未抽出时间来实现“TreeExecutor”类,该类处理从类似于 Tick() 方法。最简单的方法(在本例中使用 PrioritySelector,但您可以使用任何组合)如下;

    static void Start()
    {
        // Start MUST be called before you can tick the tree.
        Logic.Start(null);
        // do work to spool up a thread, or whatever to call Tick();
    }

    private static void Tick()
    {
        try
        {             
            Logic.Tick(null);
            // If the last status wasn't running, stop the tree, and restart it.
            if (Logic.LastStatus != RunStatus.Running)
            {
                Logic.Stop(null);
                Logic.Start(null);
            }
        }
        catch (Exception e)
        {
            // Restart on any exception.
            Logging.WriteException(e);
            Logic.Stop(null);
            Logic.Start(null);
            throw;
        }
    }

不幸的是,给出其用法的“示例”实际上取决于您使用它的目的。 (由于它如此通用,因此很难给出对任何给定项目都有意义的示例。我一直在使用它,从事物到人工智能逻辑,到工作流程,再到调度流程)

一个小例子可能会有所帮助;

    static Composite CreateFireMissile()
    {
        return new PrioritySelector(
            new Decorator(ret => CurrentShip.CurrentTarget != null,
                new Action(ret => CurrentShip.CurrentTarget.FireMissile())),

            new Decorator(ret => CurrentShip.CurrentTarget == null,
                new Decorator(ret => CurrentShip.NearbyHostiles.Count > 0,
                    new Sequence(
                        new Action(ret => CurrentShip.SetTarget(CurrentShip.NearbyHostiles[0])),
                        new Action(ret => CurrentShip.RotateTo(CurrentShip.CurrentTarget.Location))
                        )
                    )
                )       
        );
    }

同样,这实际上取决于您的要求。该库将允许您对任何复合材料进行子类化,以便更轻松地重用复合材料。 (例如,您可以创建一个 SetTargetAndRotate 操作,从而消除序列中的两个操作)

同样,如果你们有疑问,请随时提出。

I'm the author of TreeSharp, if you guys have any questions, feel free to shoot me an email (its contained in every source file in the header).

You'll first need to understand the concepts of behavior trees (the differences between selectors, sequences, decorators, actions, and the like). I also provide a few "vanity" composites to make things slightly easier (such as Wait).

The constructor-based API lets you define trees entirely via ctors (with the use of delegates which are evaluated at runtime to provide decisions, etc)

Unfortunately I never got around to implementing the "TreeExecutor" class, which handles executing an arbitrary behavior branch from something like a Tick() method. The easiest way (using a PrioritySelector in this example, but you can use any composite) is as follows;

    static void Start()
    {
        // Start MUST be called before you can tick the tree.
        Logic.Start(null);
        // do work to spool up a thread, or whatever to call Tick();
    }

    private static void Tick()
    {
        try
        {             
            Logic.Tick(null);
            // If the last status wasn't running, stop the tree, and restart it.
            if (Logic.LastStatus != RunStatus.Running)
            {
                Logic.Stop(null);
                Logic.Start(null);
            }
        }
        catch (Exception e)
        {
            // Restart on any exception.
            Logging.WriteException(e);
            Logic.Stop(null);
            Logic.Start(null);
            throw;
        }
    }

Unfortunately, giving "examples" of its usage really depends on what you're using it for. (Since its so generic, its difficult to give examples that will make sense for any given project. I've been using it from things to AI logic, to workflows, down to scheduling processes)

A small example which may help a bit;

    static Composite CreateFireMissile()
    {
        return new PrioritySelector(
            new Decorator(ret => CurrentShip.CurrentTarget != null,
                new Action(ret => CurrentShip.CurrentTarget.FireMissile())),

            new Decorator(ret => CurrentShip.CurrentTarget == null,
                new Decorator(ret => CurrentShip.NearbyHostiles.Count > 0,
                    new Sequence(
                        new Action(ret => CurrentShip.SetTarget(CurrentShip.NearbyHostiles[0])),
                        new Action(ret => CurrentShip.RotateTo(CurrentShip.CurrentTarget.Location))
                        )
                    )
                )       
        );
    }

Again, this really depends on your requirements. The library will let you subclass any composites for easier to re-use composites. (Eg; you can create a SetTargetAndRotate action, which eliminates the two actions within the Sequence)

Again, if you guys have questions, don't hesitate to ask.

ヤ经典坏疍 2024-10-10 17:51:12

杰森,
该库似乎实现了我在其他 BT 库示例中看到的所有想法。我正在考虑在我自己的一个项目中利用这样一个库,所以在我这样做之前,我只是根据简短的检查来猜测如何使用这个库。

尽管如此,我认为你应该为 Action 类的实例提供自己的回调函数,然后根据 BT 思想将各种操作拼凑成组组合(等到某件事为真后再执行,直到其中一个操作成功为止,执行所有这些行动除非失败,等等)。

华泰

Jason,
The library seems to implement all the ideas I've seen demonstrated in other examples of BT libraries. I'm looking into leveraging such a library for one of my own projects, so until I do, I'm only guessing on how to use this one based on a brief inspection.

Nevertheless, I think you would supply your own callback functions to instances of the Action class, and then piece together various actions into group combinations based on BT ideas (wait until something is true before acting, act until one of these actions is successful, perform all these actions unless one fails, etc).

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文