As others have mentioned, Godot indeed knows the const
keyword, which will work pretty much the way you (probably) expect it to do.
For static
it's different, because even in C/C++ the meaning changes based on the context it's used in. It may hide a symbol from other translation units, it might mark a local variable global or "static" to all calls of a function, and it might mark a class member shared between all instances. All these things are possible in Godot, too, but there's no specific keyword to it and you might have to add some more "glue" to get the intended behavior.
One-time initialization/lockout
I guess this is most likely what you were looking for.
In general, I'd try to avoid this approach. If you have to fill an array or something once, try doing so in _ready()
. If you really want to do it (e.g. it's heavy and you don't want to do it all the time), just create an extra variable holding the status. This is something C/C++ compilers will do in background, too. It's actually extra overhead many forget whenever they throw in static
somewhere.
var something_init: bool = false
var something: int = 0
func some_method() -> void:
if not something_init:
something_init = true
# initialize something one time here
something = get_starting_value()
# do something else
something += 1
Hide symbol from other translation units
This doesn't really apply to Godot, since (so far) there's no way to directly refer to stuff from other code files (or use headers or similar).
Global variables shared between class instances
This is again something I'd try to avoid, but if you really have to, you can do so by moving your "static" variable to a global singleton.
Singleton (statics.gd
instantiated as Statics
)
var myClassCounter: int = 0
Class
func _ready():
Statics.myClassCounter += 1