Struct Callable
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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.
public readonly struct Callable
- Inherited Members
Examples
public void PrintArgs(object ar1, object arg2, object arg3 = null)
{
GD.PrintS(arg1, arg2, arg3);
}
public void Test()
{
// This Callable object will call the PrintArgs method defined above.
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(GodotObject, StringName)
Constructs a new Callable for the method called method
in the specified target.
public Callable(GodotObject target, StringName method)
Parameters
targetGodotObjectObject that contains the method.
methodStringNameName of the method that will be called.
Properties
Delegate
Delegate of the method that will be called.
public Delegate Delegate { get; }
Property Value
Method
Name of the method that will be called.
public StringName Method { get; }
Property Value
Target
Object that contains the method.
public GodotObject Target { get; }
Property Value
Trampoline
Trampoline function pointer for dynamically invoking Delegate.
public delegate*<object, NativeVariantPtrArgs, out godot_variant, void> Trampoline { get; }
Property Value
- delegate*<object, NativeVariantPtrArgs, out godot_variant, void>
Methods
Call(params Variant[])
Calls the method represented by this Callable. Arguments can be passed and should match the method's signature.
public Variant Call(params Variant[] args)
Parameters
argsVariant[]Arguments that will be passed to the method call.
Returns
- Variant
The value returned by the method.
CallDeferred(params Variant[])
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.
public void CallDeferred(params Variant[] args)
Parameters
argsVariant[]Arguments that will be passed to the method call.
CreateWithUnsafeTrampoline(Delegate, delegate*<object, NativeVariantPtrArgs, out godot_variant, void>)
Constructs a new Callable using the trampoline
function pointer to dynamically invoke the given delegate.
The parameters passed to the trampoline function are:
- delegateObjThe given
delegate, upcast to object. - argsArray of godot_variant arguments.
- retReturn value of type godot_variant.
The delegate should be downcast to a more specific delegate type before invoking.
public static Callable CreateWithUnsafeTrampoline(Delegate @delegate, delegate*<object, NativeVariantPtrArgs, out godot_variant, void> trampoline)
Parameters
delegateDelegateDelegate method that will be called.
trampolinedelegate*<object, NativeVariantPtrArgs, out godot_variant, void>Trampoline function pointer for invoking the delegate.
Returns
Examples
Usage example:
static void Trampoline(object delegateObj, NativeVariantPtrArgs args, out godot_variant ret)
{
if (args.Count != 1)
throw new ArgumentException($"Callable expected {1} arguments but received {args.Count}.");
TResult res = ((Func<int, string>)delegateObj)(
VariantConversionCallbacks.GetToManagedCallback<int>()(args[0])
);
ret = VariantConversionCallbacks.GetToVariantCallback<string>()(res);
}
var callable = Callable.CreateWithUnsafeTrampoline((int num) => "foo" + num.ToString(), &Trampoline);
var res = (string)callable.Call(10);
Console.WriteLine(res);
From(Action)
Constructs a new Callable for the given action.
public static Callable From(Action action)
Parameters
actionActionAction method that will be called.
Returns
From<T0>(Action<T0>)
Constructs a new Callable for the given action.
public static Callable From<T0>(Action<T0> action)
Parameters
actionAction<T0>Action method that will be called.
Returns
Type Parameters
T0
From<TResult>(Func<TResult>)
Constructs a new Callable for the given func.
public static Callable From<TResult>(Func<TResult> func)
Parameters
funcFunc<TResult>Action method that will be called.
Returns
Type Parameters
TResult
From<T0, T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Func<T0, T1, T2, T3, T4, T5, T6, T7, T8, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Func<T0, T1, T2, T3, T4, T5, T6, T7, T8, TResult> func)
Parameters
funcFunc<T0, T1, T2, T3, T4, T5, T6, T7, T8, TResult>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4T5T6T7T8TResult
From<T0, T1>(Action<T0, T1>)
Constructs a new Callable for the given action.
public static Callable From<T0, T1>(Action<T0, T1> action)
Parameters
actionAction<T0, T1>Action method that will be called.
Returns
Type Parameters
T0T1
From<T0, TResult>(Func<T0, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, TResult>(Func<T0, TResult> func)
Parameters
funcFunc<T0, TResult>Action method that will be called.
Returns
Type Parameters
T0TResult
From<T0, T1, T2>(Action<T0, T1, T2>)
Constructs a new Callable for the given action.
public static Callable From<T0, T1, T2>(Action<T0, T1, T2> action)
Parameters
actionAction<T0, T1, T2>Action method that will be called.
Returns
Type Parameters
T0T1T2
From<T0, T1, TResult>(Func<T0, T1, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, T1, TResult>(Func<T0, T1, TResult> func)
Parameters
funcFunc<T0, T1, TResult>Action method that will be called.
Returns
Type Parameters
T0T1TResult
From<T0, T1, T2, T3>(Action<T0, T1, T2, T3>)
Constructs a new Callable for the given action.
public static Callable From<T0, T1, T2, T3>(Action<T0, T1, T2, T3> action)
Parameters
actionAction<T0, T1, T2, T3>Action method that will be called.
Returns
Type Parameters
T0T1T2T3
From<T0, T1, T2, TResult>(Func<T0, T1, T2, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, T1, T2, TResult>(Func<T0, T1, T2, TResult> func)
Parameters
funcFunc<T0, T1, T2, TResult>Action method that will be called.
Returns
Type Parameters
T0T1T2TResult
From<T0, T1, T2, T3, T4>(Action<T0, T1, T2, T3, T4>)
Constructs a new Callable for the given action.
public static Callable From<T0, T1, T2, T3, T4>(Action<T0, T1, T2, T3, T4> action)
Parameters
actionAction<T0, T1, T2, T3, T4>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4
From<T0, T1, T2, T3, TResult>(Func<T0, T1, T2, T3, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, T1, T2, T3, TResult>(Func<T0, T1, T2, T3, TResult> func)
Parameters
funcFunc<T0, T1, T2, T3, TResult>Action method that will be called.
Returns
Type Parameters
T0T1T2T3TResult
From<T0, T1, T2, T3, T4, T5>(Action<T0, T1, T2, T3, T4, T5>)
Constructs a new Callable for the given action.
public static Callable From<T0, T1, T2, T3, T4, T5>(Action<T0, T1, T2, T3, T4, T5> action)
Parameters
actionAction<T0, T1, T2, T3, T4, T5>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4T5
From<T0, T1, T2, T3, T4, TResult>(Func<T0, T1, T2, T3, T4, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, T1, T2, T3, T4, TResult>(Func<T0, T1, T2, T3, T4, TResult> func)
Parameters
funcFunc<T0, T1, T2, T3, T4, TResult>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4TResult
From<T0, T1, T2, T3, T4, T5, T6>(Action<T0, T1, T2, T3, T4, T5, T6>)
Constructs a new Callable for the given action.
public static Callable From<T0, T1, T2, T3, T4, T5, T6>(Action<T0, T1, T2, T3, T4, T5, T6> action)
Parameters
actionAction<T0, T1, T2, T3, T4, T5, T6>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4T5T6
From<T0, T1, T2, T3, T4, T5, TResult>(Func<T0, T1, T2, T3, T4, T5, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, T1, T2, T3, T4, T5, TResult>(Func<T0, T1, T2, T3, T4, T5, TResult> func)
Parameters
funcFunc<T0, T1, T2, T3, T4, T5, TResult>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4T5TResult
From<T0, T1, T2, T3, T4, T5, T6, T7>(Action<T0, T1, T2, T3, T4, T5, T6, T7>)
Constructs a new Callable for the given action.
public static Callable From<T0, T1, T2, T3, T4, T5, T6, T7>(Action<T0, T1, T2, T3, T4, T5, T6, T7> action)
Parameters
actionAction<T0, T1, T2, T3, T4, T5, T6, T7>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4T5T6T7
From<T0, T1, T2, T3, T4, T5, T6, TResult>(Func<T0, T1, T2, T3, T4, T5, T6, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, T1, T2, T3, T4, T5, T6, TResult>(Func<T0, T1, T2, T3, T4, T5, T6, TResult> func)
Parameters
funcFunc<T0, T1, T2, T3, T4, T5, T6, TResult>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4T5T6TResult
From<T0, T1, T2, T3, T4, T5, T6, T7, T8>(Action<T0, T1, T2, T3, T4, T5, T6, T7, T8>)
Constructs a new Callable for the given action.
public static Callable From<T0, T1, T2, T3, T4, T5, T6, T7, T8>(Action<T0, T1, T2, T3, T4, T5, T6, T7, T8> action)
Parameters
actionAction<T0, T1, T2, T3, T4, T5, T6, T7, T8>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4T5T6T7T8
From<T0, T1, T2, T3, T4, T5, T6, T7, TResult>(Func<T0, T1, T2, T3, T4, T5, T6, T7, TResult>)
Constructs a new Callable for the given func.
public static Callable From<T0, T1, T2, T3, T4, T5, T6, T7, TResult>(Func<T0, T1, T2, T3, T4, T5, T6, T7, TResult> func)
Parameters
funcFunc<T0, T1, T2, T3, T4, T5, T6, T7, TResult>Action method that will be called.
Returns
Type Parameters
T0T1T2T3T4T5T6T7TResult