summaryrefslogtreecommitdiff
path: root/scene
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-09-23 10:26:08 +0200
committerGitHub <noreply@github.com>2021-09-23 10:26:08 +0200
commit2815594ba0cc86120f70ede121a0bf32ac3ee051 (patch)
tree9b2d6a186ba6e5c980a3e603bdce8d36b184b011 /scene
parenta4b80cdad95e251d3066a6798cffc64bbf45a0e2 (diff)
parent92699520536d78fc94d986fd4d4922fad11978cf (diff)
Merge pull request #52954 from nekomatata/fix-kinematic-collision-reference
Diffstat (limited to 'scene')
-rw-r--r--scene/2d/physics_body_2d.cpp6
-rw-r--r--scene/3d/physics_body_3d.cpp6
2 files changed, 8 insertions, 4 deletions
diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp
index 799ed47862..c3dc9ab92b 100644
--- a/scene/2d/physics_body_2d.cpp
+++ b/scene/2d/physics_body_2d.cpp
@@ -58,7 +58,8 @@ Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_t
PhysicsServer2D::MotionResult result;
if (move_and_collide(p_motion, result, p_margin, 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.instantiate();
motion_cache->owner = this;
}
@@ -1369,7 +1370,8 @@ Ref<KinematicCollision2D> CharacterBody2D::_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].instantiate();
slide_colliders.write[p_bounce]->owner = this;
}
diff --git a/scene/3d/physics_body_3d.cpp b/scene/3d/physics_body_3d.cpp
index 7bd0a89eb3..48da186860 100644
--- a/scene/3d/physics_body_3d.cpp
+++ b/scene/3d/physics_body_3d.cpp
@@ -98,7 +98,8 @@ void PhysicsBody3D::remove_collision_exception_with(Node *p_node) {
Ref<KinematicCollision3D> PhysicsBody3D::_move(const Vector3 &p_motion, bool p_test_only, real_t p_margin, int p_max_collisions) {
PhysicsServer3D::MotionResult result;
if (move_and_collide(p_motion, result, p_margin, p_test_only, p_max_collisions)) {
- 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.instantiate();
motion_cache->owner = this;
}
@@ -1590,7 +1591,8 @@ Ref<KinematicCollision3D> CharacterBody3D::_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].instantiate();
slide_colliders.write[p_bounce]->owner = this;
}