godot-cpp-utils/util_functions.hpp

25 lines
678 B
C++

#ifndef UTILS_FUNCTIONS_HPP
#define UTILS_FUNCTIONS_HPP
#include <godot_cpp/variant/dictionary.hpp>
#include <godot_cpp/templates/hash_map.hpp>
template<typename T, typename U>
gd::Dictionary hashmap_to_dictionary(gd::HashMap<T, U> const &map) {
gd::Dictionary dict{};
for(gd::KeyValue<T, U> const &kvp : map)
dict[kvp.key] = kvp.value;
return dict;
}
template<typename T, typename U>
gd::HashMap<T, U> dictionary_to_hashmap(gd::Dictionary const &dict) {
gd::HashMap<T, U> map{};
gd::Array keys{dict.keys()};
for(size_t i = 0; i < keys.size(); ++i)
map.insert(keys[i], dict[keys[i]]);
return map;
}
#endif // !UTILS_FUNCTIONS_HPP