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

Fix invalid return from some more _get/_set #84060

Merged
merged 1 commit into from
Oct 28, 2023

Conversation

AThousandShips
Copy link
Member

Invalidly returned true on the non-matched path

Follow-up to:

Reviewers: If these cases are invalid (i.e. all paths should return true) then please explain why and I'll add a note for it instead

@AThousandShips AThousandShips added bug topic:animation cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Oct 27, 2023
@AThousandShips AThousandShips added this to the 4.3 milestone Oct 27, 2023
@AThousandShips AThousandShips requested a review from a team October 27, 2023 15:27
@AThousandShips AThousandShips requested a review from a team as a code owner October 27, 2023 15:27
@@ -48,12 +48,14 @@ bool Bone2D::_set(const StringName &p_path, const Variant &p_value) {
} else if (path.begins_with("default_length")) {
set_length(p_value);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is enforced by format, unless I move the closing brackets into the #ifdef

@KoBeWi
Copy link
Member

KoBeWi commented Oct 27, 2023

Given that all classes with the wrong code are related, the person who implemented these methods probably didn't know how to use them correctly. There is no reason to write it like that.

You should return false at the end of each method instead of adding else. true should be returned only when r_ret is modified.

@AThousandShips
Copy link
Member Author

AThousandShips commented Oct 27, 2023

You should return false at the end of each method instead of adding else.

Not unless you also add return true in all the if blocks, that's why the trailing true is there, this syntax is used in a lot of valid places as well, just not this way

See for example Animation::_set/_get

@KoBeWi
Copy link
Member

KoBeWi commented Oct 27, 2023

Not unless you also add return true in all the if blocks

Yep, the blocks should return.

See for example Animation::_set/_get

That one is wrong too.

@AThousandShips
Copy link
Member Author

I mean this syntax is used in plenty of places, can modify it here to simplify it but kept the changes minimal

@AThousandShips
Copy link
Member Author

AThousandShips commented Oct 27, 2023

Compare:

bool SkeletonModification2DLookAt::_get(const StringName &p_path, Variant &r_ret) const {
	String path = p_path;

	if (path.begins_with("enable_constraint")) {
		r_ret = get_enable_constraint();
	} else if (path.begins_with("constraint_angle_min")) {
		r_ret = Math::rad_to_deg(get_constraint_angle_min());
	} else if (path.begins_with("constraint_angle_max")) {
		r_ret = Math::rad_to_deg(get_constraint_angle_max());
	} else if (path.begins_with("constraint_angle_invert")) {
		r_ret = get_constraint_angle_invert();
	} else if (path.begins_with("constraint_in_localspace")) {
		r_ret = get_constraint_in_localspace();
	} else if (path.begins_with("additional_rotation")) {
		r_ret = Math::rad_to_deg(get_additional_rotation());
	}
#ifdef TOOLS_ENABLED
	else if (path.begins_with("editor/draw_gizmo")) {
		r_ret = get_editor_draw_gizmo();
	}
#endif // TOOLS_ENABLED
	else {
		return false;
	}

	return true;
}

To:

bool SkeletonModification2DLookAt::_get(const StringName &p_path, Variant &r_ret) const {
	String path = p_path;

	if (path.begins_with("enable_constraint")) {
		r_ret = get_enable_constraint();
		return true;
	} else if (path.begins_with("constraint_angle_min")) {
		r_ret = Math::rad_to_deg(get_constraint_angle_min());
		return true;
	} else if (path.begins_with("constraint_angle_max")) {
		r_ret = Math::rad_to_deg(get_constraint_angle_max());
		return true;
	} else if (path.begins_with("constraint_angle_invert")) {
		r_ret = get_constraint_angle_invert();
		return true;
	} else if (path.begins_with("constraint_in_localspace")) {
		r_ret = get_constraint_in_localspace();
		return true;
	} else if (path.begins_with("additional_rotation")) {
		r_ret = Math::rad_to_deg(get_additional_rotation());
		return true;
	}

#ifdef TOOLS_ENABLED
	if (path.begins_with("editor/draw_gizmo")) {
		r_ret = get_editor_draw_gizmo();
		return true;
	}
#endif // TOOLS_ENABLED

	return false;
}

Can go with the second syntax if desired, but I think it's for a separate PR cleaning up and unifying the style

@YuriSizov
Copy link
Contributor

I think the existing syntax has the right to be if there are further actions required after the if-else block. If not, returning early should be clearer, since it indicates that this is the last step and you don't need to read any further. But I also agree it can be pretty noisy.

So no strong opinion either way.

@KoBeWi
Copy link
Member

KoBeWi commented Oct 27, 2023

Ok I misunderstood the code. This syntax is indeed better.

Invalidly returned `true` on the non-matched path
Copy link
Member

@fire fire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention is that we have many set and gets, there's a complicated switch / if else statement that else returns the the least likely and then falls through to the most likely Boolean value.

@AThousandShips AThousandShips modified the milestones: 4.3, 4.2 Oct 28, 2023
@AThousandShips AThousandShips removed the cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release label Oct 28, 2023
@akien-mga akien-mga merged commit 191195a into godotengine:master Oct 28, 2023
15 checks passed
@AThousandShips AThousandShips deleted the get_set_fix branch October 28, 2023 16:54
@akien-mga
Copy link
Member

Thanks!

@AThousandShips
Copy link
Member Author

Thank you!

@YuriSizov
Copy link
Contributor

Cherry-picked for 4.1.4.

@YuriSizov YuriSizov removed the cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release label Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants