-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
Don't allow static functions to use non-static methods of the instance it's called from #6897
Comments
Static members CANNOT access non-static members. Your first example already results in a compilation error: But a static function can create an instance (of its own or another class), and call non-static methods on it, this is intended behavior, not an error: static func print_name():
var instance = new()
print(instance.get(&"name")) Non-static members CAN access static members. Both within a class (with or without the |
Huh, my first example does work fine in 4.1 dev2, but indeed has an error even in dev1. Guess I'll have to look into filing a bug report instead of a proposal, since it's a regression and not a lacking feature |
@L4Vo5 I opened PR godotengine/godot#77151. |
Describe the project you are working on
A level editor.
Describe the problem or limitation you are having in your project
In GDScript, static functions can access instance methods if they're called on an instance of the same type (not sure of the exact requirements, e.g. how it relates to inheritance/extending).
In other words, this code works:
As well as this, on another node:
Note that these two things don't work:
Runtime error:
Cannot call method 'get' on a null value.
Parser Error: Identifier not found: name
I doubt being able to access methods is intended. After all, using variables is rejected, but it can be circumvented with
get
.Describe the feature / enhancement and how it helps to overcome the problem or limitation
Don't allow instance methods to be called from static variables. Also, make the improper usage of instance methods raise a parser error (like variables) instead of failing at runtime.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Using a non-static instance method on a script should raise an error just like using a non-static instance variable does. Static methods shouldn't "know" that these variables/methods even exist.
If this enhancement will not be used often, can it be worked around with a few lines of script?
No, you can just do something that is conceptually wrong but without it causing an error.
Is there a reason why this should be core and not an add-on in the asset library?
gdscript is core
The text was updated successfully, but these errors were encountered: