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

Don't allow static functions to use non-static methods of the instance it's called from #6897

Closed
L4Vo5 opened this issue May 17, 2023 · 3 comments

Comments

@L4Vo5
Copy link

L4Vo5 commented May 17, 2023

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:

extends Node
class_name StaticTest

func _ready() -> void:
	print_name()
static func print_name():
	print(get(&"name"))

As well as this, on another node:

extends Node

func _ready() -> void:
	var a = StaticTest.new()
	a.print_name()

Note that these two things don't work:

extends Node

func _ready() -> void:
	StaticTest.print_name()

Runtime error: Cannot call method 'get' on a null value.

extends Node
class_name StaticTest

func _ready() -> void:
	print_name()
static func print_name():
	print(name)

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

@dalexeev
Copy link
Member

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 self prefix) and via an instance. This is not an error, although it may sometimes seem strange. Now this works for all static members, except for static variables (this is a bug). See godotengine/godot#77098.

@L4Vo5
Copy link
Author

L4Vo5 commented May 17, 2023

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

@dalexeev
Copy link
Member

@L4Vo5 I opened PR godotengine/godot#77151.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants