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

GDScript: Allow override default values of base class variables #93787

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dalexeev
Copy link
Member

@dalexeev dalexeev commented Jun 30, 2024

@override var script_var: int = f(2)
@onready @override var name: StringName = &"Test"

TODO: Implement proposal godotengine/godot-proposals#8045 (only possible for inline setters/getters).

@dalexeev dalexeev added this to the 4.x milestone Jun 30, 2024
@dalexeev dalexeev requested a review from a team as a code owner June 30, 2024 16:57
@dalexeev dalexeev marked this pull request as draft June 30, 2024 16:57
@Mickeon
Copy link
Contributor

Mickeon commented Jun 30, 2024

How far does the @override go? Would it actually let you override, say, export properties as well?

Also I see my PR to highlight overridden properties is being put to even better use now.

@dalexeev
Copy link
Member Author

How far does the @override go? Would it actually let you override, say, export properties as well?

Variable overriding is reasonably quite limited:

  • You cannot change the variable type.
  • You cannot override static variables because they belong to their classes, not to the instance.
  • You cannot change or remove setter/getter because GDScript is a fairly dynamic language and it would be weird to have different behavior depending on the type and casts inferred by the analyzer.

The only thing this feature is really intended for, is to change the default value of a variable in an inherited class, just like it is possible in native classes. This can be useful because the overriden default value is shown in the documentation and in the Inspector (the latter is not yet debugged). Also it shouldn't require @tool.

Note that the current implementation the default value is not replaced, just the inherited implicit initializer is executed after the base implicit initializer. That is, the variable will be assigned twice (or more, as many overrides). In all initializations the setter will not be called, since there is an exception for the initializer. I'm not sure if this is a good thing, but I don't see how we could implement it without complicating the compiler.

As for changing export info, it doesn't work correctly now, but I think it can be fixed:

So, I'm not sure about this feature yet, but I decided to check it out to see how feasible it is.

@Mickeon
Copy link
Contributor

Mickeon commented Jun 30, 2024

You cannot change the variable type.

Good, but in practice it would still be desirable to narrow it down with covariance like in #82477

That is, the variable will be assigned twice (or more, as many overrides). In all initializations the setter will not be called, since there is an exception for the initializer. I'm not sure if this is a good thing, but I don't see how we could implement it without complicating the compiler.

This may actually not be too much of a deal-breaker for most, but it is certainly a waste of processing time which would be nice to avoid.

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.

Allow ability to override parent variables
2 participants