Struct godot::builtin::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.

See also Godot documentation for Variant.

Implementations§

§

impl Variant

pub fn nil() -> Variant

Create an empty variant (null value in GDScript).

pub fn from<T>(value: T) -> Variant
where T: ToGodot,

Create a variant holding a non-nil value.

Equivalent to value.to_variant().

pub fn to<T>(&self) -> T
where 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,

Convert to type T, returning Err on failure.

Equivalent to T::try_from_variant(&self).

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

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

⚠️ 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>

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

Return Godot’s string representation of the variant.

See also Display impl.

pub fn hash(&self) -> i64

Return Godot’s hash value for the variant.

Godot equivalent : @GlobalScope.hash()

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 type
  • false 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 Clone for Variant

§

fn clone(&self) -> Variant

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Variant

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for Variant

§

fn default() -> Variant

Returns the “default value” for a type. Read more
§

impl Display for Variant

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Drop for Variant

§

fn drop(&mut self)

Executes the destructor for this type. Read more
§

impl FromGodot for Variant

§

fn try_from_godot( via: <Variant as GodotConvert>::Via, ) -> Result<Variant, ConvertError>

Converts the Godot representation to this type, returning Err on failure.
§

fn from_godot(via: Self::Via) -> Self

⚠️ Converts the Godot representation to this type. Read more
§

fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>

Performs the conversion from a Variant, returning Err on failure.
§

fn from_variant(variant: &Variant) -> Self

⚠️ Performs the conversion from a Variant. Read more
§

impl GodotConvert for Variant

§

type Via = Variant

The type through which Self is represented in Godot.
§

impl PartialEq for Variant

§

fn eq(&self, other: &Variant) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl ToGodot for Variant

§

fn to_godot(&self) -> <Variant as GodotConvert>::Via

Converts this type to the Godot type by reference, usually by cloning.
§

fn into_godot(self) -> <Variant as GodotConvert>::Via

Converts this type to the Godot type. Read more
§

fn to_variant(&self) -> Variant

Converts this type to a Variant.
§

impl ArrayElement for Variant

§

impl GodotType for Variant

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.