Callable
A built-in type representing a method or a standalone function.
Callable.swift:23class Callable
Callable
is a built-in Variant
type that represents a function. It can either be a method within an Object
instance, or a custom callable used for different purposes (see isCustom
). Like all Variant
types, it can be stored in variables and passed to other functions. It is most commonly used for signal callbacks.
Example:
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 getMethod
.
In GDScript, you can access methods and global functions as Callable
s:
Citizens in SwiftGodot
Conformances
protocol ContentVariantRepresentable
Some of Godot’s builtin classes use ContentType for storage. This needs to be public because it affects their initialization, but SwiftGodot users should never need to conform their types to
ContentVariantRepresentable
.protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Typealiases
Type members
init(
) Constructs an empty
Callable
, with no object nor method bound.init(@escaping (borrowing Arguments) -> Variant
) Creates a Callable instance from a Swift function
init(alreadyOwnedContent: ContentType
) init(content: ContentType
) init(from: Callable
) init(object: Object, method: StringName
) Creates a new
Callable
for the method namedmethod
in the specifiedobject
.static var godotType: Variant.GType
static let zero: ContentType
static func create(variant: Variant, method: StringName
) -> Callable Creates a new
Callable
for the method namedmethod
in the specifiedvariant
. To represent a method of a built-inVariant
type, a custom callable is used (seeisCustom
). Ifvariant
isObject
, then a standard callable will be created instead.static func != (lhs: Callable, rhs: Callable
) -> Bool Returns
true
if bothCallable
s invoke different targets.static func == (lhs: Callable, rhs: Callable
) -> Bool Returns
true
if bothCallable
s invoke the same custom target.
Instance members
var content: ContentType
func bind(Variant...
) -> Callable Returns a copy of this
Callable
with one or more arguments bound. When called, the bound arguments are passed after the arguments supplied bycall()
. See alsounbind(argcount:)
.func bindv(arguments: GArray
) -> Callable 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 bycall()
. See alsounbind(argcount:)
.func call(Variant...
) -> Variant Calls the method represented by this
Callable
. Arguments can be passed and should match the method’s signature.func callDeferred(Variant...
) 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 callv(arguments: GArray
) -> Variant Calls the method represented by this
Callable
. Unlikecall()
, this method expects all arguments to be contained inside thearguments
GArray
.func getArgumentCount(
) -> Int64 Returns the total number of arguments this
Callable
should take, including optional arguments. This means that any arguments bound withbind()
are subtracted from the result, and any arguments unbound withunbind(argcount:)
are added to the result.func getBoundArguments(
) -> GArray Return the bound arguments (as long as
getBoundArgumentsCount
is greater than zero), or empty (ifgetBoundArgumentsCount
is less than or equal to zero).func getBoundArgumentsCount(
) -> Int64 Returns the total amount of arguments bound (or unbound) via successive
bind()
orunbind(argcount:)
calls. If the amount of arguments unbound is greater than the ones bound, this function returns a value less than zero.func getMethod(
) -> StringName 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>"
.func getObjectId(
) -> Int64 Returns the ID of this
Callable
’s object (seegetInstanceId
).func hash(
) -> Int64 Returns the 32-bit hash value of this
Callable
’s object.func isCustom(
) -> Bool Returns
true
if thisCallable
is a custom callable. Custom callables are used:func isNull(
) -> Bool Returns
true
if thisCallable
has no target to call the method on.func isStandard(
) -> Bool Returns
true
if thisCallable
is a standard callable. This method is the opposite ofisCustom
. Returnsfalse
if this callable is a lambda function.func isValid(
) -> Bool Returns
true
if the callable’s object exists and has a valid method name assigned, or is a custom callable.func rpc(Variant...
) Perform an RPC (Remote Procedure Call) on all connected peers. This is used for multiplayer and is normally not available, unless the function being called has been marked as RPC (using [annotation @GDScript.@rpc] or
rpcConfig(method:config:)
). Calling this method on unsupported functions will result in an error. SeeNode/rpc(method:)
.func rpcId(peerId: Int64, Variant...
) 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 (using [annotation @GDScript.@rpc] or
rpcConfig(method:config:)
). Calling this method on unsupported functions will result in an error. SeeNode/rpcId(peerId:method:)
.func unbind(argcount: Int64
) -> Callable Returns a copy of this
Callable
with a number of arguments unbound. In other words, when the new callable is called the last few arguments supplied by the user are ignored, according toargcount
. The remaining arguments are passed to the callable. This allows to use the original callable in a context that attempts to pass more arguments than this callable can handle, e.g. a signal with a fixed number of arguments. See alsobind()
.
Type features
init?(Variant
) static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant.
static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant. This is useful when you want one method to call that will return the unwrapped Variant, regardless of whether it is a SwiftGodot.Object or not.
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.