From b67115fd35bc5e36785c02216964ce9c2d738b21 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Sun, 14 Apr 2024 23:45:22 +0800 Subject: [PATCH] Store ObjectID instead of pointer for KinematicCollision owner --- scene/2d/physics_body_2d.cpp | 18 ++++-------------- scene/2d/physics_body_2d.h | 3 +-- scene/3d/physics_body.cpp | 19 ++++--------------- scene/3d/physics_body.h | 3 +-- 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index 6aed8225e..87ae0916d 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -1002,7 +1002,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; @@ -1368,7 +1368,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]; @@ -1492,17 +1492,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; - } - } -} //////////////////////// @@ -1525,6 +1514,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; } @@ -1600,5 +1590,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 dbf308d97..86014b1bd 100644 --- a/scene/2d/physics_body_2d.h +++ b/scene/2d/physics_body_2d.h @@ -348,7 +348,6 @@ public: bool is_sync_to_physics_enabled() const; KinematicBody2D(); - ~KinematicBody2D(); }; VARIANT_ENUM_CAST(KinematicBody2D::MovingPlatformApplyVelocityOnLeave); @@ -356,7 +355,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 ee44ee5a0..19fdca93c 100644 --- a/scene/3d/physics_body.cpp +++ b/scene/3d/physics_body.cpp @@ -974,7 +974,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; @@ -1371,7 +1371,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]; @@ -1510,18 +1510,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 { @@ -1543,6 +1531,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; } @@ -1618,7 +1607,7 @@ KinematicCollision::KinematicCollision() { collision.collider = 0; collision.collider_shape = 0; collision.local_shape = 0; - owner = nullptr; + owner_id = 0; } KinematicCollision::~KinematicCollision() { diff --git a/scene/3d/physics_body.h b/scene/3d/physics_body.h index 30008f538..ca1a01893 100644 --- a/scene/3d/physics_body.h +++ b/scene/3d/physics_body.h @@ -351,7 +351,6 @@ public: bool is_sync_to_physics_enabled() const; KinematicBody(); - ~KinematicBody(); }; VARIANT_ENUM_CAST(KinematicBody::MovingPlatformApplyVelocityOnLeave); @@ -359,7 +358,7 @@ VARIANT_ENUM_CAST(KinematicBody::MovingPlatformApplyVelocityOnLeave); class KinematicCollision : public Reference { GDCLASS(KinematicCollision, Reference); - KinematicBody *owner; + ObjectID owner_id; friend class KinematicBody; KinematicBody::Collision collision;