Attention: Here be dragons
This is the
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
The most important data type in Godot.
var foo = 2 # foo is dynamically an integer foo = "Now foo is a string!" foo = RefCounted.new() # foo is an Object var bar: int = 2 # bar is a statically typed integer. # bar = "Uh oh! I can't make static variables become a different type!"
// C# is statically typed. Once a variable has a type it cannot be changed. You can use the `var` keyword to let the compiler infer the type automatically. var foo = 2; // Foo is a 32-bit integer (int). Be cautious, integers in GDScript are 64-bit and the direct C# equivalent is `long`. // foo = "foo was and will always be an integer. It cannot be turned into a string!"; var boo = "Boo is a string!"; var ref = new RefCounted(); // var is especially useful when used together with a constructor. // Godot also provides a Variant type that works like a union of all the Variant-compatible types. Variant fooVar = 2; // fooVar is dynamically an integer (stored as a `long` in the Variant type). fooVar = "Now fooVar is a string!"; fooVar = new RefCounted(); // fooVar is a GodotObject.
Godot tracks all scripting API variables within Variants. Without even realizing it, you use Variants all the time. When a particular language enforces its own rules for keeping data typed, then that language is applying its own custom logic over the base Variant scripting API.
GDScript automatically wrap values in them. It keeps all data in plain Variants by default and then optionally enforces custom static typing rules on variable types.
C# is statically typed, but uses its own implementation of the
Varianttype in place of Godot's Variant class when it needs to represent a dynamic value. A
Variantcan be assigned any compatible type implicitly but converting requires an explicit cast.
var foo = 2 match typeof(foo): TYPE_NIL: print("foo is null") TYPE_INTEGER: print("foo is an integer") TYPE_OBJECT: # Note that Objects are their own special category. # To get the name of the underlying Object type, you need the `get_class()` method. print("foo is a(n) %s" % foo.get_class()) # inject the class name into a formatted string. # Note also that there is not yet any way to get a script's `class_name` string easily. # To fetch that value, you can use ProjectSettings.get_global_class_list().