Constants in GDScript

Constants are values that cannot be changed once declared. In this example, we’ll see how to work with constants in GDScript.

# declare a constant value
const S = "constant"

func _ready():
    print(S)
    
    const N = 500000000

    const D = 3e20 / N
    print(D)
    
    # Constant expressions perform arithmetic with arbitrary precision.
    print(int(D))
    
    # A number can be given a type by using it in a context that requires one,
    # such as a variable assignment or function call. For example, here math.sin expects a float.
    print(sin(float(N)))

To run the program, save the code in a .gd file, and add it to a node in your Godot project.

$ godot

Here’s the output you can expect:

"constant"
600000000.0
600000000
-0.28470407323754404

Now that we can work with constants in GDScript, let’s learn more about the language.