Merge pull request #90669 from timothyqiu/owner-id-3.x

[3.x] Store `ObjectID` instead of pointer for KinematicCollision owner
This commit is contained in:
lawnjelly 2024-04-16 13:49:58 +01:00 committed by GitHub
commit db3fe5e5c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 10 additions and 33 deletions

View file

@ -998,7 +998,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.
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<KinematicCollision2D> 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<PhysicsBody2D>(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;
}

View file

@ -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;

View file

@ -972,7 +972,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.
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<KinematicCollision> 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<PhysicsBody>(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;
}
///////////////////////////////////////

View file

@ -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;