Skip to content

Commit 9fb9915

Browse files
committed
GDScript: Document static variables
1 parent ccae259 commit 9fb9915

File tree

1 file changed

+127
-12
lines changed

1 file changed

+127
-12
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ in case you want to take a look under the hood.
191191
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
192192
| func | Defines a function. |
193193
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
194-
| static | Defines a static function. Static member variables are not allowed. |
194+
| static | Defines a static function or a static member variable. |
195195
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
196196
| const | Defines a constant. |
197197
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
@@ -412,8 +412,8 @@ Both of these are the same::
412412

413413
.. _doc_gdscript_onready_annotation:
414414

415-
`@onready` annotation
416-
~~~~~~~~~~~~~~~~~~~~~
415+
``@onready`` annotation
416+
~~~~~~~~~~~~~~~~~~~~~~~
417417

418418
When using nodes, it's common to desire to keep references to parts
419419
of the scene in a variable. As scenes are only warranted to be
@@ -847,6 +847,110 @@ Valid types are:
847847
You can turn off this check, or make it only a warning, by changing it in
848848
the project settings. See :ref:`doc_gdscript_warning_system` for details.
849849

850+
Static variables
851+
^^^^^^^^^^^^^^^^
852+
853+
A class member variable can be declared static::
854+
855+
static var a
856+
857+
Static variables belong to the class, not instances. This means that static variables
858+
share values between multiple instances, unlike regular member variables.
859+
860+
From inside a class, you can access static variables from any function, both static and non-static.
861+
From outside the class, you can access static variables using the class or an instance
862+
(the second is not recommended as it is less readable).
863+
864+
.. note::
865+
866+
The ``@export`` and ``@onready`` annotations cannot be applied to a static variable.
867+
Local variables cannot be static.
868+
869+
The following example defines a ``Person`` class with a static variable named ``max_id``.
870+
We increment the ``max_id`` in the ``_init()`` function. This makes it easy to keep track
871+
of the number of ``Person`` instances in our game.
872+
873+
::
874+
875+
# person.gd
876+
class_name Person
877+
878+
static var max_id = 0
879+
880+
var id
881+
var name
882+
883+
func _init(p_name):
884+
max_id += 1
885+
id = max_id
886+
name = p_name
887+
888+
In this code, we create two instances of our ``Person`` class and check that the class
889+
and every instance have the same ``max_id`` value, because the variable is static and accessible to every instance.
890+
891+
::
892+
893+
# test.gd
894+
extends Node
895+
896+
func _ready():
897+
var person1 = Person.new("John Doe")
898+
var person2 = Person.new("Jane Doe")
899+
900+
print(person1.id) # 1
901+
print(person2.id) # 2
902+
903+
print(Person.max_id) # 2
904+
print(person1.max_id) # 2
905+
print(person2.max_id) # 2
906+
907+
Static variables can have type hints, setters and getters::
908+
909+
static var balance: int = 0
910+
911+
static var debt: int:
912+
get:
913+
return -balance
914+
set(value):
915+
balance = -value
916+
917+
A base class static variable can also be accessed via a child class::
918+
919+
class A:
920+
static var x = 1
921+
922+
class B extends A:
923+
pass
924+
925+
func _ready():
926+
prints(A.x, B.x) # 1 1
927+
A.x = 2
928+
prints(A.x, B.x) # 2 2
929+
B.x = 3
930+
prints(A.x, B.x) # 3 3
931+
932+
``@static_unload`` annotation
933+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
934+
935+
Since GDScript classes are resources, having static variables in a script prevents it from being unloaded
936+
even if there are no more instances of that class and no other references left. This can be important
937+
if static variables store large amounts of data or hold references to other project resources, such as scenes.
938+
You should clean up this data manually, or use the :ref:`@static_unload <class_@GDScript_annotation_@static_unload>`
939+
annotation if static variables don't store important data and can be reset.
940+
941+
.. warning::
942+
943+
Currently, due to a bug, scripts are never freed, even if ``@static_unload`` annotation is used.
944+
945+
Note that ``@static_unload`` applies to the entire script (including inner classes)
946+
and must be placed at the top of the script, before ``class_name`` and ``extends``::
947+
948+
@static_unload
949+
class_name MyNode
950+
extends Node
951+
952+
See also `Static functions`_ and `Static constructor`_.
953+
850954
Casting
851955
^^^^^^^
852956

@@ -1080,15 +1184,15 @@ Lambda functions capture the local environment. Local variables are passed by va
10801184
Static functions
10811185
^^^^^^^^^^^^^^^^
10821186

1083-
A function can be declared static. When a function is static, it has no
1084-
access to the instance member variables or ``self``. This is mainly
1085-
useful to make libraries of helper functions::
1187+
A function can be declared static. When a function is static, it has no access to the instance member variables or ``self``.
1188+
A static function has access to static variables. Also static functions are useful to make libraries of helper functions::
10861189

10871190
static func sum2(a, b):
10881191
return a + b
10891192

10901193
Lambdas cannot be declared static.
10911194

1195+
See also `Static variables`_ and `Static constructor`_.
10921196

10931197
Statements and control flow
10941198
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1467,13 +1571,11 @@ If you want to use ``extends`` too, you can keep both on the same line::
14671571

14681572
class_name MyNode extends Node
14691573

1470-
.. note:: Godot's class syntax is compact: it can only contain member variables or
1471-
functions. You can use static functions, but not static member variables. In the
1472-
same way, the engine initializes variables every time you create an instance,
1473-
and this includes arrays and dictionaries. This is in the spirit of thread
1474-
safety, since scripts can be initialized in separate threads without the user
1475-
knowing.
1574+
.. note::
14761575

1576+
Godot initializes non-static variables every time you create an instance,
1577+
and this includes arrays and dictionaries. This is in the spirit of thread safety,
1578+
since scripts can be initialized in separate threads without the user knowing.
14771579

14781580
Inheritance
14791581
^^^^^^^^^^^
@@ -1589,6 +1691,19 @@ There are a few things to keep in mind here:
15891691
func _init():
15901692
super(5)
15911693

1694+
Static constructor
1695+
^^^^^^^^^^^^^^^^^^
1696+
1697+
A static constructor is a static function ``_static_init`` that is called automatically
1698+
when the class is loaded, after the static variables have been initialized::
1699+
1700+
static var my_static_var = 1
1701+
1702+
static func _static_init():
1703+
my_static_var = 2
1704+
1705+
A static constructor cannot take arguments and must not return any value.
1706+
15921707
Inner classes
15931708
^^^^^^^^^^^^^
15941709

0 commit comments

Comments
 (0)