diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index 30d5706aea1..e38a93a4497 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -998,7 +998,7 @@ Ref KinematicBody2D::_move(const Vector2 &p_motion, bool p // Create a new instance when the cached reference is invalid or still in use in script. if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) { motion_cache.instance(); - motion_cache->owner = this; + motion_cache->owner_id = get_instance_id(); } motion_cache->collision = col; @@ -1364,7 +1364,7 @@ Ref KinematicBody2D::_get_slide_collision(int p_bounce) { // Create a new instance when the cached reference is invalid or still in use in script. if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->reference_get_count() > 1) { slide_colliders.write[p_bounce].instance(); - slide_colliders.write[p_bounce]->owner = this; + slide_colliders.write[p_bounce]->owner_id = get_instance_id(); } slide_colliders.write[p_bounce]->collision = colliders[p_bounce]; @@ -1488,17 +1488,6 @@ KinematicBody2D::KinematicBody2D() : on_wall = false; sync_to_physics = false; } -KinematicBody2D::~KinematicBody2D() { - if (motion_cache.is_valid()) { - motion_cache->owner = nullptr; - } - - for (int i = 0; i < slide_colliders.size(); i++) { - if (slide_colliders[i].is_valid()) { - slide_colliders.write[i]->owner = nullptr; - } - } -} //////////////////////// @@ -1521,6 +1510,7 @@ real_t KinematicCollision2D::get_angle(const Vector2 &p_up_direction) const { } Object *KinematicCollision2D::get_local_shape() const { + PhysicsBody2D *owner = Object::cast_to(ObjectDB::get_instance(owner_id)); if (!owner) { return nullptr; } @@ -1596,5 +1586,5 @@ KinematicCollision2D::KinematicCollision2D() { collision.collider = 0; collision.collider_shape = 0; collision.local_shape = 0; - owner = nullptr; + owner_id = 0; } diff --git a/scene/2d/physics_body_2d.h b/scene/2d/physics_body_2d.h index 111db64b834..a7aee5f01c9 100644 --- a/scene/2d/physics_body_2d.h +++ b/scene/2d/physics_body_2d.h @@ -347,7 +347,6 @@ public: bool is_sync_to_physics_enabled() const; KinematicBody2D(); - ~KinematicBody2D(); }; VARIANT_ENUM_CAST(KinematicBody2D::MovingPlatformApplyVelocityOnLeave); @@ -355,7 +354,7 @@ VARIANT_ENUM_CAST(KinematicBody2D::MovingPlatformApplyVelocityOnLeave); class KinematicCollision2D : public Reference { GDCLASS(KinematicCollision2D, Reference); - KinematicBody2D *owner; + ObjectID owner_id; friend class KinematicBody2D; KinematicBody2D::Collision collision; diff --git a/scene/3d/physics_body.cpp b/scene/3d/physics_body.cpp index 237d6b4fe85..ed18e24b562 100644 --- a/scene/3d/physics_body.cpp +++ b/scene/3d/physics_body.cpp @@ -972,7 +972,7 @@ Ref KinematicBody::_move(const Vector3 &p_motion, bool p_inf // Create a new instance when the cached reference is invalid or still in use in script. if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) { motion_cache.instance(); - motion_cache->owner = this; + motion_cache->owner_id = get_instance_id(); } motion_cache->collision = col; @@ -1369,7 +1369,7 @@ Ref KinematicBody::_get_slide_collision(int p_bounce) { // Create a new instance when the cached reference is invalid or still in use in script. if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->reference_get_count() > 1) { slide_colliders.write[p_bounce].instance(); - slide_colliders.write[p_bounce]->owner = this; + slide_colliders.write[p_bounce]->owner_id = get_instance_id(); } slide_colliders.write[p_bounce]->collision = colliders[p_bounce]; @@ -1508,18 +1508,6 @@ KinematicBody::KinematicBody() : set_safe_margin(0.001); } -KinematicBody::~KinematicBody() { - if (motion_cache.is_valid()) { - motion_cache->owner = nullptr; - } - - for (int i = 0; i < slide_colliders.size(); i++) { - if (slide_colliders[i].is_valid()) { - slide_colliders.write[i]->owner = nullptr; - } - } -} - /////////////////////////////////////// Vector3 KinematicCollision::get_position() const { @@ -1541,6 +1529,7 @@ real_t KinematicCollision::get_angle(const Vector3 &p_up_direction) const { } Object *KinematicCollision::get_local_shape() const { + PhysicsBody *owner = Object::cast_to(ObjectDB::get_instance(owner_id)); if (!owner) { return nullptr; } @@ -1616,7 +1605,7 @@ KinematicCollision::KinematicCollision() { collision.collider = 0; collision.collider_shape = 0; collision.local_shape = 0; - owner = nullptr; + owner_id = 0; } /////////////////////////////////////// diff --git a/scene/3d/physics_body.h b/scene/3d/physics_body.h index 4f7a674d39e..9eda220998b 100644 --- a/scene/3d/physics_body.h +++ b/scene/3d/physics_body.h @@ -347,7 +347,6 @@ public: bool is_sync_to_physics_enabled() const; KinematicBody(); - ~KinematicBody(); }; VARIANT_ENUM_CAST(KinematicBody::MovingPlatformApplyVelocityOnLeave); @@ -355,7 +354,7 @@ VARIANT_ENUM_CAST(KinematicBody::MovingPlatformApplyVelocityOnLeave); class KinematicCollision : public Reference { GDCLASS(KinematicCollision, Reference); - KinematicBody *owner; + ObjectID owner_id; friend class KinematicBody; KinematicBody::Collision collision;