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

Allow readonly and writeonly C# properties to be accessed from GDScript #67304

Merged
merged 1 commit into from
Sep 26, 2023

Conversation

wscalf
Copy link
Contributor

@wscalf wscalf commented Oct 12, 2022

A potential fix for #67167

As mentioned on the issue, I'm new to the codebase and may be way off. Skepticism and feedback welcome. :)

The goal is to allow GDScript to access readonly and writeonly properties on C# objects, like the str2 property of the C# class in the cross-language scripting tutorial: https://docs.godotengine.org/en/latest/tutorials/scripting/cross_language_scripting.html

As far as I can tell, this functionality is intended- ScriptPropertiesGenerator.cs even has a check to skip over generating setters for readonly properties and fields- but the code never gets there because WhereIsGodotScriptSerializable filters out readonly and writeonly properties, and readonly fields. As far as I can tell on that, it's because they're not appropriate for script serialization, not because they shouldn't be accessible via the generated GetGodotClassPropertyValue and SetGodotClassPropertyValue methods.

So, the goal of this PR is to separate those criteria into two methods- one for the type compatibility check and one for the serializability check so that ScriptSerializationGenerator.cs can call both and keep the same output it has today, while ScriptPropertiesGenerator.cs calls just the type check and generates all applicable getters and setters.

Bugsquad edit:

@wscalf wscalf requested a review from a team as a code owner October 12, 2022 11:45
@raulsntos raulsntos added this to the 4.0 milestone Oct 12, 2022
@raulsntos
Copy link
Member

Thanks for your contribution! We prefer a single commit in a given PR (unless there's good reason to keep the changes separate), could you squash the two commits into one? See PR workflow for instructions.

@raulsntos raulsntos linked an issue Oct 12, 2022 that may be closed by this pull request
@neikeq
Copy link
Contributor

neikeq commented Oct 13, 2022

I don't think write-only properties are a good idea, and I don't think anyone would miss that feature.

Read-only makes sense. I would prefer not to implement this bit by bit, as it could end up causing confusion, but since read-only properties are very common, I guess it's fine. Just be aware that for now, the value of read-only properties will be lost when reloading assemblies.

@wscalf
Copy link
Contributor Author

wscalf commented Oct 13, 2022

On write-only properties, I mainly included them for consistency and because C# allows them, but I also tested with 3.4.4 (just because it's the stable version I have around), and it does seem to support accessing read-only and write-only properties, but I agree, it's unlikely anyone would miss write-only. Are you saying I should limit this PR to readonly properties/fields?

On read-only, yeah, this was my main goal. I use a lot of computed properties . :)

@wscalf
Copy link
Contributor Author

wscalf commented Mar 11, 2023

Hey, sorry for letting this slip by me- I didn't realize there was a merge conflict. I think it's addressed now, and as far as I can tell, I'm getting the expected results in the samples project and in my original repro project.
Edit: or at least a start. The analyzers are erroring on the sample having readonly and writeonly properties, and I'm not sure what to do with them- maybe they should log an informational message about behavior that may not be supported? Or just removed?

Copy link
Member

@raulsntos raulsntos left a comment

Choose a reason for hiding this comment

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

If the purpose of this PR is to allow accessing read-only properties from GDScript, they don't need to be [Export]-able.

Since currently the value of read-only properties will be lost when reloading assemblies, I'm hesitant to allow exporting them (we can add support for exporting them in a future PR).

@wscalf
Copy link
Contributor Author

wscalf commented Aug 8, 2023

Ack, closed by accident. Work still in progress.

@wscalf
Copy link
Contributor Author

wscalf commented Aug 12, 2023

Gotcha, you're right, they don't need to be exported to be accessible from GDScript, and making them assignable in the editor mostly wouldn't make any sense (init-only might, actually, but it'd be super different and 100% agree it doesn't belong in this PR.)

I think I confused myself by trying to add the properties to a class that was for exported properties. I made separate classes for non-exported, readonly/initonly, and writeonly properties.

Copy link
Member

@raulsntos raulsntos left a comment

Choose a reason for hiding this comment

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

Thanks, I like how this looks already and it works as expected from my testing. Just needs a few minor changes and I think it's good to merge.

@wscalf wscalf force-pushed the master branch 2 times, most recently from a6eae93 to 6027724 Compare August 13, 2023 17:45
@wscalf wscalf requested a review from a team as a code owner August 13, 2023 17:45
@raulsntos raulsntos requested review from a team and removed request for a team August 13, 2023 18:11
Copy link
Member

@raulsntos raulsntos left a comment

Choose a reason for hiding this comment

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

I think this is good enough, and we can add support for serializing/exporting readonly members in a future PR. So I'm approving this PR, but would love to know @neikeq's opinion.

@akien-mga akien-mga requested a review from RedworkDE August 28, 2023 11:07
@akien-mga akien-mga modified the milestones: 4.x, 4.2 Aug 28, 2023
@akien-mga akien-mga requested a review from neikeq August 28, 2023 11:07
@shana
Copy link
Contributor

shana commented Sep 26, 2023

It makes sense to me, it adds a missing piece of language interoperability in a simple and straightforward way. 🚀

@akien-mga akien-mga merged commit 4410b0b into godotengine:master Sep 26, 2023
@akien-mga
Copy link
Member

Thanks! And congrats for your first merged Godot contribution 🎉

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.

C# readonly properties inaccessible from GDScript
5 participants