Variant and Object
The Variant and Object classes are core concept of Godot's architecture and determine the way resources and scripts will be set up, so having a basic understanding of those classes is a good first step to get started with Godot. Don't be afraid if you find it all too abstract – you can always come back to it later once you have more experience in making games with the engine.
Variants
In the C++ code the Variant class (core/variant.h) is used to hold values of different types. Variants are used throughout the engine to carry values in different situations where it is necessary to hold values in a generic container. Example situations where Variants are used:
- For any value that is carried in a GDScript variable or passed around,
- For any value that is edited in the engine editor,
- For any value that is serialized (for saving resources, transmitting over the network, etc.),
- Values carried on keyframes of animations.
Variants carry all the basic types necessary for game development, i.e.:
- Numbers (int, double, bool),
- Strings (unicode),
- Types to describe the space (Vector2, Vector3, Plane, Rectangle, AABB, Quaternion),
- Matrices (Transform for 3D, Matrix32 for 2D),
- Arrays (raw bytes, numbers, vectors, colors, Variants),
- Dictionaries (using Variants for both keys and values),
- Engine types (Object, RID),
- Other misc types (Colors, Input Event, optimized scene paths).
Objects
Object is a basic type for most objects in the engine like scenes, nodes and resources that we will explain in the next chapter. Here we are still at a very abstract level, close to the engine conception.
As in most object-oriented programming language, the main features of the Object class are:
- Inheritance. Objects know their type and inheritance line.
- Reference. Object types are registered in the engine (ObjectTypeDB), and can be instanced using their type name. All instances of Object have a unique numerical ID, which can be used to reference the Object and retrieve the original instance if necessary (for example when working with a Foreign Function Interface to call other languages, such as Java).
- Introspection. Objects describe their interfaces (properties and methods) using Variants. This description can be used to interact with the Object through various methods, for example:
- Serializing the Object,
- Calling the Object's methods and properties from GDScript (or any other scripting language),
- Animating the Object. An animation track can set values to the Object's properties, or call its methods,
- Editing. A generic editing UI can edit any property of the Object.