-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Classes are not considered constants in GDScript #33531
Comments
our I think we have to make our |
This might be related to the usual reference cycle problem. Consider: class X:
func do_stuff():
return Y[0].new()
const Y = [X] |
yeah, still we can do this class X:
func do_stuff() -> X:
return [X][0].new() runtime constant resolve the reference cycle problem (but changing runtime const might break our old stuff). |
Support for immutable variables is being tracked in #23695. |
@Calinou ## script.gd
extends Node
const c = 3
var v = 3 extends Node2D
var script = preload("res://script.gd")
func _init():
script.c ## valid
script.v ## invalid
script.new().v ## valid |
Doesn't need to be runtime, we can tell which class it is at compilation time. We just need to make the parser know that this case can be considered constant. The compiler will already have a script reference to place there when needed.
This can become quite complex, but it isn't need to solve this particular issue. If you think this can be useful in an specific case, you can open a proposal: godotengine/godot-proposals#573 |
I'm not sure if this is the same or related issue... If the class is defined in a separate file, then assignment to a const is allowed. HOWEVER, GDScript doesn't infer static type as it does with a var...
Different files...
(3.2.1 stable) |
@ThakeeNathees, thanks for the links, but I'm failing to see the relevance. My post is about the behavior of regular file classes assigned to const, which "works" (unlike inner classes) but does not allow static typing as I expected. I edited my code and comments above to make this point more clear. I may have misjudged its relationship to this issue (or even if I have an issue). Please enlighten me before I open a separate issue. |
@charliewhitfield oh It's a completely different situation, sorry for that, I think you should create a new issue so someone can fix it. |
OK. I agree it is different than either existing issue. I'll open a new one. |
Godot 3.2 alpha1
The following examples all produce an
Expected a constant expression
error:I wanted to use a class in a constant, but it's not possible, while it is possible with scripts obtained with
preload
.The text was updated successfully, but these errors were encountered: