The most important data type in Godot.
var foo = 2 # foo is dynamically an integer foo = "Now foo is a string!" foo = Reference.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!"
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.
objecttype in place of Godot's Variant class when it needs to represent a dynamic value.
objectis the Mono runtime's equivalent of the same concept.
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 need to dig deeply into a hidden ProjectSettings setting: an Array of Dictionaries called "_global_script_classes". # Open your project.godot file to see it up close.
A Variant takes up only 20 bytes and can store almost any engine datatype inside of it. Variants are rarely used to hold information for long periods of time. Instead, they are used mainly for communication, editing, serialization and moving data around.
Godot has specifically invested in making its Variant class as flexible as possible; so much so that it is used for a multitude of operations to facilitate communication between all of Godot's systems.
Containers (Array and Dictionary): Both are implemented using variants. A Dictionary can match any datatype used as key to any other datatype. An Array just holds an array of Variants. Of course, a Variant can also hold a Dictionary and an Array inside, making it even more flexible.
Modifications to a container will modify all references to it. A Mutex should be created to lock it if multi-threaded access is desired.
© 2014–2020 Juan Linietsky, Ariel Manzur, Godot Engine contributors
Licensed under the MIT License.