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.
If the body is lower in the hierarchy (in front of the Area2D), errors are written to the console.
Steps to reproduce
- Create
CharacterBody2Dwith collision. - Create
Area2Dwith collision. - Connect
BodyEnteredsignal forArea2D. - 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)
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 frommonitor_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 themonitor_query_listin 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
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_listtwice.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_DISABLEDit 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
CharacterBody2DBack to 1., when
_apply_disabled()goes the area route, this function is called, particularly it calls forget_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 oncall_queries().4. Error log when
CharacterBody2Dis on topI 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_spacedoes it’s thing.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