godot::prelude

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

Variant (stable)

Implementations§

§

impl 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) -> Variant
where T: ToGodot,

Create a variant holding a non-nil value.

Equivalent to value.to_variant(), but consumes the argument.

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 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 AsArg<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 Export for Variant

§

fn export_hint() -> PropertyHintInfo

The export info to use for an exported field of this type, if no other export info is specified.
§

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 ParamType for Variant

§

fn owned_to_arg<'v>(self) -> <Variant as ParamType>::Arg<'v>

Converts an owned value to the canonical argument type, which can be passed to impl AsArg<T>. Read more
§

impl PartialEq for Variant

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

impl ToGodot for Variant

§

type ToVia<'v> = <Variant as GodotConvert>::Via

Target type of to_godot(), which can differ from Via for pass-by-reference types. Read more
§

fn to_godot(&self) -> <Variant as ToGodot>::ToVia<'_>

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

fn to_variant(&self) -> Variant

Converts this type to a 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

Specific property hints, only override if they deviate from GodotType::property_info, e.g. for enums/newtypes.
§

impl ArrayElement for Variant

§

impl<'r> AsArg<Variant> for &'r 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§

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

🔬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,

Source§

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§

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>,

Source§

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>,

Source§

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.