-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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: Issues with static variables #77098
Comments
I'm interested in this is there anything more you may need help with |
Regarding this, calling static methods from instances is seen as a bad thing and generates a warning, so I assumed this wouldn't be wanted for static variables either. Since it's a new feature it can be forbidden from the start instead of just a warning. But static variables do not have the same potential issue as static methods, then maybe it does not matter. This is also looks inconsistent for me if calling static methods from instance generates warnings but accessing static variables do not. |
That warning is a bit too eager though, as seen in #74397. So we probably shouldn't take it as design guidance, unless we can make sure it's only emitted in cases where it makes sense. It was added mostly as bandaid for issues such as
which were valid code in 3.x but would suddenly do nothing in 4.x, as the method is now static and returns an instance. That's arguably the only case which should really trigger a warning (calling a static method on an instance that returns an instance of the same type, i.e. a static constructor). |
I agree that accessing static members via an instance is confusing and is not recommended. But given that it works for other types of static members (methods, constants, enums, inner classes), it would be strange not to support it only for variables. If warnings are needed, we can add them later. Also inside a class you don't strictly separate instance properties and static variables, there are no mandatory prefixes (like At the moment |
If you can't access static variables from an instance, then when you're using an object from another script you won't be able to access its static variables at all unless you know its exact type, even though you can access instance variables without knowing the type either. |
Godot version
v4.1.dev.custom_build [5c653c2]
System information
Kubuntu 23.04
Issue description
1. The setter is called when initializing a static variable (for both cases: a. if a static type is specified and b. if there is an initializer).
This is because
_make_static_initializer()
usescodegen.generator->write_set_named()
rather thancodegen.generator->write_assign()
since static variables belong to the script. We probably need a separate instruction that will not call the setter. I'm not familiar with the late stages, so I'm not sure how to properly fix this.Also food for thought: does this make static variables slightly slower than non-static ones?
MRP
77098_1.zip
2. An inner class static variable is falsely considered readonly.
But you can work around this:
This is an analyzer bug. I can try to fix it.
https://github.com/godotengine/godot/blob/5c653c27cdf779e1e70a16ec9514435537a01779/modules/gdscript/gdscript_analyzer.cpp#L2465C18-L2468
For scripts with
class_name
this already works.MRP
77098_2.zip
3. You can apply the
@export*
and@onready
annotations to static variables, but that doesn't make sense and may not work correctly (since one script can be attached to multiple objects). I can add the necessary checks.MRP
77098_3.zip
4.
Not a bug,but more of a design issue. Should static variables be accessed via an instance? Currently no, but within a class, you can access both static and non-static variables the same way, without theself
prefix.Or perhaps we should think again about the keyword for accessing the current class (
Self
). See godotengine/godot-proposals#391.EDIT: You can access any static member via
self
or instance, except for static variables. Probably for consistency and compatibility we should allow this for static variables as well.By the way, the line 38 is incorrectly marked as safe.
MRP
77098_4.zip
5. Separated to #78195.
Old content
5. Static variables are not in the property list of both the object and the script. If we want the former, then we need a new
PROPERTY_USAGE_STATIC
flag, but I think we can just add it to the script's property list.EDIT: I think this makes sense, since we have to add the
static
flag in the user documentation, and also because point 4.Steps to reproduce
See above.
Minimal reproduction project
N/ABugsquad edit: add MRPs
The text was updated successfully, but these errors were encountered: