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
Implementations§
§impl Callable
impl Callable
pub fn from_object_method<T, S>(object: &Gd<T>, method_name: S) -> Callable
pub fn from_object_method<T, S>(object: &Gd<T>, method_name: S) -> Callable
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
Available on since_api="4.2"
only.
pub fn from_fn<F, S>(name: S, rust_function: F) -> Callable
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) -> Callablewhere
C: RustCallable,
Available on since_api="4.2"
only.
pub fn from_custom<C>(callable: C) -> Callablewhere
C: RustCallable,
since_api="4.2"
only.Create a highly configurable callable from Rust.
See RustCallable
for requirements on the type.
pub fn invalid() -> Callable
pub fn invalid() -> Callable
Creates an invalid/empty object that cannot be called.
Godot equivalent: Callable()
pub fn callv(&self, arguments: &Array<Variant>) -> Variant
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
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>
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>>
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 whether the method exists for that
target or not). Also returns None
if the object is dead. You can differentiate these two cases using object_id()
.
Godot equivalent: get_object
pub fn object_id(&self) -> Option<InstanceId>
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.
If the pointed-to object is dead, the ID will still be returned. Use object()
to check for liveness.
Godot equivalent: get_object_id
pub fn hash(&self) -> u32
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
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
Trait Implementations§
§impl ArrayElement for Callable
impl ArrayElement for Callable
fn debug_validate_elements(_array: &Array<Self>) -> Result<(), ConvertError>
§impl FromGodot for Callable
impl FromGodot for Callable
§fn try_from_godot(
via: <Callable as GodotConvert>::Via,
) -> Result<Callable, ConvertError>
fn try_from_godot( via: <Callable as GodotConvert>::Via, ) -> Result<Callable, 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 Callable
impl GodotConvert for Callable
§impl ToGodot for Callable
impl ToGodot for Callable
§type ToVia<'v> = <Callable as GodotConvert>::Via
type ToVia<'v> = <Callable as GodotConvert>::Via
to_godot()
, which differs from Via
for pass-by-reference types.§fn to_godot(&self) -> <Callable as ToGodot>::ToVia<'_>
fn to_godot(&self) -> <Callable as ToGodot>::ToVia<'_>
§fn to_variant(&self) -> Variant
fn to_variant(&self) -> Variant
§impl Var for Callable
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
fn var_hint() -> PropertyHintInfo
GodotType::property_info
, e.g. for enums/newtypes.impl GodotType for Callable
Auto Trait Implementations§
impl Freeze for Callable
impl RefUnwindSafe for Callable
impl !Send for Callable
impl !Sync for Callable
impl Unpin for Callable
impl UnwindSafe for Callable
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
)