Godot.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. return T::___get_class_name();
  174. }
  175. template <class T, class R, class... args>
  176. const char *___get_method_class_name(R (T::*p)(args... a) const) {
  177. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  178. return T::___get_class_name();
  179. }
  180. // Okay, time for some template magic.
  181. // Many thanks to manpat from the GDL Discord Server.
  182. // This is stuff that's available in C++14 I think, but whatever.
  183. template <int... I>
  184. struct __Sequence {};
  185. template <int N, int... I>
  186. struct __construct_sequence {
  187. using type = typename __construct_sequence<N - 1, N - 1, I...>::type;
  188. };
  189. template <int... I>
  190. struct __construct_sequence<0, I...> {
  191. using type = __Sequence<I...>;
  192. };
  193. // Now the wrapping part.
  194. template <class T, class R, class... As>
  195. struct _WrappedMethod {
  196. R(T::*f)
  197. (As...);
  198. template <int... I>
  199. void apply(Variant *ret, T *obj, Variant **args, __Sequence<I...>) {
  200. *ret = (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
  201. }
  202. };
  203. template <class T, class... As>
  204. struct _WrappedMethod<T, void, As...> {
  205. void (T::*f)(As...);
  206. template <int... I>
  207. void apply(Variant *ret, T *obj, Variant **args, __Sequence<I...>) {
  208. (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
  209. }
  210. };
  211. template <class T, class R, class... As>
  212. godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) {
  213. godot_variant v;
  214. godot::api->godot_variant_new_nil(&v);
  215. T *obj = (T *)user_data;
  216. _WrappedMethod<T, R, As...> *method = (_WrappedMethod<T, R, As...> *)method_data;
  217. Variant *var = (Variant *)&v;
  218. Variant **arg = (Variant **)args;
  219. method->apply(var, obj, arg, typename __construct_sequence<sizeof...(As)>::type{});
  220. return v;
  221. }
  222. template <class T, class R, class... As>
  223. void *___make_wrapper_function(R (T::*f)(As...)) {
  224. using MethodType = _WrappedMethod<T, R, As...>;
  225. MethodType *p = (MethodType *)godot::api->godot_alloc(sizeof(MethodType));
  226. p->f = f;
  227. return (void *)p;
  228. }
  229. template <class T, class R, class... As>
  230. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(As...)) {
  231. return (__godot_wrapper_method)&__wrapped_method<T, R, As...>;
  232. }
  233. template <class T, class R, class... A>
  234. void *___make_wrapper_function(R (T::*f)(A...) const) {
  235. return ___make_wrapper_function((R(T::*)(A...))f);
  236. }
  237. template <class T, class R, class... A>
  238. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A...) const) {
  239. return ___get_wrapper_function((R(T::*)(A...))f);
  240. }
  241. template <class M>
  242. void register_method(const char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
  243. godot_instance_method method = {};
  244. method.method_data = ___make_wrapper_function(method_ptr);
  245. method.free_func = godot::api->godot_free;
  246. method.method = (__godot_wrapper_method)___get_wrapper_function(method_ptr);
  247. godot_method_attributes attr = {};
  248. attr.rpc_type = rpc_type;
  249. godot::nativescript_api->godot_nativescript_register_method(godot::_RegisterState::nativescript_handle,
  250. ___get_method_class_name(method_ptr), name, attr, method);
  251. }
  252. // User can specify a derived class D to register the method for, instead of it being inferred.
  253. template <class D, class B, class R, class... As>
  254. void register_method_explicit(const char *name, R (B::*method_ptr)(As...),
  255. godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
  256. static_assert(std::is_base_of<B, D>::value, "Explicit class must derive from method class");
  257. register_method(name, static_cast<R (D::*)(As...)>(method_ptr), rpc_type);
  258. }
  259. template <class T, class P>
  260. struct _PropertySetFunc {
  261. void (T::*f)(P);
  262. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value) {
  263. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *)method_data;
  264. T *obj = (T *)user_data;
  265. Variant *v = (Variant *)value;
  266. (obj->*(set_func->f))(_ArgCast<P>::_arg_cast(*v));
  267. }
  268. };
  269. template <class T, class P>
  270. struct _PropertyGetFunc {
  271. P(T::*f)
  272. ();
  273. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data) {
  274. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *)method_data;
  275. T *obj = (T *)user_data;
  276. godot_variant var;
  277. godot::api->godot_variant_new_nil(&var);
  278. Variant *v = (Variant *)&var;
  279. *v = (obj->*(get_func->f))();
  280. return var;
  281. }
  282. };
  283. template <class T, class P>
  284. struct _PropertyDefaultSetFunc {
  285. P(T::*f);
  286. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value) {
  287. _PropertyDefaultSetFunc<T, P> *set_func = (_PropertyDefaultSetFunc<T, P> *)method_data;
  288. T *obj = (T *)user_data;
  289. Variant *v = (Variant *)value;
  290. (obj->*(set_func->f)) = _ArgCast<P>::_arg_cast(*v);
  291. }
  292. };
  293. template <class T, class P>
  294. struct _PropertyDefaultGetFunc {
  295. P(T::*f);
  296. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data) {
  297. _PropertyDefaultGetFunc<T, P> *get_func = (_PropertyDefaultGetFunc<T, P> *)method_data;
  298. T *obj = (T *)user_data;
  299. godot_variant var;
  300. godot::api->godot_variant_new_nil(&var);
  301. Variant *v = (Variant *)&var;
  302. *v = (obj->*(get_func->f));
  303. return var;
  304. }
  305. };
  306. template <class T, class P>
  307. void register_property(const char *name, P(T::*var), P default_value,
  308. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  309. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  310. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  311. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  312. Variant def_val = default_value;
  313. usage = (godot_property_usage_flags)((int)usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE);
  314. if (def_val.get_type() == Variant::OBJECT) {
  315. Object *o = detail::get_wrapper<Object>(def_val.operator godot_object *());
  316. if (o && o->is_class("Resource")) {
  317. hint = (godot_property_hint)((int)hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE);
  318. hint_string = o->get_class();
  319. }
  320. }
  321. godot_string *_hint_string = (godot_string *)&hint_string;
  322. godot_property_attributes attr = {};
  323. if (def_val.get_type() == Variant::NIL) {
  324. attr.type = Variant::OBJECT;
  325. } else {
  326. attr.type = def_val.get_type();
  327. attr.default_value = *(godot_variant *)&def_val;
  328. }
  329. attr.hint = hint;
  330. attr.rset_type = rpc_mode;
  331. attr.usage = usage;
  332. attr.hint_string = *_hint_string;
  333. _PropertyDefaultSetFunc<T, P> *wrapped_set =
  334. (_PropertyDefaultSetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultSetFunc<T, P>));
  335. wrapped_set->f = var;
  336. _PropertyDefaultGetFunc<T, P> *wrapped_get =
  337. (_PropertyDefaultGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultGetFunc<T, P>));
  338. wrapped_get->f = var;
  339. godot_property_set_func set_func = {};
  340. set_func.method_data = (void *)wrapped_set;
  341. set_func.free_func = godot::api->godot_free;
  342. set_func.set_func = &_PropertyDefaultSetFunc<T, P>::_wrapped_setter;
  343. godot_property_get_func get_func = {};
  344. get_func.method_data = (void *)wrapped_get;
  345. get_func.free_func = godot::api->godot_free;
  346. get_func.get_func = &_PropertyDefaultGetFunc<T, P>::_wrapped_getter;
  347. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle,
  348. T::___get_class_name(), name, &attr, set_func, get_func);
  349. }
  350. template <class T, class P>
  351. void register_property(const char *name, void (T::*setter)(P), P (T::*getter)(), P default_value,
  352. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  353. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  354. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  355. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  356. Variant def_val = default_value;
  357. godot_string *_hint_string = (godot_string *)&hint_string;
  358. godot_property_attributes attr = {};
  359. if (def_val.get_type() == Variant::NIL) {
  360. attr.type = Variant::OBJECT;
  361. } else {
  362. attr.type = def_val.get_type();
  363. attr.default_value = *(godot_variant *)&def_val;
  364. }
  365. attr.hint = hint;
  366. attr.rset_type = rpc_mode;
  367. attr.usage = usage;
  368. attr.hint_string = *_hint_string;
  369. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertySetFunc<T, P>));
  370. wrapped_set->f = setter;
  371. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyGetFunc<T, P>));
  372. wrapped_get->f = getter;
  373. godot_property_set_func set_func = {};
  374. set_func.method_data = (void *)wrapped_set;
  375. set_func.free_func = godot::api->godot_free;
  376. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  377. godot_property_get_func get_func = {};
  378. get_func.method_data = (void *)wrapped_get;
  379. get_func.free_func = godot::api->godot_free;
  380. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  381. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle,
  382. T::___get_class_name(), name, &attr, set_func, get_func);
  383. }
  384. template <class T, class P>
  385. void register_property(const char *name, void (T::*setter)(P), P (T::*getter)() const, P default_value,
  386. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  387. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  388. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  389. register_property(name, setter, (P(T::*)())getter, default_value, rpc_mode, usage, hint, hint_string);
  390. }
  391. template <class T>
  392. void register_signal(String name, Dictionary args = Dictionary()) {
  393. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  394. godot_signal signal = {};
  395. signal.name = *(godot_string *)&name;
  396. signal.num_args = args.size();
  397. signal.num_default_args = 0;
  398. // Need to check because malloc(0) is platform-dependent. Zero arguments will leave args to nullptr.
  399. if (signal.num_args != 0) {
  400. signal.args = (godot_signal_argument *)godot::api->godot_alloc(sizeof(godot_signal_argument) * signal.num_args);
  401. memset((void *)signal.args, 0, sizeof(godot_signal_argument) * signal.num_args);
  402. }
  403. for (int i = 0; i < signal.num_args; i++) {
  404. // Array entry = args[i];
  405. // String name = entry[0];
  406. String name = args.keys()[i];
  407. godot_string *_key = (godot_string *)&name;
  408. godot::api->godot_string_new_copy(&signal.args[i].name, _key);
  409. // if (entry.size() > 1) {
  410. // signal.args[i].type = entry[1];
  411. // }
  412. signal.args[i].type = args.values()[i];
  413. }
  414. godot::nativescript_api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle,
  415. T::___get_class_name(), &signal);
  416. for (int i = 0; i < signal.num_args; i++) {
  417. godot::api->godot_string_destroy(&signal.args[i].name);
  418. }
  419. if (signal.args) {
  420. godot::api->godot_free(signal.args);
  421. }
  422. }
  423. template <class T, class... Args>
  424. void register_signal(String name, Args... varargs) {
  425. register_signal<T>(name, Dictionary::make(varargs...));
  426. }
  427. #ifndef GODOT_CPP_NO_OBJECT_CAST
  428. template <class T>
  429. T *Object::cast_to(const Object *obj) {
  430. if (!obj)
  431. return nullptr;
  432. if (T::___CLASS_IS_SCRIPT) {
  433. size_t have_tag = (size_t)godot::nativescript_1_1_api->godot_nativescript_get_type_tag(obj->_owner);
  434. if (have_tag) {
  435. if (!godot::_TagDB::is_type_known((size_t)have_tag)) {
  436. have_tag = 0;
  437. }
  438. }
  439. if (!have_tag) {
  440. have_tag = obj->_type_tag;
  441. }
  442. if (godot::_TagDB::is_type_compatible(T::___get_id(), have_tag)) {
  443. return detail::get_custom_class_instance<T>(obj);
  444. }
  445. } else {
  446. if (godot::core_1_2_api->godot_object_cast_to(obj->_owner, (void *)T::___get_id())) {
  447. return (T *)obj;
  448. }
  449. }
  450. return nullptr;
  451. }
  452. #endif
  453. } // namespace godot
  454. #endif // GODOT_HPP