Godot.hpp 20 KB

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