-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
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
Conversation
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs
Outdated
Show resolved
Hide resolved
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs
Outdated
Show resolved
Hide resolved
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. |
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. |
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 . :) |
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. |
There was a problem hiding this 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).
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs
Outdated
Show resolved
Hide resolved
Ack, closed by accident. Work still in progress. |
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. |
There was a problem hiding this 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.
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/OneWayProperties/AllReadOnly.cs
Outdated
Show resolved
Hide resolved
...les/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/OneWayProperties/AllWriteOnly.cs
Outdated
Show resolved
Hide resolved
...ditor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/OneWayProperties/MixedReadOnlyWriteOnly.cs
Outdated
Show resolved
Hide resolved
...ditor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/OneWayProperties/MixedReadOnlyWriteOnly.cs
Outdated
Show resolved
Hide resolved
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/OneWayProperties/AllReadOnly.cs
Outdated
Show resolved
Hide resolved
...ditor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/OneWayProperties/MixedReadOnlyWriteOnly.cs
Outdated
Show resolved
Hide resolved
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedProperties.cs
Outdated
Show resolved
Hide resolved
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs
Outdated
Show resolved
Hide resolved
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs
Outdated
Show resolved
Hide resolved
a6eae93
to
6027724
Compare
There was a problem hiding this 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.
It makes sense to me, it adds a missing piece of language interoperability in a simple and straightforward way. 🚀 |
Thanks! And congrats for your first merged Godot contribution 🎉 |
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: