summaryrefslogtreecommitdiff
path: root/scene/3d
diff options
context:
space:
mode:
authorMilan Davidovic <milan.davidovic@protonmail.com>2019-08-19 11:11:14 +0200
committerMilan Davidovic <milan.davidovic@protonmail.com>2019-08-26 15:10:15 +0200
commitc12ce2b4ddaf22cb06b69a8d0c685b54485e8c05 (patch)
treeee5707f9cbff729cda848b78b23747272ff316e1 /scene/3d
parentcce148b0242836b5c32a7fa6c39013a2fc1c9eff (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/3d')
-rw-r--r--scene/3d/physics_body.cpp12
-rw-r--r--scene/3d/physics_body.h1
2 files changed, 13 insertions, 0 deletions
diff --git a/scene/3d/physics_body.cpp b/scene/3d/physics_body.cpp
index 19d5f1dd3c..3c52547bf1 100644
--- a/scene/3d/physics_body.cpp
+++ b/scene/3d/physics_body.cpp
@@ -1387,6 +1387,18 @@ Ref<KinematicCollision> KinematicBody::_get_slide_collision(int p_bounce) {
return slide_colliders[p_bounce];
}
+void KinematicBody::_notification(int p_what) {
+ if (p_what == NOTIFICATION_ENTER_TREE) {
+ // Reset move_and_slide() data.
+ on_floor = false;
+ on_floor_body = RID();
+ on_ceiling = false;
+ on_wall = false;
+ colliders.clear();
+ floor_velocity = Vector3();
+ }
+}
+
void KinematicBody::_bind_methods() {
ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec", "infinite_inertia", "exclude_raycast_shapes", "test_only"), &KinematicBody::_move, DEFVAL(true), DEFVAL(true), DEFVAL(false));
diff --git a/scene/3d/physics_body.h b/scene/3d/physics_body.h
index aa6030d44e..7db7da6086 100644
--- a/scene/3d/physics_body.h
+++ b/scene/3d/physics_body.h
@@ -314,6 +314,7 @@ private:
Ref<KinematicCollision> _get_slide_collision(int p_bounce);
protected:
+ void _notification(int p_what);
static void _bind_methods();
public: