Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Callable

A built-in type representing a method or a standalone function.

Description

Callable is a built-in Variant type that represents a function. It can either be a method within an Object instance, or a standalone function not related to any object, like a lambda function. Like all Variant types, it can be stored in variables and passed to other functions. It is most commonly used for signal callbacks.

Example:

func print_args(arg1, arg2, arg3 = ""):
    prints(arg1, arg2, arg3)

func test():
    var callable = Callable(self, "print_args")
    callable.call("hello", "world")  # Prints "hello world ".
    callable.call(Vector2.UP, 42, callable)  # Prints "(0, -1) 42 Node(node.gd)::print_args".
    callable.call("invalid")  # Invalid call, should have at least 2 arguments.

In GDScript, it's possible to create lambda functions within a method. Lambda functions are custom callables that are not associated with an Object instance. Optionally, lambda functions can also be named. The name will be displayed in the debugger, or when calling get_method.

func _init():
    var my_lambda = func (message):
        print(message)

    # Prints Hello everyone!
    my_lambda.call("Hello everyone!")

    # Prints "Attack!", when the button_pressed signal is emitted.
    button_pressed.connect(func(): print("Attack!"))

Note: Methods of native types such as Signal, Array, or Dictionary are not of type Callable in order to avoid unnecessary overhead. If you need to pass those methods as Callable, use a lambda function as a wrapper.

func _init():
    var my_dictionary = { "hello": "world" }

    # This will not work, `clear` is not a callable.
    create_tween().tween_callback(my_dictionary.clear)

    # This will work, as lambdas are custom callables.
    create_tween().tween_callback(func(): my_dictionary.clear())

참고

There are notable differences when using this API with C#. See C#과 GDScript의 API 차이점 for more information.

Constructors

Callable

Callable ( )

Callable

Callable ( Callable from )

Callable

Callable ( Object object, StringName method )

Methods

Callable

bind ( ... ) vararg const

Callable

bindv ( Array arguments )

Variant

call ( ... ) vararg const

void

call_deferred ( ... ) vararg const

Variant

callv ( Array arguments ) const

Array

get_bound_arguments ( ) const

int

get_bound_arguments_count ( ) const

StringName

get_method ( ) const

Object

get_object ( ) const

int

get_object_id ( ) const

int

hash ( ) const

bool

is_custom ( ) const

bool

is_null ( ) const

bool

is_standard ( ) const

bool

is_valid ( ) const

void

rpc ( ... ) vararg const

void

rpc_id ( int peer_id, ... ) vararg const

Callable

unbind ( int argcount ) const

Operators

bool

operator != ( Callable right )

bool

operator == ( Callable right )


Constructor Descriptions

Callable Callable ( )

Constructs an empty Callable, with no object nor method bound.


Callable Callable ( Callable from )

Constructs a Callable as a copy of the given Callable.


Callable Callable ( Object object, StringName method )

Creates a new Callable for the method named method in the specified object.


Method Descriptions

Callable bind ( ... ) vararg const

Returns a copy of this Callable with one or more arguments bound. When called, the bound arguments are passed after the arguments supplied by call. See also unbind.

Note: When this method is chained with other similar methods, the order in which the argument list is modified is read from right to left.


Callable bindv ( Array arguments )

Returns a copy of this Callable with one or more arguments bound, reading them from an array. When called, the bound arguments are passed after the arguments supplied by call. See also unbind.

Note: When this method is chained with other similar methods, the order in which the argument list is modified is read from right to left.


Variant call ( ... ) vararg const

Calls the method represented by this Callable. Arguments can be passed and should match the method's signature.


void call_deferred ( ... ) vararg const

Calls the method represented by this Callable in deferred mode, i.e. at the end of the current frame. Arguments can be passed and should match the method's signature.

func _ready():
    grab_focus.call_deferred()

See also Object.call_deferred.


Variant callv ( Array arguments ) const

Calls the method represented by this Callable. Unlike call, this method expects all arguments to be contained inside the arguments Array.


Array get_bound_arguments ( ) const

Return the bound arguments (as long as get_bound_arguments_count is greater than zero), or empty (if get_bound_arguments_count is less than or equal to zero).


int get_bound_arguments_count ( ) const

Returns the total amount of arguments bound (or unbound) via successive bind or unbind calls. If the amount of arguments unbound is greater than the ones bound, this function returns a value less than zero.


StringName get_method ( ) const

Returns the name of the method represented by this Callable. If the callable is a GDScript lambda function, returns the function's name or "<anonymous lambda>".


Object get_object ( ) const

Returns the object on which this Callable is called.


int get_object_id ( ) const

Returns the ID of this Callable's object (see Object.get_instance_id).


int hash ( ) const

Returns the 32-bit hash value of this Callable's object.

Note: Callables with equal content will always produce identical hash values. However, the reverse is not true. Returning identical hash values does not imply the callables are equal, because different callables can have identical hash values due to hash collisions. The engine uses a 32-bit hash algorithm for hash.


bool is_custom ( ) const

Returns true if this Callable is a custom callable. Custom callables are created from bind or