Godot.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*************************************************************************/
  2. /* Godot.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_HPP
  31. #define GODOT_HPP
  32. #include <cstdlib>
  33. #include <cstring>
  34. #include <gdnative_api_struct.gen.h>
  35. #include <nativescript/godot_nativescript.h>
  36. #include <typeinfo>
  37. #include "CoreTypes.hpp"
  38. #include "Ref.hpp"
  39. #include "TagDB.hpp"
  40. #include "Variant.hpp"
  41. #include "Object.hpp"
  42. #include "GodotGlobal.hpp"
  43. namespace godot {
  44. namespace detail {
  45. // Godot classes are wrapped by heap-allocated instances mimicking them through the C API.
  46. // They all inherit `_Wrapped`.
  47. template <class T>
  48. T *get_wrapper(godot_object *obj) {
  49. return (T *)godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, obj);
  50. }
  51. // Custom class instances are not obtainable by just casting the pointer to the base class they inherit,
  52. // partly because in Godot, scripts are not instances of the classes themselves, they are only attached to them.
  53. // Yet we want to "fake" it as if they were the same entity.
  54. template <class T>
  55. T *get_custom_class_instance(const Object *obj) {
  56. return (obj) ? (T *)godot::nativescript_api->godot_nativescript_get_userdata(obj->_owner) : nullptr;
  57. }
  58. template <class T>
  59. inline T *create_custom_class_instance() {
  60. // Usually, script instances hold a reference to their NativeScript resource.
  61. // that resource is obtained from a `.gdns` file, which in turn exists because
  62. // of the resource system of Godot. We can't cleanly hardcode that here,
  63. // so the easiest for now (though not really clean) is to create new resource instances,
  64. // individually attached to the script instances.
  65. // We cannot use wrappers because of https://github.com/godotengine/godot/issues/39181
  66. // godot::NativeScript *script = godot::NativeScript::_new();
  67. // script->set_library(get_wrapper<godot::GDNativeLibrary>((godot_object *)godot::gdnlib));
  68. // script->set_class_name(T::___get_class_name());
  69. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  70. // So we use the C API directly.
  71. static godot_class_constructor script_constructor = godot::api->godot_get_class_constructor("NativeScript");
  72. static godot_method_bind *mb_set_library = godot::api->godot_method_bind_get_method("NativeScript", "set_library");
  73. static godot_method_bind *mb_set_class_name = godot::api->godot_method_bind_get_method("NativeScript", "set_class_name");
  74. godot_object *script = script_constructor();
  75. {
  76. const void *args[] = { godot::gdnlib };
  77. godot::api->godot_method_bind_ptrcall(mb_set_library, script, args, nullptr);
  78. }
  79. {
  80. const String class_name = T::___get_class_name();
  81. const void *args[] = { &class_name };
  82. godot::api->godot_method_bind_ptrcall(mb_set_class_name, script, args, nullptr);
  83. }
  84. // Now to instanciate T, we initially did this, however in case of Reference it returns a variant with refcount
  85. // already initialized, which woud cause inconsistent behavior compared to other classes (we still have to return a pointer).
  86. //Variant instance_variant = script->new_();
  87. //T *instance = godot::get_custom_class_instance<T>(instance_variant);
  88. // So we should do this instead, however while convenient, it uses unnecessary wrapper objects.
  89. // Object *base_obj = T::___new_godot_base();
  90. // base_obj->set_script(script);
  91. // return get_custom_class_instance<T>(base_obj);
  92. // Again using the C API to do exactly what we have to do.
  93. static godot_class_constructor base_constructor = godot::api->godot_get_class_constructor(T::___get_godot_class_name());
  94. static godot_method_bind *mb_set_script = godot::api->godot_method_bind_get_method("Object", "set_script");
  95. godot_object *base_obj = base_constructor();
  96. {
  97. const void *args[] = { script };
  98. godot::api->godot_method_bind_ptrcall(mb_set_script, base_obj, args, nullptr);
  99. }
  100. return (T *)godot::nativescript_api->godot_nativescript_get_userdata(base_obj);
  101. }
  102. } // namespace detail
  103. // Used in the definition of a custom class.
  104. //
  105. // Name: Name of your class, without namespace
  106. // Base: Name of the direct base class, with namespace if necessary
  107. //
  108. // ___get_class_name: Name of the class
  109. // ___get_godot_class_name: Name of the Godot base class this class inherits from (i.e not direct)
  110. // _new: Creates a new instance of the class
  111. // ___get_id: Gets the unique ID of the class. Godot and custom classes are both within that set.
  112. // ___get_base_id: Gets the ID of the direct base class, as returned by ___get_id
  113. // ___get_base_class_name: Name of the direct base class
  114. // ___get_from_variant: Converts a Variant into an Object*. Will be non-null if the class matches.
  115. #define GODOT_CLASS(Name, Base) \
  116. \
  117. public: \
  118. inline static const char *___get_class_name() { return #Name; } \
  119. enum { ___CLASS_IS_SCRIPT = 1 }; \
  120. inline static const char *___get_godot_class_name() { \
  121. return Base::___get_godot_class_name(); \
  122. } \
  123. inline static Name *_new() { \
  124. return godot::detail::create_custom_class_instance<Name>(); \
  125. } \
  126. inline static size_t ___get_id() { return typeid(Name).hash_code(); } \
  127. inline static size_t ___get_base_id() { return Base::___get_id(); } \
  128. inline static const char *___get_base_class_name() { return Base::___get_class_name(); } \
  129. inline static godot::Object *___get_from_variant(godot::Variant a) { \
  130. return (godot::Object *)godot::detail::get_custom_class_instance<Name>( \
  131. godot::Object::___get_from_variant(a)); \
  132. } \
  133. \
  134. private:
  135. // Legacy compatibility
  136. #define GODOT_SUBCLASS(Name, Base) GODOT_CLASS(Name, Base)
  137. template <class T>
  138. struct _ArgCast {
  139. static T _arg_cast(Variant a) {
  140. return a;
  141. }
  142. };
  143. template <class T>
  144. struct _ArgCast<T *> {
  145. static T *_arg_cast(Variant a) {
  146. return (T *)T::___get_from_variant(a);
  147. }
  148. };
  149. template <>
  150. struct _ArgCast<Variant> {
  151. static Variant _arg_cast(Variant a) {
  152. return a;
  153. }
  154. };
  155. // instance and destroy funcs
  156. template <class T>
  157. void *_godot_class_instance_func(godot_object *p, void * /*method_data*/) {
  158. T *d = new T();
  159. d->_owner = p;
  160. d->_type_tag = typeid(T).hash_code();
  161. d->_init();
  162. return d;
  163. }
  164. template <class T>
  165. void _godot_class_destroy_func(godot_object * /*p*/, void * /*method_data*/, void *data) {
  166. T *d = (T *)data;
  167. delete d;
  168. }
  169. template <class T>
  170. void register_class() {
  171. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  172. godot_instance_create_func create = {};
  173. create.create_func = _godot_class_instance_func<T>;
  174. godot_instance_destroy_func destroy = {};
  175. destroy.destroy_func = _godot_class_destroy_func<T>;
  176. _TagDB::register_type(T::___get_id(), T::___get_base_id());
  177. godot::nativescript_api->godot_nativescript_register_class(godot::_RegisterState::nativescript_handle,
  178. T::___get_class_name(), T::___get_base_class_name(), create, destroy);
  179. godot::nativescript_1_1_api->godot_nativescript_set_type_tag(godot::_RegisterState::nativescript_handle,
  180. T::___get_class_name(), (const void *)T::___get_id());
  181. T::_register_methods();
  182. }
  183. template <class T>
  184. void register_tool_class() {
  185. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  186. godot_instance_create_func create = {};
  187. create.create_func = _godot_class_instance_func<T>;
  188. godot_instance_destroy_func destroy = {};
  189. destroy.destroy_func = _godot_class_destroy_func<T>;
  190. _TagDB::register_type(T::___get_id(), T::___get_base_id());
  191. godot::nativescript_api->godot_nativescript_register_tool_class(godot::_RegisterState::nativescript_handle,
  192. T::___get_class_name(), T::___get_base_class_name(), create, destroy);
  193. godot::nativescript_1_1_api->godot_nativescript_set_type_tag(godot::_RegisterState::nativescript_handle,
  194. T::___get_class_name(), (const void *)T::___get_id());
  195. T::_register_methods();
  196. }
  197. // method registering
  198. typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
  199. template <class T, class R, class... args>
  200. const char *___get_method_class_name(R (T::*p)(args... a)) {
  201. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  202. (void)p; // To avoid "unused parameter" warnings. `p` is required for template matching.
  203. return T::___get_class_name();
  204. }
  205. // This second version is also required to match constant functions
  206. template <class T, class R, class... args>
  207. const char *___get_method_class_name(R (T::*p)(args... a) const) {
  208. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  209. (void)p; // To avoid "unused parameter" warnings. `p` is required for template matching.
  210. return T::___get_class_name();
  211. }
  212. // Okay, time for some template magic.
  213. // Many thanks to manpat from the GDL Discord Server.
  214. // This is stuff that's available in C++14 I think, but whatever.
  215. template <int... I>
  216. struct __Sequence {};
  217. template <int N, int... I>
  218. struct __construct_sequence {
  219. using type = typename __construct_sequence<N - 1, N - 1, I...>::type;
  220. };
  221. template <int... I>
  222. struct __construct_sequence<0, I...> {
  223. using type = __Sequence<I...>;
  224. };
  225. // Now the wrapping part.
  226. template <class T, class R, class... As>
  227. struct _WrappedMethod {
  228. R(T::*f)
  229. (As...);
  230. template <int... I>
  231. void apply(Variant *ret, T *obj, Variant **args, __Sequence<I...>) {
  232. *ret = (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
  233. }
  234. };
  235. template <class T, class... As>
  236. struct _WrappedMethod<T, void, As...> {
  237. void (T::*f)(As...);
  238. template <int... I>
  239. void apply(Variant * /*ret*/, T *obj, Variant **args, __Sequence<I...>) {
  240. (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
  241. }
  242. };
  243. template <class T, class R, class... As>
  244. godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int /*num_args*/, godot_variant **args) {
  245. godot_variant v;
  246. godot::api->godot_variant_new_nil(&v);
  247. T *obj = (T *)user_data;
  248. _WrappedMethod<T, R, As...> *method = (_WrappedMethod<T, R, As...> *)method_data;
  249. Variant *var = (Variant *)&v;
  250. Variant **arg = (Variant **)args;
  251. method->apply(var, obj, arg, typename __construct_sequence<sizeof...(As)>::type{});
  252. return v;
  253. }
  254. template <class T, class R, class... As>
  255. void *___make_wrapper_function(R (T::*f)(As...)) {
  256. using MethodType = _WrappedMethod<T, R, As...>;
  257. MethodType *p = (MethodType *)godot::api->godot_alloc(sizeof(MethodType));
  258. p->f = f;
  259. return (void *)p;
  260. }
  261. template <class T, class R, class... As>
  262. __godot_wrapper_method ___get_wrapper_function(R (T::* /*f*/)(As...)) {
  263. return (__godot_wrapper_method)&__wrapped_method<T, R, As...>;
  264. }
  265. template <class T, class R, class... A>
  266. void *___make_wrapper_function(R (T::*f)(A...) const) {
  267. return ___make_wrapper_function((R(T::*)(A...))f);
  268. }
  269. template <class T, class R, class... A>
  270. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A...) const) {
  271. return ___get_wrapper_function((R(T::*)(A...))f);
  272. }
  273. template <class M>
  274. void register_method(const char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
  275. godot_instance_method method = {};
  276. method.method_data = ___make_wrapper_function(method_ptr);
  277. method.free_func = godot::api->godot_free;
  278. method.method = (__godot_wrapper_method)___get_wrapper_function(method_ptr);
  279. godot_method_attributes attr = {};
  280. attr.rpc_type = rpc_type;
  281. godot::nativescript_api->godot_nativescript_register_method(godot::_RegisterState::nativescript_handle,
  282. ___get_method_class_name(method_ptr), name, attr, method);
  283. }
  284. // User can specify a derived class D to register the method for, instead of it being inferred.
  285. template <class D, class B, class R, class... As>
  286. void register_method_explicit(const char *name, R (B::*method_ptr)(As...),
  287. godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
  288. static_assert(std::is_base_of<B, D>::value, "Explicit class must derive from method class");
  289. register_method(name, static_cast<R (D::*)(As...)>(method_ptr), rpc_type);
  290. }
  291. template <class T, class P>
  292. struct _PropertySetFunc {
  293. void (T::*f)(P);
  294. static void _wrapped_setter(godot_object * /*object*/, void *method_data, void *user_data, godot_variant *value) {
  295. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *)method_data;
  296. T *obj = (T *)user_data;
  297. Variant *v = (Variant *)value;
  298. (obj->*(set_func->f))(_ArgCast<P>::_arg_cast(*v));
  299. }
  300. };
  301. template <class T, class P>
  302. struct _PropertyGetFunc {
  303. P(T::*f)
  304. ();
  305. static godot_variant _wrapped_getter(godot_object * /*object*/, void *method_data, void *user_data) {
  306. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *)method_data;
  307. T *obj = (T *)user_data;
  308. godot_variant var;
  309. godot::api->godot_variant_new_nil(&var);
  310. Variant *v = (Variant *)&var;
  311. *v = (obj->*(get_func->f))();
  312. return var;
  313. }
  314. };
  315. template <class T, class P>
  316. struct _PropertyDefaultSetFunc {
  317. P(T::*f);
  318. static void _wrapped_setter(godot_object * /*object*/, void *method_data, void *user_data, godot_variant *value) {
  319. _PropertyDefaultSetFunc<T, P> *set_func = (_PropertyDefaultSetFunc<T, P> *)method_data;
  320. T *obj = (T *)user_data;
  321. Variant *v = (Variant *)value;
  322. (obj->*(set_func->f)) = _ArgCast<P>::_arg_cast(*v);
  323. }
  324. };
  325. template <class T, class P>
  326. struct _PropertyDefaultGetFunc {
  327. P(T::*f);
  328. static godot_variant _wrapped_getter(godot_object * /*object*/, void *method_data, void *user_data) {
  329. _PropertyDefaultGetFunc<T, P> *get_func = (_PropertyDefaultGetFunc<T, P> *)method_data;
  330. T *obj = (T *)user_data;
  331. godot_variant var;
  332. godot::api->godot_variant_new_nil(&var);
  333. Variant *v = (Variant *)&var;
  334. *v = (obj->*(get_func->f));
  335. return var;
  336. }
  337. };
  338. template <class T, class P>
  339. void register_property(const char *name, P(T::*var), P default_value,
  340. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  341. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  342. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  343. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  344. Variant def_val = default_value;
  345. usage = (godot_property_usage_flags)((int)usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE);
  346. if (def_val.get_type() == Variant::OBJECT) {
  347. Object *o = detail::get_wrapper<Object>(def_val.operator godot_object *());
  348. if (o && o->is_class("Resource")) {
  349. hint = (godot_property_hint)((int)hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE);
  350. hint_string = o->get_class();
  351. }
  352. }
  353. godot_string *_hint_string = (godot_string *)&hint_string;
  354. godot_property_attributes attr = {};
  355. if (def_val.get_type() == Variant::NIL) {
  356. attr.type = Variant::OBJECT;
  357. } else {
  358. attr.type = def_val.get_type();
  359. attr.default_value = *(godot_variant *)&def_val;
  360. }
  361. attr.hint = hint;
  362. attr.rset_type = rpc_mode;
  363. attr.usage = usage;
  364. attr.hint_string = *_hint_string;
  365. _PropertyDefaultSetFunc<T, P> *wrapped_set =
  366. (_PropertyDefaultSetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultSetFunc<T, P>));
  367. wrapped_set->f = var;
  368. _PropertyDefaultGetFunc<T, P> *wrapped_get =
  369. (_PropertyDefaultGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultGetFunc<T, P>));
  370. wrapped_get->f = var;
  371. godot_property_set_func set_func = {};
  372. set_func.method_data = (void *)wrapped_set;
  373. set_func.free_func = godot::api->godot_free;
  374. set_func.set_func = &_PropertyDefaultSetFunc<T, P>::_wrapped_setter;
  375. godot_property_get_func get_func = {};
  376. get_func.method_data = (void *)wrapped_get;
  377. get_func.free_func = godot::api->godot_free;
  378. get_func.get_func = &_PropertyDefaultGetFunc<T, P>::_wrapped_getter;
  379. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle,
  380. T::___get_class_name(), name, &attr, set_func, get_func);
  381. }
  382. template <class T, class P>
  383. void register_property(const char *name, void (T::*setter)(P), P (T::*getter)(), P default_value,
  384. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  385. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  386. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  387. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  388. Variant def_val = default_value;
  389. godot_string *_hint_string = (godot_string *)&hint_string;
  390. godot_property_attributes attr = {};
  391. if (def_val.get_type() == Variant::NIL) {
  392. attr.type = Variant::OBJECT;
  393. } else {
  394. attr.type = def_val.get_type();
  395. attr.default_value = *(godot_variant *)&def_val;
  396. }
  397. attr.hint = hint;
  398. attr.rset_type = rpc_mode;
  399. attr.usage = usage;
  400. attr.hint_string = *_hint_string;
  401. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertySetFunc<T, P>));
  402. wrapped_set->f = setter;
  403. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyGetFunc<T, P>));
  404. wrapped_get->f = getter;
  405. godot_property_set_func set_func = {};
  406. set_func.method_data = (void *)wrapped_set;
  407. set_func.free_func = godot::api->godot_free;
  408. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  409. godot_property_get_func get_func = {};
  410. get_func.method_data = (void *)wrapped_get;
  411. get_func.free_func = godot::api->godot_free;
  412. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  413. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle,
  414. T::___get_class_name(), name, &attr, set_func, get_func);
  415. }
  416. template <class T, class P>
  417. void register_property(const char *name, void (T::*setter)(P), P (T::*getter)() const, P default_value,
  418. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  419. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  420. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  421. register_property(name, setter, (P(T::*)())getter, default_value, rpc_mode, usage, hint, hint_string);
  422. }
  423. template <class T>
  424. void register_signal(String name, Dictionary args = Dictionary()) {
  425. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  426. godot_signal signal = {};
  427. signal.name = *(godot_string *)&name;
  428. signal.num_args = args.size();
  429. signal.num_default_args = 0;
  430. // Need to check because malloc(0) is platform-dependent. Zero arguments will leave args to nullptr.
  431. if (signal.num_args != 0) {
  432. signal.args = (godot_signal_argument *)godot::api->godot_alloc(sizeof(godot_signal_argument) * signal.num_args);
  433. memset((void *)signal.args, 0, sizeof(godot_signal_argument) * signal.num_args);
  434. }
  435. for (int i = 0; i < signal.num_args; i++) {
  436. // Array entry = args[i];
  437. // String name = entry[0];
  438. String name = args.keys()[i];
  439. godot_string *_key = (godot_string *)&name;
  440. godot::api->godot_string_new_copy(&signal.args[i].name, _key);
  441. // if (entry.size() > 1) {
  442. // signal.args[i].type = entry[1];
  443. // }
  444. signal.args[i].type = args.values()[i];
  445. }
  446. godot::nativescript_api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle,
  447. T::___get_class_name(), &signal);
  448. for (int i = 0; i < signal.num_args; i++) {
  449. godot::api->godot_string_destroy(&signal.args[i].name);
  450. }
  451. if (signal.args) {
  452. godot::api->godot_free(signal.args);
  453. }
  454. }
  455. template <class T, class... Args>
  456. void register_signal(String name, Args... varargs) {
  457. register_signal<T>(name, Dictionary::make(varargs...));
  458. }
  459. #ifndef GODOT_CPP_NO_OBJECT_CAST
  460. template <class T>
  461. T *Object::cast_to(const Object *obj) {
  462. if (!obj)
  463. return nullptr;
  464. if (T::___CLASS_IS_SCRIPT) {
  465. size_t have_tag = (size_t)godot::nativescript_1_1_api->godot_nativescript_get_type_tag(obj->_owner);
  466. if (have_tag) {
  467. if (!godot::_TagDB::is_type_known((size_t)have_tag)) {
  468. have_tag = 0;
  469. }
  470. }
  471. if (!have_tag) {
  472. have_tag = obj->_type_tag;
  473. }
  474. if (godot::_TagDB::is_type_compatible(T::___get_id(), have_tag)) {
  475. return detail::get_custom_class_instance<T>(obj);
  476. }
  477. } else {
  478. if (godot::core_1_2_api->godot_object_cast_to(obj->_owner, (void *)T::___get_id())) {
  479. return (T *)obj;
  480. }
  481. }
  482. return nullptr;
  483. }
  484. #endif
  485. } // namespace godot
  486. #endif // GODOT_HPP