Merge pull request #71000 from reduz/callable-bind-from-array

Allow binding Callable arguments from an array
This commit is contained in:
Rémi Verschelde 2023-01-07 13:18:53 +01:00
commit 41b74c675f
No known key found for this signature in database
GPG key ID: C3336907360768E1
4 changed files with 23 additions and 0 deletions

View file

@ -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)));
}

View file

@ -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;

View file

@ -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);

View file

@ -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>