-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
Add support for static variables in GDScript #76264
Conversation
41302c6
to
08a739a
Compare
I think I fixed the leak now. |
1. Runtime error when using a static function in a static variable initializer. extends Node
static func f():
return "test"
static var s = f() # Runtime Error: Invalid call. Nonexistent function 'f' in base 'Nil'.
func _ready() -> void:
print(s) 2. Static variables of inner classes are not initialized. extends Node
class A:
static var a := 42
func _ready() -> void:
print(A.a) # <null> 3. Static variables do not appear in autocompletion when accessed from outside. 4. |
It's in my "garbage collection" todo list. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved by the GDScript team in a PR meeting.
Thank you George for this big PR. Truly a great job. What would we do without you!
08a739a
to
c1a81c0
Compare
Updated the code to fix some of the mentioned issues. I did not touch autocompletion yet (other than adding |
Needs rebase, then should be ready to merge I think. |
c1a81c0
to
8b62d7a
Compare
Rebased. |
Which allows editable data associated with a particular class instead of the instance. Scripts with static variables are kept in memory indefinitely unless the `@static_unload` annotation is used or the `static_unload()` method is called on the GDScript. If the custom function `_static_init()` exists it will be called when the class is loaded, after the static variables are set.
8b62d7a
to
0ba6048
Compare
Thanks! |
Hi, this appears to have broken android template_debug builds on master, due to the mix of TOOLS_ENABLED/DEBUG_ENABLED conditions. eg.
|
How easily can this be ported to 4.x in the future? |
This has already been merged for 4.1 (and therefore also for 4.x). You probably mean backport for 4.0.x. I think this is against Godot release policy, as this change is too big and risky for patch releases, and most importantly, it's a feature implementation, not a bug fix. But the good news is that you won't have to wait very long for 4.1! |
This was merged into |
@atirut-w Master will become 4.1 |
Which allows editable data associated with a particular class instead of the instance. Scripts with static variables are kept in memory indefinitely unless the
@static_unload
annotation is used or thestatic_unload()
method is called on the GDScript.If the custom function
_static_init()
exists it will be called when the class is loaded, after the static variables are set.Closes godotengine/godot-proposals#6156
Note: During development I noticed a problem with GDScriptCache that is keeping all scripts in memory even after not being referenced anymore, so the
static_unload
implemented here (both method and annotation) won't be effective until the problem is fixed.