From ea9dff87ae29ea34cff8d0452c2ea799c16b2d0c Mon Sep 17 00:00:00 2001 From: Guillaume Turri Date: Sun, 28 Jul 2024 10:24:09 +0200 Subject: [PATCH] Add error checks for bad configuration in `PathFollow2D/3D` `set_progress_ratio` When a PathFollow is badly configured it's possible to have code that call get_progress_ratio just after set_progress_ratio does not return the value just set, which may be confusing for developer (ie that myPathFollow2D.set_progress_ratio(0.5) myPathFollow2D.get_progress_ratio() does not return 0.5) This commit makes ensures the developer has useful error messages in such case, to make it easier to troubleshot it. --- doc/classes/PathFollow2D.xml | 1 + doc/classes/PathFollow3D.xml | 1 + scene/2d/path_2d.cpp | 7 ++++--- scene/3d/path_3d.cpp | 7 ++++--- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/doc/classes/PathFollow2D.xml b/doc/classes/PathFollow2D.xml index 61db34fb024..7444bfc23aa 100644 --- a/doc/classes/PathFollow2D.xml +++ b/doc/classes/PathFollow2D.xml @@ -26,6 +26,7 @@ The distance along the path as a number in the range 0.0 (for the first vertex) to 1.0 (for the last). This is just another way of expressing the progress within the path, as the offset supplied is multiplied internally by the path's length. + It can be set or get only if the [PathFollow2D] is the child of a [Path2D] which is part of the scene tree, and that this [Path2D] has a [Curve2D] with a non-zero length. Otherwise, trying to set this field will print an error, and getting this field will return [code]0.0[/code]. If [code]true[/code], this node rotates to follow the path, with the +X direction facing forward on the path. diff --git a/doc/classes/PathFollow3D.xml b/doc/classes/PathFollow3D.xml index b505c6ea8ba..bcd04d51ca2 100644 --- a/doc/classes/PathFollow3D.xml +++ b/doc/classes/PathFollow3D.xml @@ -36,6 +36,7 @@ The distance from the first vertex, considering 0.0 as the first vertex and 1.0 as the last. This is just another way of expressing the progress within the path, as the progress supplied is multiplied internally by the path's length. + It can be set or get only if the [PathFollow3D] is the child of a [Path3D] which is part of the scene tree, and that this [Path3D] has a [Curve3D] with a non-zero length. Otherwise, trying to set this field will print an error, and getting this field will return [code]0.0[/code]. Allows or forbids rotation on one or more axes, depending on the [enum RotationMode] constants being used. diff --git a/scene/2d/path_2d.cpp b/scene/2d/path_2d.cpp index 282d14da5d5..c3768386e9f 100644 --- a/scene/2d/path_2d.cpp +++ b/scene/2d/path_2d.cpp @@ -378,9 +378,10 @@ real_t PathFollow2D::get_progress() const { } void PathFollow2D::set_progress_ratio(real_t p_ratio) { - if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) { - set_progress(p_ratio * path->get_curve()->get_baked_length()); - } + ERR_FAIL_NULL_MSG(path, "Can only set progress ratio on a PathFollow2D that is the child of a Path2D which is itself part of the scene tree."); + ERR_FAIL_COND_MSG(path->get_curve().is_null(), "Can't set progress ratio on a PathFollow2D that does not have a Curve."); + ERR_FAIL_COND_MSG(!path->get_curve()->get_baked_length(), "Can't set progress ratio on a PathFollow2D that has a 0 length curve."); + set_progress(p_ratio * path->get_curve()->get_baked_length()); } real_t PathFollow2D::get_progress_ratio() const { diff --git a/scene/3d/path_3d.cpp b/scene/3d/path_3d.cpp index 1f8f7cd54ce..dc030b6a0f3 100644 --- a/scene/3d/path_3d.cpp +++ b/scene/3d/path_3d.cpp @@ -461,9 +461,10 @@ real_t PathFollow3D::get_progress() const { } void PathFollow3D::set_progress_ratio(real_t p_ratio) { - if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) { - set_progress(p_ratio * path->get_curve()->get_baked_length()); - } + ERR_FAIL_NULL_MSG(path, "Can only set progress ratio on a PathFollow3D that is the child of a Path3D which is itself part of the scene tree."); + ERR_FAIL_COND_MSG(path->get_curve().is_null(), "Can't set progress ratio on a PathFollow3D that does not have a Curve."); + ERR_FAIL_COND_MSG(!path->get_curve()->get_baked_length(), "Can't set progress ratio on a PathFollow3D that has a 0 length curve."); + set_progress(p_ratio * path->get_curve()->get_baked_length()); } real_t PathFollow3D::get_progress_ratio() const {