Variant and objects
Variant and objects
Variant and objects are very core concept of Godot structure especially when scripting, which comes very quickly in the game production.
In C++ the class Variant (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's 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 on 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, for example:
- Numbers (int, double, bool)
- Strings (unicode)
- Types to describe spaces (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)
Object is a basic type for most objects in the engine like scene, nodes and resources that we will explain in the next chapter. Here we are still at a very abstrac level, close to the engine conception.
As is any programming language, Objects main features will be :
- 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 as 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 interface (properties and methods) using Variants. This description can be used to interact with the object through varios 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.