Store ObjectID instead of pointer for KinematicCollision owner

This commit is contained in:
Haoyu Qiu 2024-04-14 23:45:22 +08:00 committed by Relintai
parent 9f75f7dfc0
commit b67115fd35
4 changed files with 10 additions and 33 deletions

View File

@ -1002,7 +1002,7 @@ Ref<KinematicCollision2D> KinematicBody2D::_move(const Vector2 &p_motion, bool p
// Create a new instance when the cached reference is invalid or still in use in script. // 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) { if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) {
motion_cache.instance(); motion_cache.instance();
motion_cache->owner = this; motion_cache->owner_id = get_instance_id();
} }
motion_cache->collision = col; motion_cache->collision = col;
@ -1368,7 +1368,7 @@ Ref<KinematicCollision2D> KinematicBody2D::_get_slide_collision(int p_bounce) {
// Create a new instance when the cached reference is invalid or still in use in script. // 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) { 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].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]; slide_colliders.write[p_bounce]->collision = colliders[p_bounce];
@ -1492,17 +1492,6 @@ KinematicBody2D::KinematicBody2D() :
on_wall = false; on_wall = false;
sync_to_physics = 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 { Object *KinematicCollision2D::get_local_shape() const {
PhysicsBody2D *owner = Object::cast_to<PhysicsBody2D>(ObjectDB::get_instance(owner_id));
if (!owner) { if (!owner) {
return nullptr; return nullptr;
} }
@ -1600,5 +1590,5 @@ KinematicCollision2D::KinematicCollision2D() {
collision.collider = 0; collision.collider = 0;
collision.collider_shape = 0; collision.collider_shape = 0;
collision.local_shape = 0; collision.local_shape = 0;
owner = nullptr; owner_id = 0;
} }

View File

@ -348,7 +348,6 @@ public:
bool is_sync_to_physics_enabled() const; bool is_sync_to_physics_enabled() const;
KinematicBody2D(); KinematicBody2D();
~KinematicBody2D();
}; };
VARIANT_ENUM_CAST(KinematicBody2D::MovingPlatformApplyVelocityOnLeave); VARIANT_ENUM_CAST(KinematicBody2D::MovingPlatformApplyVelocityOnLeave);
@ -356,7 +355,7 @@ VARIANT_ENUM_CAST(KinematicBody2D::MovingPlatformApplyVelocityOnLeave);
class KinematicCollision2D : public Reference { class KinematicCollision2D : public Reference {
GDCLASS(KinematicCollision2D, Reference); GDCLASS(KinematicCollision2D, Reference);
KinematicBody2D *owner; ObjectID owner_id;
friend class KinematicBody2D; friend class KinematicBody2D;
KinematicBody2D::Collision collision; KinematicBody2D::Collision collision;

View File

@ -974,7 +974,7 @@ Ref<KinematicCollision> 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. // 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) { if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) {
motion_cache.instance(); motion_cache.instance();
motion_cache->owner = this; motion_cache->owner_id = get_instance_id();
} }
motion_cache->collision = col; motion_cache->collision = col;
@ -1371,7 +1371,7 @@ Ref<KinematicCollision> KinematicBody::_get_slide_collision(int p_bounce) {
// Create a new instance when the cached reference is invalid or still in use in script. // 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) { 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].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]; slide_colliders.write[p_bounce]->collision = colliders[p_bounce];
@ -1510,18 +1510,6 @@ KinematicBody::KinematicBody() :
set_safe_margin(0.001); 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 { 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 { Object *KinematicCollision::get_local_shape() const {
PhysicsBody *owner = Object::cast_to<PhysicsBody>(ObjectDB::get_instance(owner_id));
if (!owner) { if (!owner) {
return nullptr; return nullptr;
} }
@ -1618,7 +1607,7 @@ KinematicCollision::KinematicCollision() {
collision.collider = 0; collision.collider = 0;
collision.collider_shape = 0; collision.collider_shape = 0;
collision.local_shape = 0; collision.local_shape = 0;
owner = nullptr; owner_id = 0;
} }
KinematicCollision::~KinematicCollision() { KinematicCollision::~KinematicCollision() {

View File

@ -351,7 +351,6 @@ public:
bool is_sync_to_physics_enabled() const; bool is_sync_to_physics_enabled() const;
KinematicBody(); KinematicBody();
~KinematicBody();
}; };
VARIANT_ENUM_CAST(KinematicBody::MovingPlatformApplyVelocityOnLeave); VARIANT_ENUM_CAST(KinematicBody::MovingPlatformApplyVelocityOnLeave);
@ -359,7 +358,7 @@ VARIANT_ENUM_CAST(KinematicBody::MovingPlatformApplyVelocityOnLeave);
class KinematicCollision : public Reference { class KinematicCollision : public Reference {
GDCLASS(KinematicCollision, Reference); GDCLASS(KinematicCollision, Reference);
KinematicBody *owner; ObjectID owner_id;
friend class KinematicBody; friend class KinematicBody;
KinematicBody::Collision collision; KinematicBody::Collision collision;