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

Add support for static variables in GDScript #76264

Merged
merged 1 commit into from
Apr 27, 2023

Conversation

vnen
Copy link
Member

@vnen vnen commented Apr 19, 2023

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.

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.

@vnen vnen added this to the 4.1 milestone Apr 19, 2023
@vnen vnen requested review from a team as code owners April 19, 2023 18:48
@vnen vnen force-pushed the gdscript-static-variales branch 4 times, most recently from 41302c6 to 08a739a Compare April 20, 2023 14:47
@vnen
Copy link
Member Author

vnen commented Apr 20, 2023

I think I fixed the leak now.

@dalexeev
Copy link
Member

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. _static_init() and unload_static() do not appear in autocompletion.

modules/gdscript/doc_classes/GDScript.xml Outdated Show resolved Hide resolved
modules/gdscript/gdscript_parser.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_parser.cpp Outdated Show resolved Hide resolved
@adamscott
Copy link
Member

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.

It's in my "garbage collection" todo list.

Copy link
Member

@adamscott adamscott left a 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!

@vnen
Copy link
Member Author

vnen commented Apr 25, 2023

Updated the code to fix some of the mentioned issues. I did not touch autocompletion yet (other than adding _static_init()) but can be done at a later step if this is merged.

@akien-mga
Copy link
Member

Needs rebase, then should be ready to merge I think.

@vnen
Copy link
Member Author

vnen commented Apr 27, 2023

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.
@akien-mga akien-mga merged commit c4a9d32 into godotengine:master Apr 27, 2023
@akien-mga
Copy link
Member

Thanks!

@vnen vnen deleted the gdscript-static-variales branch April 27, 2023 16:57
@shendo
Copy link
Contributor

shendo commented Apr 28, 2023

Hi, this appears to have broken android template_debug builds on master, due to the mix of TOOLS_ENABLED/DEBUG_ENABLED conditions. eg.

scons platform=android target=template_debug arch=armv7
...
modules/gdscript/gdscript.cpp:746:3: error: use of undeclared identifier '_save_old_static_data'
                _save_old_static_data();
                ^
modules/gdscript/gdscript.cpp:823:3: error: use of undeclared identifier '_restore_old_static_data'
                _restore_old_static_data();
                ^
2 errors generated.
scons: *** [modules/gdscript/gdscript.android.template_debug.arm32.o] Error 1
scons: building terminated because of errors.

@akien-mga
Copy link
Member

@shendo Fixed with 7780926.

@atirut-w
Copy link
Contributor

How easily can this be ported to 4.x in the future?

@dalexeev
Copy link
Member

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!

@atirut-w
Copy link
Contributor

This was merged into master though?

@Zireael07
Copy link
Contributor

@atirut-w Master will become 4.1

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

Successfully merging this pull request may close these issues.

Add static variables in GDScript
7 participants