Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Don't override KinematicCollision reference when still in use in script #52955

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions scene/2d/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,8 @@ Ref<KinematicCollision2D> KinematicBody2D::_move(const Vector2 &p_motion, bool p
Collision col;

if (move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_test_only)) {
if (motion_cache.is_null()) {
// 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;
}
Expand Down Expand Up @@ -1320,7 +1321,8 @@ Ref<KinematicCollision2D> KinematicBody2D::_get_slide_collision(int p_bounce) {
slide_colliders.resize(p_bounce + 1);
}

if (slide_colliders[p_bounce].is_null()) {
// 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;
}
Expand Down
6 changes: 4 additions & 2 deletions scene/3d/physics_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,8 @@ void RigidBody::_reload_physics_characteristics() {
Ref<KinematicCollision> KinematicBody::_move(const Vector3 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes, bool p_test_only) {
Collision col;
if (move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_test_only)) {
if (motion_cache.is_null()) {
// 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;
}
Expand Down Expand Up @@ -1330,7 +1331,8 @@ Ref<KinematicCollision> KinematicBody::_get_slide_collision(int p_bounce) {
slide_colliders.resize(p_bounce + 1);
}

if (slide_colliders[p_bounce].is_null()) {
// 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;
}
Expand Down