ashblue / fluid-behavior-tree

Behavior trees for Unity3D projects. Written with a code driven approach on the builder pattern.
MIT License
959 stars 109 forks source link

BT window shows all tasks as inactive when EditorApplication.isPaused #94

Open JeremyVansnick opened 6 months ago

JeremyVansnick commented 6 months ago

When I pause the editor, the behavior tree shows all the nodes as inactive. This is unpractical because we want to ideally be able to pause and see exactly what our AI is doing at that particular frame. image

(I am using Unity 2022.3.22f1) I am surprised this issue hasn't been observed before, since when I look at the code it seems quite logical that there would be a bug there. I don't think it's Unity version related.

The state of activation of visual tasks is set to false inside of the OnGUI() method. This works fine so long as the game is running. But the moment, the game is paused, the OnGUI() method is still running, which means all nodes become inactive.

The solution is to update the state of nodes in a method that runs at the same rate as the Update() function, in this case the EditorApplication.onUpdate. We can't do it in OnGUI because OnGUI() runs many times per frame and is completely unrelated to when the EditorApplication.isPaused is set properly.

=> This snippet is to show the first thing I tried and it doesn't work. It's because EditorApplication.isPaused is not set to true before OnGUI has had the time to run several times already, so the nodes are set to false.

        public void Print ()
        {
            _printer.Print(_taskActive);

            if (!EditorApplication.isPaused)
                 _taskActive = false;

            foreach (var child in _children) {
                child.Print();
            }
        }

Anyway... here's the proper approach: We print things in OnGUI. But we update values in EditorApplication.onUpdate... And we don't update values when the game is paused.

In BehaviorTreeWindow.cs

        void OnEnable() {
            EditorApplication.update += OnEditorUpdate;
        }

        void OnDisable() {
            EditorApplication.update -= OnEditorUpdate;
        }

        private void OnEditorUpdate() {
            if (!EditorApplication.isPaused)
                _printer?.UpdateValues();
        }

In BehaviorTreePrinter.cs

        public void UpdateValues()
        {
            _root.UpdateValues();
        }

In VisualTask.cs

      public void Print ()
        {
            _printer.Print(_taskActive);

            // _taskActive = false; // <= comment this out

            foreach (var child in _children) {
                child.Print();
            }
        }

        public void UpdateValues()
        {
            _printer.UpdateValues(_taskActive);
            _taskActive = false;

            foreach (var child in _children) {
                child.UpdateValues();
            }
        }

In NodePrinterController.cs

      public void UpdateValues(bool _taskIsActive)
        {
            _faders.Update(_taskIsActive);
        }

Now our GUI logic is separated from our value updating logic, and the nodes are keeping their active state even when the game is paused! image

ashblue commented 6 months ago

Yeah this is a feature I've thought about adding for a while. Being able to see the graph highlights in the pause state would be super useful. Definitely was annoying for me when I was working on a game recently and I needed to pause.

Your solution looks pretty solid. Next time I'm working on Fluid BT I need to do a bundle of small patches and features (will include this and get you a credit). It might be a little bit of time before I get to this but it should happen eventually.

MarcosAlfonso commented 3 months ago

@JeremyVansnick Appreciate this contribution, just getting started with this package and ran into this issue pretty quickly while trying to learn things. Made you're proposed modifications and it's working great 👍