Created
March 5, 2018 20:02
-
-
Save danielytics/41b054b260432b2e641c37c501154b47 to your computer and use it in GitHub Desktop.
Helper template functions for registering and emitting Godot signals with arguments from GDNative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HELPERS_H | |
#define HELPERS_H | |
#include <core/Godot.hpp> | |
#include <core/Dictionary.hpp> | |
#include <core/String.hpp> | |
#include <Node.hpp> | |
namespace helpers { | |
namespace impl { | |
template <typename KV, typename KeyT, typename ValueT> | |
void append_kv (KV& kv, KeyT key, ValueT value) { | |
kv[key] = value; | |
} | |
template <typename KV, typename KeyT, typename ValueT, typename... Args> | |
void append_kv (KV& kv, KeyT key, ValueT value, Args... args) { | |
kv[key] = value; | |
append_kv(kv, args...); | |
} | |
template <typename T, typename ValueT> | |
void append (T& appendable, ValueT value) { | |
appendable.append(value); | |
} | |
template <typename T, typename ValueT, typename... Args> | |
void append (T& appendable, ValueT value, Args... args) { | |
appendable.append(value); | |
append(appendable, args...); | |
} | |
} | |
// Registering signals | |
template <typename T, typename... Args> | |
void register_signal (godot::String name, Args... args) { | |
godot::Dictionary dict; | |
impl::append_kv(dict, args...); | |
godot::register_signal<T>(name, dict); | |
} | |
template <typename T> | |
void register_signal (godot::String name) { | |
godot::register_signal<T>(name); | |
} | |
// Emitting signals | |
template <typename... Args> | |
void emit_signal(godot::Node* owner, godot::String name, Args... args) { | |
godot::Array array; | |
impl::append(array, args...); | |
owner->emit_signal(name, array); | |
} | |
void emit_signal(godot::Node* owner, godot::String name) { | |
owner->emit_signal(name); | |
} | |
} // namespace helpers | |
#endif // HELPERS_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment