godot::builtin

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

§Godot docs

Callable (stable)

Implementations§

§

impl Callable

pub fn from_object_method<T, S>(object: &Gd<T>, method_name: S) -> Callable
where T: GodotClass, S: AsArg<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 cannot 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 surrounding 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>

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 Callable

§

type Via = Callable

The type through which Self is represented in Godot.
§

impl ParamType for Callable

§

fn owned_to_arg<'v>(self) -> <Callable 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 Callable

§

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

§

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

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

fn to_godot(&self) -> <Callable 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 Callable

§

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

§

fn set_property(&mut self, value: <Callable 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 Callable

§

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

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

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.