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

Allowed setters and getters to take additional arguments (superseded by #78991) #76665

Closed

Conversation

nlupugla
Copy link
Contributor

@nlupugla nlupugla commented May 2, 2023

Closes godotengine/godot-proposals#6750.
Closes godotengine/godot-proposals#3096.
Closes godotengine/godot-proposals#6107 (discussion).
Closes #74144.

addresses the following proposals:
godotengine/godot-proposals#2817.
godotengine/godot-proposals#2983.
godotengine/godot-proposals#4867.
godotengine/godot-proposals#4868.

For example, the following is now possible:

@tool
extends Node2D

@onready var sprite := $Icon as Sprite2D
@export var offset : Vector2 : set=set_sprite_property, get=get_sprite_property
@export var centered : bool : set=set_sprite_property, get=get_sprite_property


func get_sprite_property(property : StringName) -> Variant:
	return sprite.get(property)
	
	
func set_sprite_property(property : StringName, value : Variant) -> void:
	sprite.set(property, value)

Previously, one would have had to use a more verbose and tedious approach:

@tool
extends Node2D

@onready var sprite := $Icon as Sprite2D
@export var offset : Vector2 : set=set_sprite_offset, get=get_sprite_offset
@export var centered : bool : set=set_sprite_centered, get=get_sprite_centered


func get_sprite_offset() -> Variant:
	return sprite.get("offset")
	
	
func set_sprite_offset(value : Variant) -> void:
	sprite.set("offset", value)


func get_sprite_centered() -> Variant:
	return sprite.get("centered")
	
	
func set_sprite_property(value : Variant) -> void:
	sprite.set("centered", value)

TODO:

  • Attach some GDScript tests.
  • Require that the first argument of a 2-argument setter and a 1-argument getter be a StringName

@Calinou Calinou added this to the 4.x milestone May 2, 2023
@nlupugla nlupugla force-pushed the set-get-variable-property branch from cb5fba7 to c409ead Compare May 2, 2023 00:59
@nlupugla nlupugla force-pushed the set-get-variable-property branch 2 times, most recently from ca7ffea to 0507a82 Compare May 3, 2023 03:02
@nlupugla
Copy link
Contributor Author

nlupugla commented May 3, 2023

I noticed the tests complained about "name" shadowing a class variable. I wrote a few lines like Variant name = p_name;. I changed them to Variant property_name = p_name, so hopefully that fixes the issue.

Copy link
Member

@dalexeev dalexeev left a comment

Choose a reason for hiding this comment

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

I tested and reviewed this PR and added some changes: signature analysis, support for optional arguments (godotengine/godot-proposals#6107, vnen's comment), tests.

Commit: dalexeev@19b170a
Patch: review.patch.txt

You can apply the patch with git apply or git am (and then you need to squash the commits into one). You can credit me as a co-author if you wish.

@nlupugla
Copy link
Contributor Author

Thanks for the review @dalexeev :)
I applied your patch and pushed the changes, but I don't think I quite figured out the whole rebasing thing. I tried to squash my commits together and credit you as a coauthor, but got confused somewhere along the way...

Copy link
Member

@dalexeev dalexeev left a comment

Choose a reason for hiding this comment

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

There should be one commit. Use rebase, don't merge master into your branch.

prop: prefix World! suffix
prop: <null>
Copy link
Member

Choose a reason for hiding this comment

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

This is a bug. This is why I prefer not to use --gdscript-generate-tests but to edit .out manually, even if it's tedious.

my_float_alias = 2.0
my_int_alias = 3
print((my_float_alias == my_float) && (my_float == 2.0))
print((my_int_alias == my_int) && (my_int == 3))
Copy link
Member

Choose a reason for hiding this comment

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

This file does not follow the GDScript style guide (wrong spaces, symbolic logical operators, missing trailing newline). This is undesirable in non-parser tests. In addition, I do not see the point in this test, in my opinion it duplicates the ones I have already added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I was going to ask you about this test on Rocket. I had written this a week or two ago before your patch. I noticed it was redundant with your tests. I can remove it.

@dalexeev

This comment was marked as outdated.

@nlupugla
Copy link
Contributor Author

Thanks for preparing the squashed commit! Unless there's a rush to get the PR accepted, I want to see if I can figure out how to do it on my own, just for self-education. If there is a rush, let me know and I'll go ahead and apply the patch :)

@nlupugla nlupugla force-pushed the set-get-variable-property branch from 679f7c3 to 45c0ab6 Compare May 12, 2023 15:59
@nlupugla
Copy link
Contributor Author

Alright, I think I managed to figure out how to squash the commits and credit you. Thanks for the help :)
Just so you know, I haven't had a chance yet to test the changes you made. Should I trust that your changes are good and remove my "draft" status or would you appreciate if I did some testing?

@nlupugla nlupugla force-pushed the set-get-variable-property branch 2 times, most recently from 45c0ab6 to 744056a Compare May 12, 2023 16:12
@dalexeev
Copy link
Member

Alright, I think I managed to figure out how to squash the commits and credit you.

No, it doesn't look correct. You just rolled back to your old version and added my tests, but the rest of the changes were discarded (signature analysis, support for optional arguments, etc.). Also, Co-authored-by is incorrect.

Should I trust that your changes are good and remove my "draft" status or would you appreciate if I did some testing?

I ran the tests locally, on my PC they complete successfully. Also I tested manually, looks correct.

Sorry for making the review in the form of an "ultimatum patch", I respect your desire to figure it out on your own. I just want to help you and save time, yours and mine. In any case, I want to say that as a first contribution, your work is already impressive!

@nlupugla nlupugla force-pushed the set-get-variable-property branch from 744056a to 713cb8c Compare May 12, 2023 18:40
@nlupugla
Copy link
Contributor Author

Thanks :)
I think I fixed it now.

@dalexeev

This comment was marked as resolved.

@nlupugla nlupugla force-pushed the set-get-variable-property branch from 713cb8c to b1c5072 Compare May 12, 2023 19:01
@nlupugla
Copy link
Contributor Author

Nice. I'm recompiling on my machine just to test things out again. When I'm satisfied, I'll remove my draft status.

@nlupugla nlupugla force-pushed the set-get-variable-property branch from b1c5072 to 78120ae Compare May 12, 2023 19:42
has_valid_setter = true;

GDScriptParser::ParameterNode *value_arg = setter_function->parameters[setter_function->mandatory_parameter_count - 1];
if (setter_function->mandatory_parameter_count > 2) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (setter_function->mandatory_parameter_count > 2) {
if (setter_function->mandatory_parameter_count == 0 || setter_function->mandatory_parameter_count > 2) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed it.
Thanks for the help and patience!

@nlupugla nlupugla force-pushed the set-get-variable-property branch 2 times, most recently from 3acea5c to 14aba97 Compare May 13, 2023 04:35
@nlupugla nlupugla marked this pull request as ready for review May 13, 2023 04:36
@nlupugla nlupugla requested a review from a team as a code owner May 13, 2023 04:36
@nlupugla
Copy link
Contributor Author

I did some brief testing on my end and everything seemed to check out. I removed the draft status from my PR.

I edited my original post so that it mentions being able to have optional arguments in the setters and getters. Should I edit the title of my PR as well, or will that mess something up?

@dalexeev

This comment was marked as resolved.

@nlupugla nlupugla changed the title Allowed setters and getters to take the name of their property as an additional argument Allowed setters and getters to take additional arguments May 13, 2023
@dalexeev

This comment was marked as resolved.

Copy link
Member

@dalexeev dalexeev left a comment

Choose a reason for hiding this comment

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

Looks good to me! There is only a minor suggestion for naming CallError variables. I think this PR is ready for review by the GDScript team.

@nlupugla
Copy link
Contributor Author

Looks good to me! There is only a minor suggestion for naming CallError variables. I think this PR is ready for review by the GDScript team.

Awesome :)

So should I name the call errors to err, ce, or call_err?

Also, what are the next steps for updating the documentation on setters/getters to reflect the changes? Should I start writing a PR to update the docs now, or wait until these changes are approved?

@dalexeev
Copy link
Member

So should I name the call errors to err, ce, or call_err?

It seems to me that the name err is worse, since it is usually used for bool or Error codes. For CallError, the names ce, call_err, or the full call_error can be used. But in any case, it has the lowest priority. Things like renaming local/internal variables, fixing comment style, adding/removing blank lines are best done on a case-by-case basis, on adjacent lines, as long as it doesn't require a lot of effort and doesn't interfere with the perception of major changes. You are not required to bring the entire file to a single style, there is no point or benefit in this.

Should I start writing a PR to update the docs now, or wait until these changes are approved?

I don't think this is strictly regulated, but in practice PRs for docs and/or demo projects are usually opened after the PR in the main repository is merged. Since the docs cannot be updated before the main PR is merged. Plus, during the review process, you may be asked to make some changes, which will also require you to update the docs PR.

Therefore, in my opinion, it is better to wait for the final version, so as not to do extra work. It's normal for the documentation (Manual) to be a bit behind the current state, no one expects it to be updated the same day a feature is in master. The exception is the Class Reference, since it's in the main repository, we try to keep it up to date.

@nlupugla nlupugla force-pushed the set-get-variable-property branch from c738a72 to f5c3662 Compare May 24, 2023 15:20
@nlupugla
Copy link
Contributor Author

Something failed in the Github Actions Workflow run that ran last week. It seemed unrelated to the changes on my branch, so I just updated my branch to be synced with the current upstream/master with the hopes that it will fix the problem. Is there a way for me to run the workflow tests locally so I don't have to wait on Github Actions to be sure the commit is okay?

@dalexeev
Copy link
Member

Yes, you can:

scons dev_build=true tests=true
bin/<godot executable> --test

But this is only for your platform. Tests on GitHub run for different platforms and compilation flags.

@nlupugla
Copy link
Contributor Author

nlupugla commented May 24, 2023

Ah, gotcha. The failure was on Linux and I have Windows, so I guess there's not much I can do but hope it passes this time :)

@nlupugla nlupugla force-pushed the set-get-variable-property branch from f5c3662 to e91fa45 Compare May 27, 2023 14:44
@nlupugla
Copy link
Contributor Author

Hi @dalexeev :)

I updated my branch to be concurrent with master. Let me know if there's anything else I need to do to help the PR move along :)

@AThousandShips
Copy link
Member

AThousandShips commented Jun 14, 2023

This is a new feature and therefore unlikely to be included in 4.1, as we are in feature freeze, so chances are it will have to wait for approval until after the release of 4.1, to focus on things for 4.1 (Hopefully that won't take too long either!)

@nlupugla nlupugla force-pushed the set-get-variable-property branch from e91fa45 to 176d55a Compare June 19, 2023 16:44
@nlupugla
Copy link
Contributor Author

Reworked the error messages as suggested.

@dalexeev
Copy link
Member

dalexeev commented Jul 2, 2023

Need a rebase after #77129.

@nlupugla nlupugla force-pushed the set-get-variable-property branch from 176d55a to befc384 Compare July 3, 2023 15:11
@nlupugla nlupugla requested a review from a team as a code owner July 3, 2023 15:11
@nlupugla nlupugla closed this Jul 3, 2023
@nlupugla nlupugla force-pushed the set-get-variable-property branch from befc384 to e044e13 Compare July 3, 2023 15:12
@AThousandShips
Copy link
Member

You reset the branch to the base branch, which closes the PR automatically

@nlupugla nlupugla changed the title Allowed setters and getters to take additional arguments Allowed setters and getters to take additional arguments (deprecated, see https://github.com/godotengine/godot/pull/78991) Jul 3, 2023
@AThousandShips
Copy link
Member

Superseded by: #78991

@dalexeev dalexeev changed the title Allowed setters and getters to take additional arguments (deprecated, see https://github.com/godotengine/godot/pull/78991) Allowed setters and getters to take additional arguments (superseded by #78991) Jul 4, 2023
@YuriSizov YuriSizov removed this from the 4.x milestone Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants