Struct Variant
pub struct Variant { /* private fields */ }
Expand description
Godot variant type, able to store a variety of different types.
While Godot variants do not appear very frequently in Rust due to their lack of compile-time type-safety, they are central to all sorts of dynamic APIs. For example, if you want to call a method on an object based on a string, you will need variants to store arguments and return value.
§Conversions
For type conversions, please read the godot::meta
module docs.
§Godot docs
Implementations§
§impl Variant
impl Variant
pub fn nil() -> Variant
pub fn nil() -> Variant
Create an empty variant (null
value in GDScript).
If a Godot engine API accepts object (not variant) parameters and you’d like to pass null
, use
Gd::null_arg()
instead.
pub fn from<T>(value: T) -> Variantwhere
T: ToGodot,
pub fn from<T>(value: T) -> Variantwhere
T: ToGodot,
Create a variant holding a non-nil value.
Equivalent to value.to_variant()
.
pub fn to<T>(&self) -> Twhere
T: FromGodot,
pub fn to<T>(&self) -> Twhere
T: FromGodot,
⚠️ Convert to type T
, panicking on failure.
Equivalent to T::from_variant(&self)
.
§Panics
When this variant holds a different type.
pub fn try_to<T>(&self) -> Result<T, ConvertError>where
T: FromGodot,
pub fn try_to<T>(&self) -> Result<T, ConvertError>where
T: FromGodot,
Convert to type T
, returning Err
on failure.
Equivalent to T::try_from_variant(&self)
.
pub fn is_nil(&self) -> bool
pub fn is_nil(&self) -> bool
Checks whether the variant is empty (null
value in GDScript).
See also Self::get_type
.
pub fn get_type(&self) -> VariantType
pub fn get_type(&self) -> VariantType
Returns the type that is currently held by this variant.
If this variant holds a type Object
but no instance (represented as a null object pointer), then Nil
will be returned for
consistency. This may deviate from Godot behavior – for example, calling Node::get_node_or_null()
with an invalid
path returns a variant that has type Object
but acts like Nil
for all practical purposes.
pub fn call(&self, method: impl Into<StringName>, args: &[Variant]) -> Variant
pub fn call(&self, method: impl Into<StringName>, args: &[Variant]) -> Variant
⚠️ Calls the specified method
with the given args
.
Supports Object
as well as built-ins with methods (e.g. Array
, Vector3
, GString
, etc.).
§Panics
- If
self
is not a variant type which supports method calls. - If the method does not exist or the signature is not compatible with the passed arguments.
- If the call causes an error.
pub fn evaluate(&self, rhs: &Variant, op: VariantOperator) -> Option<Variant>
pub fn evaluate(&self, rhs: &Variant, op: VariantOperator) -> Option<Variant>
Evaluates an expression using a GDScript operator.
Returns the result of the operation, or None
if the operation is not defined for the given operand types.
Recommended to be used with fully-qualified call syntax.
For example, Variant::evaluate(&a, &b, VariantOperator::Add)
is equivalent to a + b
in GDScript.
pub fn stringify(&self) -> GString
pub fn stringify(&self) -> GString
Return Godot’s string representation of the variant.
See also Display
impl.
pub fn hash(&self) -> i64
pub fn hash(&self) -> i64
Return Godot’s hash value for the variant.
Godot equivalent : @GlobalScope.hash()
pub fn booleanize(&self) -> bool
pub fn booleanize(&self) -> bool
Interpret the Variant
as bool
.
Returns false
only if the variant’s current value is the default value for its type. For example:
nil
for the nil typefalse
for bool- zero for numeric types
- empty string
- empty container (array, packed array, dictionary)
- default-constructed other builtins (e.g. zero vector, degenerate plane, zero RID, etc…)
Trait Implementations§
§impl ArrayElement for Variant
impl ArrayElement for Variant
fn debug_validate_elements(_array: &Array<Self>) -> Result<(), ConvertError>
§impl Export for Variant
impl Export for Variant
§fn export_hint() -> PropertyHintInfo
fn export_hint() -> PropertyHintInfo
§fn as_node_class() -> Option<ClassName>
fn as_node_class() -> Option<ClassName>
§impl FromGodot for Variant
impl FromGodot for Variant
§fn try_from_godot(
via: <Variant as GodotConvert>::Via,
) -> Result<Variant, ConvertError>
fn try_from_godot( via: <Variant as GodotConvert>::Via, ) -> Result<Variant, ConvertError>
Err
on failure.§fn from_godot(via: Self::Via) -> Self
fn from_godot(via: Self::Via) -> Self
§fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
Variant
, returning Err
on failure.§fn from_variant(variant: &Variant) -> Self
fn from_variant(variant: &Variant) -> Self
§impl GodotConvert for Variant
impl GodotConvert for Variant
§impl ToGodot for Variant
impl ToGodot for Variant
§type ToVia<'v> = <Variant as GodotConvert>::Via
type ToVia<'v> = <Variant as GodotConvert>::Via
to_godot()
, which differs from Via
for pass-by-reference types.§fn to_godot(&self) -> <Variant as ToGodot>::ToVia<'_>
fn to_godot(&self) -> <Variant as ToGodot>::ToVia<'_>
§fn to_variant(&self) -> Variant
fn to_variant(&self) -> Variant
§impl Var for Variant
impl Var for Variant
fn get_property(&self) -> <Variant as GodotConvert>::Via
fn set_property(&mut self, value: <Variant as GodotConvert>::Via)
§fn var_hint() -> PropertyHintInfo
fn var_hint() -> PropertyHintInfo
GodotType::property_info
, e.g. for enums/newtypes.impl GodotType for Variant
Auto Trait Implementations§
impl Freeze for Variant
impl RefUnwindSafe for Variant
impl !Send for Variant
impl !Sync for Variant
impl Unpin for Variant
impl UnwindSafe for Variant
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)