+2 votes

I was wondering why it isn't possible to define a static variable or constant in gdscript, I guess it is like that by design but I don't get it at all

Godot version 3.2.3
in Engine by (24 points)

4 Answers

0 votes

A constant can be defined using const key word, only a static variable can't be daclared, no idea why.

by (1,333 points)
0 votes

If you mean global variables, you can use singletons to define variables that can be accessed from anywhere in your program.

by (330 points)

No he's not talking about globals most likely he's a C/C++/C# programmer

Those languages you can keep var in memory like this

func setup():
    static bool runOnce = true:
    if runOnce:
        runOnce = false
        print("setting up")

No matter how many times you call the func setup() "setting up" will only be called once

Honestly this is only necessary in low level languages where you have to do memory management yourself

I had another exemple in mind :
I was just working on a custom camera and I wanted to implement animation for the camera movement, I was thinking of using the Tween class to help me move the camera, the problem is I wanted to have an exported parameter inside my CustomCamera class to let the user of this class select the type of transition they want.
Something like that would do the trick

exported (Tween.TransitionType) transition_type:int = Tween.TransitionType.TRANS_LINEAR

But I can't because the enum TransitionType inside the Tween class can't be set as static, to access the enumeration I have to instance an object first (a thing I can't do when exporting variable)

But yes I mostly have experience with C/C++/C#/Java languages so removing the possibility for me to declare parameters as static is tricky to me, I have to rethink entirely how to manage variables

I believe the way Godot sets up enums are like this
enum {TYPE1, TYPE2, TYPE3} so there exists no TransitionType var as they are added directly to the node

The quickest approach I can think of for your dilemma is to define your own transitions enum

export(TRANSITIONS) var transition = TRANSITIONS.BACK
+2 votes

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


func _ready():
    Statics.myClassCounter += 1
by (234 points)
+1 vote

Here's another work-around for static variable in GDScript, which hasn't been mentioned yet.

  1. Define a const Dictionary variable on your class.
  2. Store and access your static variables as properties on that Dictionary.

This works because the const Dictionary instance is shared between instances of the class. Even though the Dictionary is const and can't be re-assigned, it is still "mutable", so you can change its properties as much as you want.

Here's an example:

class_name Main
extends Node

func _ready() -> void:
    var a := MyClass.new()
    var b := MyClass.new()

    a.STATIC_VARIABLES.is_my_flag_enabled = true


class MyClass extends Reference:

    const STATIC_VARIABLES := {
        is_my_flag_enabled = false,
by (35 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected]ngine.org with your username.