diff options
author | Juan Linietsky <reduzio@gmail.com> | 2023-01-06 16:35:42 +0100 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2023-01-06 22:37:25 +0100 |
commit | d762a0395a27d523d73066708d0d8f0e9077f5fd (patch) | |
tree | 5b7618ecf6266299c80b0c004e0d569b012ea721 | |
parent | b14f7aa9f92ff44135c283a9c88dab5ef9136d64 (diff) |
Allow binding Callable arguments from an array
Restores 3.x functionality that was removed in the Signal/Callable refactor of 4.0.
Fixes #64668.
Implements https://github.com/godotengine/godot-proposals/issues/6034
Usage:
```GDScript
callable.bindv([arg1,arg2,arg3])
```
-rw-r--r-- | core/variant/callable.cpp | 14 | ||||
-rw-r--r-- | core/variant/callable.h | 1 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 1 | ||||
-rw-r--r-- | doc/classes/Callable.xml | 7 |
4 files changed, 23 insertions, 0 deletions
diff --git a/core/variant/callable.cpp b/core/variant/callable.cpp index ba3fc536d7..60549d567e 100644 --- a/core/variant/callable.cpp +++ b/core/variant/callable.cpp @@ -102,6 +102,20 @@ Callable Callable::bindp(const Variant **p_arguments, int p_argcount) const { } return Callable(memnew(CallableCustomBind(*this, args))); } + +Callable Callable::bindv(const Array &p_arguments) { + if (p_arguments.is_empty()) { + return *this; // No point in creating a new callable if nothing is bound. + } + + Vector<Variant> args; + args.resize(p_arguments.size()); + for (int i = 0; i < p_arguments.size(); i++) { + args.write[i] = p_arguments[i]; + } + return Callable(memnew(CallableCustomBind(*this, args))); +} + Callable Callable::unbind(int p_argcount) const { return Callable(memnew(CallableCustomUnbind(*this, p_argcount))); } diff --git a/core/variant/callable.h b/core/variant/callable.h index c9d32afb61..43fef47ec0 100644 --- a/core/variant/callable.h +++ b/core/variant/callable.h @@ -98,6 +98,7 @@ public: template <typename... VarArgs> Callable bind(VarArgs... p_args); + Callable bindv(const Array &p_arguments); Callable bindp(const Variant **p_arguments, int p_argcount) const; Callable unbind(int p_argcount) const; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 088e24ba6e..2182c988fb 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -2017,6 +2017,7 @@ static void _register_variant_builtin_methods() { bind_method(Callable, get_object_id, sarray(), varray()); bind_method(Callable, get_method, sarray(), varray()); bind_method(Callable, hash, sarray(), varray()); + bind_method(Callable, bindv, sarray("arguments"), varray()); bind_method(Callable, unbind, sarray("argcount"), varray()); bind_custom(Callable, call, _VariantCall::func_Callable_call, true, Variant); diff --git a/doc/classes/Callable.xml b/doc/classes/Callable.xml index 1fb4f91920..5c845ba57e 100644 --- a/doc/classes/Callable.xml +++ b/doc/classes/Callable.xml @@ -77,6 +77,13 @@ Returns a copy of this [Callable] with one or more arguments bound. When called, the bound arguments are passed [i]after[/i] the arguments supplied by [method call]. </description> </method> + <method name="bindv"> + <return type="Callable" /> + <param index="0" name="arguments" type="Array" /> + <description> + Returns a copy of this [Callable] with one or more arguments bound, reading them from an array. When called, the bound arguments are passed [i]after[/i] the arguments supplied by [method call]. + </description> + </method> <method name="call" qualifiers="vararg const"> <return type="Variant" /> <description> |