godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.87k stars 21.14k forks source link

VisibleOnScreenEnabler causes error when used with NavigationAgent Avoidance #83069

Open peter-rocchio opened 1 year ago

peter-rocchio commented 1 year ago

Godot version

v4.1.2.stable.official [399c9dc39]

System information

Windows 11, Vulkan

Issue description

When a VisibleOnScreenEnabler2D is paired with a CharacterBody2D using avoidance on a NavigationAgent2D the velocity_computed signal generates and error if attempting to set the velocity and call move_and_slide.

The error is:

E 0:00:00:0481   agent.gd:5 @ _on_navigation_agent_2d_velocity_computed(): Condition "!body->get_space()" is true. Returning: false
  <C++ Source>   servers/physics_2d/godot_physics_server_2d.cpp:997 @ body_test_motion()
  <Stack Trace>  agent.gd:5 @ _on_navigation_agent_2d_velocity_computed()

In the attached minimal reproduction project this error occurs only once, however in a different project this error happens every frame if more than 1 of the same agent is in the scene.

Steps to reproduce

  1. Create a CharacterBody2D
  2. Add CollisionShape2D, VisibleOnScreenEnabler2D, and NavigationAgent2D nodes to the CharacterBody2D
  3. Connect the velocity_computed signal of the NavigationAgent2D node to the CharacterBody2D
  4. In the signal callback function add:
    velocity = safe_velocity
    move_and_slide()
  5. Add the CharacterBody2D to a Node2D scene
  6. Run the scene
  7. Observe the error

Minimal reproduction project

NavAgentSafeVelocity.zip

smix8 commented 1 year ago

This error is unrelated to navigation.

So what you can do in your own scripts is to check the process mode / physics space first to guard against the error.

What imo physics can do long-term is to work with a real enable/disable toggle or pause modes. Right now it goes the lazy way of removing RIDs or disabling entire server processing when paused which causes all kinds of issues. Navigation had many of the same issues (e.g. agents were removed from maps instead of just paused) before the enabled modes were added.

peter-rocchio commented 1 year ago

This error is unrelated to navigation.

  • The VisibleOnScreenEnabler2D disables the node's processing mode.
  • A physics node with disabled processing mode gets its physics space removed.
  • move_and_slide() will trigger an error with no physics space.

So what you can do in your own scripts is to check the process mode / physics space first to guard against the error.

What imo physics can do long-term is to work with a real enable/disable toggle or pause modes. Right now it goes the lazy way of removing RIDs or disabling entire server processing when paused which causes all kinds of issues. Navigation had many of the same issues (e.g. agents were removed from maps instead of just paused) before the enabled modes were added.

Thank you for clarifying, and thank you for the navigation system. It's been a pleasure to use and I look forward to all the new features on the way.