godot: Pausing causes various errors with `Area2D`s

Godot version

4.0.dev (a534346)

System information

Windows 10

Issue description

If a body is within an Area2D’s CollisionShape2D when the scene is paused and resumed two things happen: If the body is higher in hierarchy (behind the Area2D), all signals are disconnected from the Area2D.

https://user-images.githubusercontent.com/21325943/170354862-e9274c51-848d-47a2-9b4c-9cc1f4e47b36.mp4

If the body is lower in the hierarchy (in front of the Area2D), errors are written to the console.

https://user-images.githubusercontent.com/21325943/170354890-7fd076fc-691e-4fa2-a2ea-7f3badfa97f0.mp4

Steps to reproduce

  1. Create CharacterBody2D with collision.
  2. Create Area2D with collision.
  3. Connect BodyEntered signal for Area2D.
  4. Pause and Resume game while body is inside Area2D.

Minimal reproduction project

CollisionBugGD.zip (converted to GDScript project)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 13
  • Comments: 15 (10 by maintainers)

Most upvoted comments

1. Wrong behaviors caused by pause

It takes two to make a collision. If either is disabled, collision will no longer exist. It should be noted that unpair will cause the area to eventually be added to monitor_query_list, although it may have previously tried to remove from monitor_query_list.

https://github.com/godotengine/godot/blob/ba54c34551d1bda5139515d74e7d614ccbc43cea/servers/physics_2d/godot_area_2d.cpp#L66-L68

If it is disabled in the order of A-B (Area-Body), the area will eventually be added to the monitor_query_list; if it is disabled in the order of B-A (Body-Area), the area will not be added to the monitor_query_list in the end.

https://github.com/godotengine/godot/blob/ba54c34551d1bda5139515d74e7d614ccbc43cea/servers/physics_2d/godot_space_2d.cpp#L1126-L1130

That is, in the above code, monitor_query_list.first() is different in the two cases. Area2D::_body_inout() will be called when A-B, but will not be called when B-A.

2. Error message during resumed

ERROR: Condition "p_elem->_root" is true.
   at: add (./core/templates/self_list.h:46)

For the error message during resumed. The body is paired with the area in place and becomes active. This causes the same body to be added to the active_list twice.

https://github.com/godotengine/godot/blob/4df80b0e629e25653a706f6721c13a1f9d1da368/servers/physics_2d/godot_area_pair_2d.cpp#L99-L101 https://github.com/godotengine/godot/blob/4df80b0e629e25653a706f6721c13a1f9d1da368/servers/physics_2d/godot_body_2d.cpp#L410-L412

I spent a while looking into this one, posting my findings here cause I am not sure what to do to fix:

1. Discrepancy in behaviour depending on who is on top

Whoever is on top of the tree is the one “monitoring” for the other, then, when enabling/disabling the process mode code will follow different paths on _apply_disabled() and _apply_enabled() resulting in different behaviours.

2. Signal’s disconnection

There isn’t in fact a signal disconnection, but by the way the area2d function monitoring bodies coming in and out, when that is on top of the hierarchy (thus monitoring) and you set PROCESS_MODE_DISABLED it doesn’t get a callback saying the object is out. This makes it so this counter doesn’t get decremented, and when the object actually leaves the area this is never called.

3. Why is the callback not called when Area2D is the one monitoring but not when it’s CharacterBody2D

Back to 1., when _apply_disabled() goes the area route, this function is called, particularly it calls for get_space()->area_remove_from_monitor_query_list(&monitor_query_list); in essence removing itself from the monitor_query_list and making it so the callback set for the leaving of the object to not be called on call_queries().

4. Error log when CharacterBody2D is on top

I haven’t really looked much to it, but this function has some comments on the counter that feels weird to me. https://github.com/godotengine/godot/blob/master/scene/2d/physics_body_2d.cpp#L366


Possible fix

On theory this would handle the worst reported issue, making sure all callbacks are handled before set_space does it’s thing.

void GodotArea2D::set_space(GodotSpace2D *p_space) {
	call_queries();  // make sure to handle any callbacks before clearing the space
	if (get_space()) {
		if (monitor_query_list.in_list()) {
			get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
		}
		if (moved_list.in_list()) {
			get_space()->area_remove_from_moved_list(&moved_list);
		}
	}

	monitored_bodies.clear();
	monitored_areas.clear();

	_set_space(p_space);
}

I am not opening a PR because this looks completely wrong, imo the way the process enable/disable is currently done is quite error prone and invites these quirks for specific cases.

Hey, is there any workaround for this issue right now on 4.2 stable? Seems kinda of a big deal that the engine just breaks itself if you happen to pause while inside an Area2D collision shape.

Sorry if I seem a little salty. I really love Godot and I know its not anyone’s fault, but it’s just that I’ve been banging my head against a wall for an entire day trying to make this work, just to find out its a bug.

Tested the new PR. While it doesn’t fix the problem enough to close out this ticket, it’s still a big step forward. However, I am curious if it is intentional that pausing/unpausing a scene causes enter/exit signals to retrigger in Area*D’s. That is the current behavior, so unrelated to the referenced PR, but it’s definitely problematic. My wonder is if it’s more or less problematic if it does than if it doesn’t.

It looks like this system is supposed to implement reference counting for a scenario where multiple object are referencing the same body_map object, but the implementation of Area2D as it currently exists creates a body_map for each instance of Area2D.

I would need information from someone more knowledgeable about the physics engine as a whole, but it appears the only references to value.rc are within this specific function call.

This is also an issue in Area3D