Struct godot::prelude::Callable

pub struct Callable { /* private fields */ }
Expand description

A Callable represents a function in Godot.

Usually a callable is a reference to an Object and a method name, this is a standard callable. But can also be a custom callable, which is usually created from bind, unbind, or a GDScript lambda. See Callable::is_custom.

Currently it is impossible to use bind and unbind in GDExtension, see godot-cpp#802.

Implementations§

§

impl Callable

pub fn from_object_method<T, S>(object: &Gd<T>, method_name: S) -> Callable
where T: GodotClass, S: Into<StringName>,

Create a callable for the method object::method_name.

See also Gd::callable().

Godot equivalent: Callable(Object object, StringName method)

pub fn from_fn<F, S>(name: S, rust_function: F) -> Callable
where F: 'static + Send + Sync + FnMut(&[&Variant]) -> Result<Variant, ()>, S: Into<GString>,

Available on since_api="4.2" only.

Create a callable from a Rust function or closure.

name is used for the string representation of the closure, which helps debugging.

Callables created through multiple from_fn() calls are never equal, even if they refer to the same function. If you want to use equality, either clone an existing Callable instance, or define your own PartialEq impl with Callable::from_custom.

§Example
let callable = Callable::from_fn("sum", |args: &[&Variant]| {
    let sum: i32 = args.iter().map(|arg| arg.to::<i32>()).sum();
    Ok(sum.to_variant())
});

pub fn from_custom<C>(callable: C) -> Callable
where C: RustCallable,

Available on since_api="4.2" only.

Create a highly configurable callable from Rust.

See RustCallable for requirements on the type.

pub fn invalid() -> Callable

Creates an invalid/empty object that is not able to be called.

Godot equivalent: Callable()

pub fn callv(&self, arguments: Array<Variant>) -> Variant

Calls the method represented by this callable.

Arguments passed should match the method’s signature.

  • If called with more arguments than expected by the method, the extra arguments will be ignored and the call continues as normal.
  • If called with fewer arguments than expected it will crash Godot, without triggering UB.
  • If called with arguments of the wrong type then an error will be printed and the call will return NIL.
  • If called on an invalid Callable then no error is printed, and NIL is returned.

Godot equivalent: callv

pub fn bindv(&self, arguments: Array<Variant>) -> Callable

Returns a copy of this Callable with one or more arguments bound, reading them from an array.

Godot equivalent: bindv

pub fn method_name(&self) -> Option<StringName>

Returns the name of the method represented by this callable. If the callable is a lambda function, returns the function’s name.

§Known Bugs

Getting the name of a lambda errors instead of returning its name, see godot#73052.

Godot equivalent: get_method

pub fn object(&self) -> Option<Gd<Object>>

Returns the object on which this callable is called.

Returns None when this callable doesn’t have any target object to call a method on, regardless of if the method exists for that target or not.

Godot equivalent: get_object

pub fn object_id(&self) -> Option<InstanceId>

Returns the ID of this callable’s object, see also Gd::instance_id.

Returns None when this callable doesn’t have any target to call a method on.

Godot equivalent: get_object_id

pub fn hash(&self) -> u32

Returns the 32-bit hash value of this callable’s object.

Godot equivalent: hash

pub fn is_custom(&self) -> bool

Returns true if this callable is a custom callable.

Custom callables are mainly created from bind or unbind. In GDScript, lambda functions are also custom callables.

If a callable is not a custom callable, then it is considered a standard callable, this function is the opposite of Callable.is_standard.

Godot equivalent: is_custom

pub fn is_null(&self) -> bool

Returns true if this callable has no target to call the method on.

This is not the negated form of is_valid, as is_valid will return false if the callable has a target but the method does not exist.

Godot equivalent: is_null

pub fn is_valid(&self) -> bool

Returns true if the callable’s object exists and has a valid method name assigned, or is a custom callable.

Godot equivalent: is_valid

Trait Implementations§

§

impl Clone for Callable

§

fn clone(&self) -> Callable

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 Callable

§

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

Formats the value using the given formatter. Read more
§

impl Display for Callable

§

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

Formats the value using the given formatter. Read more
§

impl Drop for Callable

§

fn drop(&mut self)

Executes the destructor for this type. Read more
§

impl FromGodot for Callable

§

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

Performs the conversion.
§

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

⚠️ Performs the conversion. Read more
§

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

Performs the conversion from a Variant.
§

fn from_variant(variant: &Variant) -> Self

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

impl GodotConvert for Callable

§

type Via = Callable

The type through which Self is represented in Godot.
§

impl PartialEq for Callable

§

fn eq(&self, other: &Callable) -> 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 Callable

§

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

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

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

Converts this type to the Godot type. Read more
§

fn to_variant(&self) -> Variant

Converts this type to a Variant.
§

impl TypeStringHint for Callable

§

fn type_string() -> String

Returns the representation of this type as a type string. Read more
§

impl Var for Callable

§

impl ArrayElement for Callable

§

impl GodotType for Callable

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