Variant
Variant objects box various Godot Objects, you create them with one of the constructors, and you can retrieve the contents using the various extension constructors that are declared on the various types that are wrapped.
Variant.swift:45class Variant
You can retrieve the type of a variant from the gtype
property.
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.
A Variant:
Can store almost any Godot engine datatype.
Can perform operations between many variants. GDScript uses Variant as its atomic/native datatype.
Can be hashed, so it can be compared to other variants.
Can be used to convert safely between datatypes.
Can be used to abstract calling methods and their arguments. Godot exports all its functions through variants.
Can be used to defer calls or move data between threads.
Can be serialized as binary and stored to disk, or transferred via network.
Can be serialized to text and use it for printing values and editable settings.
Can work as an exported property, so the editor can edit it universally.
Can be used for dictionaries, arrays, parsers, etc.
Modifications to a container will modify all references to it.
Citizens in SwiftGodot
Conformances
protocol CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.
Types
enum GType
enum Operator
enum VariantIndexerError
Errors raised by the variant subscript
Type members
init(
) Creates an empty Variant, that represents the Godot type
nil
init(Variant
) Creates a new Variant based on a copy of the reference variant passed in
init((some VariantStorable)?
) init(some VariantStorable
) static func typeName(GType
) -> String Gets the name of a Variant type.
static func == (lhs: Variant, rhs: Variant
) -> Bool Compares two variants, does this by delegating the comparison to Godot
Instance members
var debugDescription: String
var description: String
var gtype: GType
This describes the type of the data wrapped by this variant
var isNull: Bool
Returns true if the variant is flagged as being an object (
gtype == .object
) and it has a nil pointer.subscript(Int
) -> Variant? Variants that represent arrays can be indexed, this subscript allows you to fetch the individual elements of those arrays
func asObject<T>(T.Type
) -> T? Attempts to cast the Variant into a SwiftGodot.Object, if the variant contains a value of type
.object
, then a.object, the value
nil` is returned.func call(method: StringName, Variant...
) -> Result<Variant, CallErrorType> Invokes a variant’s method by name.
func hash(into: inout Hasher
)
Type features
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.