create Variant Dictionary method map() similar to Variant Array method map() #7973
Efimero
started this conversation in
Engine Core
Replies: 1 comment
-
With some help from a friend, we figured this might be the code that would be added to Dictionary.cpp Variant Dictionary::map(const Callable &p_callable) const {
Dictionary n;
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
const Variant **argptrs = (const Variant **)alloca(2 * sizeof(Variant *));
argptrs[0] = &E.key;
argptrs[1] = &E.value;
Variant result;
Callable::CallError ce;
p_callable.call(argptrs, 2, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(Array(), "Error calling method from 'map': " + Variant::get_callable_error_text(p_callable, argptrs, 2, ce));
}
n[E.key] = result;
}
return n;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I believe it would be very useful to have a Dictionary::map() method in GDScript similar to what Array has.
Array::map()
It would work in a similar fashion, but the lambda provided would have two parameters, for the key and the value, and would return a new value for that key. This would enable better, more readable code in cases where you want to (immutably) alter a dictionary preserving the keys.
For example:
Beta Was this translation helpful? Give feedback.
All reactions