Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JeremyVansnick opened this issue Apr 11, 2024 · 6 comments · Fixed by #103
Closed

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

JeremyVansnick opened this issue Apr 11, 2024 · 6 comments · Fixed by #103
Labels
enhancement New feature or request released

Comments

@JeremyVansnick
Copy link

JeremyVansnick commented Apr 11, 2024

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 ashblue added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels Apr 11, 2024
@ashblue
Copy link
Owner

ashblue commented Apr 13, 2024

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
Copy link

@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 👍

@ashblue
Copy link
Owner

ashblue commented Nov 9, 2024

@allcontributors add @JeremyVansnick for code

Copy link
Contributor

@ashblue

I've put up a pull request to add @JeremyVansnick! 🎉

@ashblue
Copy link
Owner

ashblue commented Nov 9, 2024

@JeremyVansnick I implemented the pause functionality. Works amazingly well!!! There was was performance bug I found double polling the fader but I fixed it. Will go out here shortly in the next release.

@ashblue ashblue closed this as completed in b031653 Nov 9, 2024
ashblue pushed a commit that referenced this issue Nov 9, 2024
# [2.3.0](v2.2.3...v2.3.0) (2024-11-09)

### Bug Fixes

* **assetpath.cs:** retarget PATH_PROJECT ([45f5620](45f5620)), closes [#79](#79)
* **builds:** node version now pulls from .nvmrc ([8564210](8564210))
* **conditions:** generic condition had exit and init reversed ([c9ac8cc](c9ac8cc))
* **dark mode:** boxes keep proper color in dark mode for the visualizer ([b3dd08f](b3dd08f)), closes [#48](#48)
* **visualizer:** vertically connected nodes now connect properly ([321e47e](321e47e)), closes [#29](#29)

### Features

* **visualizer:** pausing the game now keeps active node highlighting ([b031653](b031653)), closes [#94](#94)
@ashblue
Copy link
Owner

ashblue commented Nov 9, 2024

🎉 This issue has been resolved in version 2.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants