Callable¶
An object representing a method in a certain object that can be called.
Description¶
Callable
is a first class object which can be held in variables and passed to functions. It represents a given method in an Object, and is typically 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.
public void PrintArgs(object arg1, object arg2, object arg3 = null)
{
GD.PrintS(arg1, arg2, arg3);
}
public void Test()
{
Callable callable = new Callable(this, nameof(PrintArgs));
callable.Call("hello", "world"); // Prints "hello world null".
callable.Call(Vector2.Up, 42, callable); // Prints "(0, -1) 42 Node(Node.cs)::PrintArgs".
callable.Call("invalid"); // Invalid call, should have at least 2 arguments.
}
Constructors¶
Callable ( ) |
|
Callable ( Object object, StringName method ) |
Methods¶
bind ( ... ) vararg const |
|
call ( ... ) vararg const |
|
void |
call_deferred ( ... ) vararg const |
get_method ( ) const |
|
get_object ( ) const |
|
get_object_id ( ) const |
|
hash ( ) const |
|
is_custom ( ) const |
|
is_null ( ) const |
|
is_standard ( ) const |
|
is_valid ( ) const |
|
void |
rpc ( ... ) vararg const |
void |
|
Operators¶
operator != ( ) |
|
operator != ( Callable right ) |
|
operator == ( ) |
|
operator == ( Callable right ) |
Constructor Descriptions¶
Callable Callable ( )
Constructs a null Callable
with no object nor method bound.
Constructs a Callable
as a copy of the given Callable
.
Callable Callable ( Object object, StringName method )
Creates a new Callable
for the method called method
in the specified object
.
Method Descriptions¶
Callable bind ( ... ) vararg const
Returns a copy of this Callable
with the arguments bound. Bound arguments are passed after the arguments supplied by call.
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. during the idle frame. Arguments can be passed and should match the method's signature.
StringName get_method ( ) const
Returns the name of the method represented by this Callable
.
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: Callable
s 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 whose behavior differs based on implementation details. Custom callables are used in the engine for various reasons. If true
, you can't use get_method.
bool is_null ( ) const
Returns true
if this Callable
has no target to call the method on.
bool is_standard ( ) const
Returns true
if this Callable
is a standard callable, referencing an object and a method using a StringName.
bool is_valid ( ) const
Returns true
if the object exists and has a valid function assigned, or is a custom callable.
void rpc ( ... ) vararg const
Perform an RPC (Remote Procedure Call). This is used for multiplayer and is normally not available unless the function being called has been marked as RPC. Calling it on unsupported functions will result in an error.
void rpc_id ( int peer_id, ... ) vararg const
Perform an RPC (Remote Procedure Call) on a specific peer ID (see multiplayer documentation for reference). This is used for multiplayer and is normally not available unless the function being called has been marked as RPC. Calling it on unsupported functions will result in an error.
Returns a copy of this Callable
with the arguments unbound. Calling the returned Callable
will call the method without the extra arguments that are supplied in the Callable
on which you are calling this method.
Operator Descriptions¶
bool operator != ( )
Returns true
if both Callable
s invoke different targets.
bool operator == ( )
Returns true
if both Callable
s invoke the same custom target.