diff options
author | Milan Davidovic <milan.davidovic@protonmail.com> | 2019-08-19 11:11:14 +0200 |
---|---|---|
committer | Milan Davidovic <milan.davidovic@protonmail.com> | 2019-08-26 15:10:15 +0200 |
commit | c12ce2b4ddaf22cb06b69a8d0c685b54485e8c05 (patch) | |
tree | ee5707f9cbff729cda848b78b23747272ff316e1 /scene/2d | |
parent | cce148b0242836b5c32a7fa6c39013a2fc1c9eff (diff) |
Prevent move_and_slide() to generate an error.
When moving KinematicBody2D from one scene to another and not freeing
the old scene, the first call to move_and_slide() in the new scene will
generate an error because KinematicBody2D keeps internaly a
RID on_floor_body of a body resource in the old scene which no more has
a space assigned.
To fix this, on_floor_body is set to empty RID in response to
NOTIFICATION_ENTER_TREE notification of KinematicBody2D and
KinematicBody. Also all other data related to move_and_slide() is reset:
floor, ceiling, wall flags, colliders vector, floor_velocity.
This fixes #31416.
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/physics_body_2d.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index 49da709a47..ec4545c3a0 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -1446,6 +1446,14 @@ void KinematicBody2D::_direct_state_changed(Object *p_state) { void KinematicBody2D::_notification(int p_what) { if (p_what == NOTIFICATION_ENTER_TREE) { last_valid_transform = get_global_transform(); + + // Reset move_and_slide() data. + on_floor = false; + on_floor_body = RID(); + on_ceiling = false; + on_wall = false; + colliders.clear(); + floor_velocity = Vector2(); } if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) { |