example.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /* godot-cpp integration testing project.
  2. *
  3. * This is free and unencumbered software released into the public domain.
  4. */
  5. #include "example.h"
  6. #include <godot_cpp/core/class_db.hpp>
  7. #include <godot_cpp/classes/global_constants.hpp>
  8. #include <godot_cpp/classes/label.hpp>
  9. #include <godot_cpp/classes/multiplayer_api.hpp>
  10. #include <godot_cpp/classes/multiplayer_peer.hpp>
  11. #include <godot_cpp/classes/os.hpp>
  12. #include <godot_cpp/variant/utility_functions.hpp>
  13. using namespace godot;
  14. class MyCallableCustom : public CallableCustom {
  15. public:
  16. virtual uint32_t hash() const {
  17. return 27;
  18. }
  19. virtual String get_as_text() const {
  20. return "<MyCallableCustom>";
  21. }
  22. static bool compare_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  23. return p_a == p_b;
  24. }
  25. virtual CompareEqualFunc get_compare_equal_func() const {
  26. return &MyCallableCustom::compare_equal_func;
  27. }
  28. static bool compare_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  29. return (void *)p_a < (void *)p_b;
  30. }
  31. virtual CompareLessFunc get_compare_less_func() const {
  32. return &MyCallableCustom::compare_less_func;
  33. }
  34. bool is_valid() const {
  35. return true;
  36. }
  37. virtual ObjectID get_object() const {
  38. return ObjectID();
  39. }
  40. virtual int get_argument_count(bool &r_is_valid) const {
  41. r_is_valid = true;
  42. return 2;
  43. }
  44. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const {
  45. r_return_value = "Hi";
  46. r_call_error.error = GDEXTENSION_CALL_OK;
  47. }
  48. };
  49. void ExampleRef::set_id(int p_id) {
  50. id = p_id;
  51. }
  52. int ExampleRef::get_id() const {
  53. return id;
  54. }
  55. void ExampleRef::_notification(int p_what) {
  56. if (p_what == NOTIFICATION_POSTINITIALIZE) {
  57. post_initialized = true;
  58. }
  59. }
  60. void ExampleRef::_bind_methods() {
  61. ClassDB::bind_method(D_METHOD("set_id", "id"), &ExampleRef::set_id);
  62. ClassDB::bind_method(D_METHOD("get_id"), &ExampleRef::get_id);
  63. ClassDB::bind_method(D_METHOD("was_post_initialized"), &ExampleRef::was_post_initialized);
  64. ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
  65. }
  66. ExampleRef::ExampleRef() {
  67. id = 0;
  68. }
  69. ExampleRef::~ExampleRef() {
  70. }
  71. int Example::test_static(int p_a, int p_b) {
  72. return p_a + p_b;
  73. }
  74. void Example::test_static2() {
  75. //UtilityFunctions::print(" void static");
  76. }
  77. int Example::def_args(int p_a, int p_b) {
  78. return p_a + p_b;
  79. }
  80. void Example::_notification(int p_what) {
  81. if (p_what == NOTIFICATION_READY) {
  82. Dictionary opts;
  83. opts["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY;
  84. opts["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
  85. opts["call_local"] = true;
  86. opts["channel"] = 0;
  87. rpc_config("test_rpc", opts);
  88. }
  89. //UtilityFunctions::print("Notification: ", String::num(p_what));
  90. }
  91. bool Example::_set(const StringName &p_name, const Variant &p_value) {
  92. String name = p_name;
  93. if (name.begins_with("dproperty")) {
  94. int64_t index = name.get_slicec('_', 1).to_int();
  95. dprop[index] = p_value;
  96. return true;
  97. }
  98. if (name == "property_from_list") {
  99. property_from_list = p_value;
  100. return true;
  101. }
  102. return false;
  103. }
  104. bool Example::_get(const StringName &p_name, Variant &r_ret) const {
  105. String name = p_name;
  106. if (name.begins_with("dproperty")) {
  107. int64_t index = name.get_slicec('_', 1).to_int();
  108. r_ret = dprop[index];
  109. return true;
  110. }
  111. if (name == "property_from_list") {
  112. r_ret = property_from_list;
  113. return true;
  114. }
  115. return false;
  116. }
  117. String Example::_to_string() const {
  118. return "[ GDExtension::Example <--> Instance ID:" + uitos(get_instance_id()) + " ]";
  119. }
  120. void Example::_get_property_list(List<PropertyInfo> *p_list) const {
  121. p_list->push_back(PropertyInfo(Variant::VECTOR3, "property_from_list"));
  122. for (int i = 0; i < 3; i++) {
  123. p_list->push_back(PropertyInfo(Variant::VECTOR2, "dproperty_" + itos(i)));
  124. }
  125. }
  126. bool Example::_property_can_revert(const StringName &p_name) const {
  127. if (p_name == StringName("property_from_list") && property_from_list != Vector3(42, 42, 42)) {
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. };
  133. bool Example::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  134. if (p_name == StringName("property_from_list")) {
  135. r_property = Vector3(42, 42, 42);
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. };
  141. void Example::_validate_property(PropertyInfo &p_property) const {
  142. String name = p_property.name;
  143. // Test hiding the "mouse_filter" property from the editor.
  144. if (name == "mouse_filter") {
  145. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  146. }
  147. }
  148. void Example::_bind_methods() {
  149. // Methods.
  150. ClassDB::bind_method(D_METHOD("simple_func"), &Example::simple_func);
  151. ClassDB::bind_method(D_METHOD("simple_const_func"), &Example::simple_const_func);
  152. ClassDB::bind_method(D_METHOD("custom_ref_func", "ref"), &Example::custom_ref_func);
  153. ClassDB::bind_method(D_METHOD("custom_const_ref_func", "ref"), &Example::custom_const_ref_func);
  154. ClassDB::bind_method(D_METHOD("image_ref_func", "image"), &Example::image_ref_func);
  155. ClassDB::bind_method(D_METHOD("image_const_ref_func", "image"), &Example::image_const_ref_func);
  156. ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
  157. ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
  158. ClassDB::bind_method(D_METHOD("return_empty_ref"), &Example::return_empty_ref);
  159. ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
  160. ClassDB::bind_method(D_METHOD("extended_ref_checks", "ref"), &Example::extended_ref_checks);
  161. ClassDB::bind_method(D_METHOD("is_object_binding_set_by_parent_constructor"), &Example::is_object_binding_set_by_parent_constructor);
  162. ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
  163. ClassDB::bind_method(D_METHOD("test_tarray_arg", "array"), &Example::test_tarray_arg);
  164. ClassDB::bind_method(D_METHOD("test_tarray"), &Example::test_tarray);
  165. ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
  166. ClassDB::bind_method(D_METHOD("test_tdictionary_arg", "dictionary"), &Example::test_tdictionary_arg);
  167. ClassDB::bind_method(D_METHOD("test_tdictionary"), &Example::test_tdictionary);
  168. ClassDB::bind_method(D_METHOD("test_node_argument"), &Example::test_node_argument);
  169. ClassDB::bind_method(D_METHOD("test_string_ops"), &Example::test_string_ops);
  170. ClassDB::bind_method(D_METHOD("test_str_utility"), &Example::test_str_utility);
  171. ClassDB::bind_method(D_METHOD("test_string_is_forty_two"), &Example::test_string_is_forty_two);
  172. ClassDB::bind_method(D_METHOD("test_string_resize"), &Example::test_string_resize);
  173. ClassDB::bind_method(D_METHOD("test_typed_array_of_packed"), &Example::test_typed_array_of_packed);
  174. ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
  175. ClassDB::bind_method(D_METHOD("test_vector_init_list"), &Example::test_vector_init_list);
  176. ClassDB::bind_method(D_METHOD("test_object_cast_to_node", "object"), &Example::test_object_cast_to_node);
  177. ClassDB::bind_method(D_METHOD("test_object_cast_to_control", "object"), &Example::test_object_cast_to_control);
  178. ClassDB::bind_method(D_METHOD("test_object_cast_to_example", "object"), &Example::test_object_cast_to_example);
  179. ClassDB::bind_method(D_METHOD("test_variant_vector2i_conversion", "variant"), &Example::test_variant_vector2i_conversion);
  180. ClassDB::bind_method(D_METHOD("test_variant_int_conversion", "variant"), &Example::test_variant_int_conversion);
  181. ClassDB::bind_method(D_METHOD("test_variant_float_conversion", "variant"), &Example::test_variant_float_conversion);
  182. ClassDB::bind_method(D_METHOD("test_add_child", "node"), &Example::test_add_child);
  183. ClassDB::bind_method(D_METHOD("test_set_tileset", "tilemap", "tileset"), &Example::test_set_tileset);
  184. ClassDB::bind_method(D_METHOD("test_variant_call", "variant"), &Example::test_variant_call);
  185. ClassDB::bind_method(D_METHOD("test_callable_mp"), &Example::test_callable_mp);
  186. ClassDB::bind_method(D_METHOD("test_callable_mp_ret"), &Example::test_callable_mp_ret);
  187. ClassDB::bind_method(D_METHOD("test_callable_mp_retc"), &Example::test_callable_mp_retc);
  188. ClassDB::bind_method(D_METHOD("test_callable_mp_static"), &Example::test_callable_mp_static);
  189. ClassDB::bind_method(D_METHOD("test_callable_mp_static_ret"), &Example::test_callable_mp_static_ret);
  190. ClassDB::bind_method(D_METHOD("test_custom_callable"), &Example::test_custom_callable);
  191. ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
  192. ClassDB::bind_method(D_METHOD("test_variant_iterator", "input"), &Example::test_variant_iterator);
  193. ClassDB::bind_method(D_METHOD("test_rpc", "value"), &Example::test_rpc);
  194. ClassDB::bind_method(D_METHOD("test_send_rpc", "value"), &Example::test_send_rpc);
  195. ClassDB::bind_method(D_METHOD("return_last_rpc_arg"), &Example::return_last_rpc_arg);
  196. ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
  197. ClassDB::bind_method(D_METHOD("callable_bind"), &Example::callable_bind);
  198. ClassDB::bind_method(D_METHOD("test_post_initialize"), &Example::test_post_initialize);
  199. GDVIRTUAL_BIND(_do_something_virtual, "name", "value");
  200. ClassDB::bind_method(D_METHOD("test_virtual_implemented_in_script"), &Example::test_virtual_implemented_in_script);
  201. GDVIRTUAL_BIND(_do_something_virtual_with_control, "control");
  202. ClassDB::bind_method(D_METHOD("test_use_engine_singleton"), &Example::test_use_engine_singleton);
  203. ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
  204. ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
  205. ClassDB::bind_static_method("Example", D_METHOD("test_library_path"), &Example::test_library_path);
  206. {
  207. MethodInfo mi;
  208. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  209. mi.name = "varargs_func";
  210. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func", &Example::varargs_func, mi);
  211. }
  212. {
  213. MethodInfo mi;
  214. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  215. mi.name = "varargs_func_nv";
  216. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_nv", &Example::varargs_func_nv, mi);
  217. }
  218. {
  219. MethodInfo mi;
  220. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  221. mi.name = "varargs_func_void";
  222. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_void", &Example::varargs_func_void, mi);
  223. }
  224. // Properties.
  225. ADD_GROUP("Test group", "group_");
  226. ADD_SUBGROUP("Test subgroup", "group_subgroup_");
  227. ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
  228. ClassDB::bind_method(D_METHOD("get_v4"), &Example::get_v4);
  229. ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
  230. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
  231. // Signals.
  232. ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
  233. ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
  234. // Constants.
  235. BIND_ENUM_CONSTANT(FIRST);
  236. BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
  237. BIND_BITFIELD_FLAG(FLAG_ONE);
  238. BIND_BITFIELD_FLAG(FLAG_TWO);
  239. BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
  240. BIND_ENUM_CONSTANT(OUTSIDE_OF_CLASS);
  241. }
  242. bool Example::has_object_instance_binding() const {
  243. return internal::gdextension_interface_object_get_instance_binding(_owner, internal::token, nullptr);
  244. }
  245. Example::Example() :
  246. object_instance_binding_set_by_parent_constructor(has_object_instance_binding()) {
  247. // Test conversion, to ensure users can use all parent class functions at this time.
  248. // It would crash if instance binding still not be initialized.
  249. Variant v = Variant(this);
  250. Object *o = (Object *)v;
  251. //UtilityFunctions::print("Constructor.");
  252. }
  253. Example::~Example() {
  254. //UtilityFunctions::print("Destructor.");
  255. }
  256. // Methods.
  257. void Example::simple_func() {
  258. emit_custom_signal("simple_func", 3);
  259. }
  260. void Example::simple_const_func() const {
  261. ((Example *)this)->emit_custom_signal("simple_const_func", 4);
  262. }
  263. int Example::custom_ref_func(Ref<ExampleRef> p_ref) {
  264. return p_ref.is_valid() ? p_ref->get_id() : -1;
  265. }
  266. int Example::custom_const_ref_func(const Ref<ExampleRef> &p_ref) {
  267. return p_ref.is_valid() ? p_ref->get_id() : -1;
  268. }
  269. String Example::image_ref_func(Ref<Image> p_image) {
  270. return p_image.is_valid() ? String("valid") : String("invalid");
  271. }
  272. String Example::image_const_ref_func(const Ref<Image> &p_image) {
  273. return p_image.is_valid() ? String("valid") : String("invalid");
  274. }
  275. String Example::return_something(const String &base) {
  276. return base + String("42");
  277. }
  278. Viewport *Example::return_something_const() const {
  279. if (is_inside_tree()) {
  280. Viewport *result = get_viewport();
  281. return result;
  282. }
  283. return nullptr;
  284. }
  285. Ref<ExampleRef> Example::return_empty_ref() const {
  286. Ref<ExampleRef> ref;
  287. return ref;
  288. }
  289. ExampleRef *Example::return_extended_ref() const {
  290. // You can instance and return a refcounted object like this, but keep in mind that refcounting starts with the returned object
  291. // and it will be destroyed when all references are destroyed. If you store this pointer you run the risk of having a pointer
  292. // to a destroyed object.
  293. return memnew(ExampleRef());
  294. }
  295. Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
  296. // This is therefore the preferred way of instancing and returning a refcounted object:
  297. Ref<ExampleRef> ref;
  298. ref.instantiate();
  299. return ref;
  300. }
  301. Variant Example::varargs_func(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  302. return arg_count;
  303. }
  304. int Example::varargs_func_nv(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  305. return 42 + arg_count;
  306. }
  307. void Example::varargs_func_void(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  308. emit_custom_signal("varargs_func_void", arg_count + 1);
  309. }
  310. void Example::emit_custom_signal(const String &name, int value) {
  311. emit_signal("custom_signal", name, value);
  312. }
  313. bool Example::is_object_binding_set_by_parent_constructor() const {
  314. return object_instance_binding_set_by_parent_constructor;
  315. }
  316. Array Example::test_array() const {
  317. Array arr;
  318. arr.resize(2);
  319. arr[0] = Variant(1);
  320. arr[1] = Variant(2);
  321. return arr;
  322. }
  323. String Example::test_string_ops() const {
  324. String s = String("A");
  325. s += "B";
  326. s += "C";
  327. s += char32_t(0x010E);
  328. s = s + "E";
  329. return s;
  330. }
  331. String Example::test_str_utility() const {
  332. return UtilityFunctions::str("Hello, ", "World", "! The answer is ", 42);
  333. }
  334. bool Example::test_string_is_forty_two(const String &p_string) const {
  335. return strcmp(p_string.utf8().ptr(), "forty two") == 0;
  336. }
  337. String Example::test_string_resize(String p_string) const {
  338. int orig_len = p_string.length();
  339. p_string.resize(orig_len + 3);
  340. char32_t *data = p_string.ptrw();
  341. data[orig_len + 0] = '!';
  342. data[orig_len + 1] = '?';
  343. data[orig_len + 2] = '\0';
  344. return p_string;
  345. }
  346. TypedArray<PackedInt32Array> Example::test_typed_array_of_packed() const {
  347. TypedArray<PackedInt32Array> arr;
  348. PackedInt32Array packed_arr1;
  349. packed_arr1.push_back(1);
  350. packed_arr1.push_back(2);
  351. arr.push_back(packed_arr1);
  352. PackedInt32Array packed_arr2;
  353. packed_arr2.push_back(3);
  354. packed_arr2.push_back(4);
  355. arr.push_back(packed_arr2);
  356. return arr;
  357. }
  358. int Example::test_vector_ops() const {
  359. PackedInt32Array arr;
  360. arr.push_back(10);
  361. arr.push_back(20);
  362. arr.push_back(30);
  363. arr.push_back(45);
  364. int ret = 0;
  365. for (const int32_t &E : arr) {
  366. ret += E;
  367. }
  368. return ret;
  369. }
  370. int Example::test_vector_init_list() const {
  371. PackedInt32Array arr = { 10, 20, 30, 45 };
  372. int ret = 0;
  373. for (const int32_t &E : arr) {
  374. ret += E;
  375. }
  376. return ret;
  377. }
  378. Callable Example::test_callable_mp() {
  379. return callable_mp(this, &Example::unbound_method1);
  380. }
  381. Callable Example::test_callable_mp_ret() {
  382. return callable_mp(this, &Example::unbound_method2);
  383. }
  384. Callable Example::test_callable_mp_retc() const {
  385. return callable_mp(this, &Example::unbound_method3);
  386. }
  387. Callable Example::test_callable_mp_static() const {
  388. return callable_mp_static(&Example::unbound_static_method1);
  389. }
  390. Callable Example::test_callable_mp_static_ret() const {
  391. return callable_mp_static(&Example::unbound_static_method2);
  392. }
  393. Callable Example::test_custom_callable() const {
  394. return Callable(memnew(MyCallableCustom));
  395. }
  396. void Example::unbound_method1(Object *p_object, String p_string, int p_int) {
  397. String test = "unbound_method1: ";
  398. test += p_object->get_class();
  399. test += " - " + p_string;
  400. emit_custom_signal(test, p_int);
  401. }
  402. String Example::unbound_method2(Object *p_object, String p_string, int p_int) {
  403. String test = "unbound_method2: ";
  404. test += p_object->get_class();
  405. test += " - " + p_string;
  406. test += " - " + itos(p_int);
  407. return test;
  408. }
  409. String Example::unbound_method3(Object *p_object, String p_string, int p_int) const {
  410. String test = "unbound_method3: ";
  411. test += p_object->get_class();
  412. test += " - " + p_string;
  413. test += " - " + itos(p_int);
  414. return test;
  415. }
  416. void Example::unbound_static_method1(Example *p_object, String p_string, int p_int) {
  417. String test = "unbound_static_method1: ";
  418. test += p_object->get_class();
  419. test += " - " + p_string;
  420. p_object->emit_custom_signal(test, p_int);
  421. }
  422. String Example::unbound_static_method2(Object *p_object, String p_string, int p_int) {
  423. String test = "unbound_static_method2: ";
  424. test += p_object->get_class();
  425. test += " - " + p_string;
  426. test += " - " + itos(p_int);
  427. return test;
  428. }
  429. int Example::test_tarray_arg(const TypedArray<int64_t> &p_array) {
  430. int sum = 0;
  431. for (int i = 0; i < p_array.size(); i++) {
  432. sum += (int)p_array[i];
  433. }
  434. return sum;
  435. }
  436. TypedArray<Vector2> Example::test_tarray() const {
  437. TypedArray<Vector2> arr;
  438. arr.resize(2);
  439. arr[0] = Vector2(1, 2);
  440. arr[1] = Vector2(2, 3);
  441. return arr;
  442. }
  443. Dictionary Example::test_dictionary() const {
  444. Dictionary dict;
  445. dict["hello"] = "world";
  446. dict["foo"] = "bar";
  447. return dict;
  448. }
  449. int Example::test_tdictionary_arg(const TypedDictionary<String, int64_t> &p_dictionary) {
  450. int sum = 0;
  451. TypedArray<int64_t> values = p_dictionary.values();
  452. for (int i = 0; i < p_dictionary.size(); i++) {
  453. sum += (int)values[i];
  454. }
  455. return sum;
  456. }
  457. TypedDictionary<Vector2, Vector2i> Example::test_tdictionary() const {
  458. TypedDictionary<Vector2, Vector2i> dict;
  459. dict[Vector2(1, 2)] = Vector2i(2, 3);
  460. return dict;
  461. }
  462. Example *Example::test_node_argument(Example *p_node) const {
  463. return p_node;
  464. }
  465. bool Example::test_object_cast_to_node(Object *p_object) const {
  466. return Object::cast_to<Node>(p_object) != nullptr;
  467. }
  468. bool Example::test_object_cast_to_control(Object *p_object) const {
  469. return Object::cast_to<Control>(p_object) != nullptr;
  470. }
  471. bool Example::test_object_cast_to_example(Object *p_object) const {
  472. return Object::cast_to<Example>(p_object) != nullptr;
  473. }
  474. Vector2i Example::test_variant_vector2i_conversion(const Variant &p_variant) const {
  475. return p_variant;
  476. }
  477. int Example::test_variant_int_conversion(const Variant &p_variant) const {
  478. return p_variant;
  479. }
  480. float Example::test_variant_float_conversion(const Variant &p_variant) const {
  481. return p_variant;
  482. }
  483. void Example::test_add_child(Node *p_node) {
  484. add_child(p_node);
  485. }
  486. void Example::test_set_tileset(TileMap *p_tilemap, const Ref<TileSet> &p_tileset) const {
  487. p_tilemap->set_tileset(p_tileset);
  488. }
  489. Variant Example::test_variant_call(Variant p_variant) {
  490. return p_variant.call("test", "hello");
  491. }
  492. BitField<Example::Flags> Example::test_bitfield(BitField<Flags> flags) {
  493. return flags;
  494. }
  495. Variant Example::test_variant_iterator(const Variant &p_input) {
  496. Array output;
  497. Variant iter;
  498. bool is_init_valid = true;
  499. if (!p_input.iter_init(iter, is_init_valid)) {
  500. if (!is_init_valid) {
  501. return "iter_init: not valid";
  502. }
  503. return output;
  504. }
  505. bool is_iter_next_valid = true;
  506. bool is_iter_get_valid = true;
  507. do {
  508. if (!is_iter_next_valid) {
  509. return "iter_next: not valid";
  510. }
  511. Variant value = p_input.iter_get(iter, is_iter_get_valid);
  512. if (!is_iter_get_valid) {
  513. return "iter_get: not valid";
  514. }
  515. output.push_back(((int)value) + 5);
  516. } while (p_input.iter_next(iter, is_iter_next_valid));
  517. if (!is_iter_next_valid) {
  518. return "iter_next: not valid";
  519. }
  520. return output;
  521. }
  522. void Example::test_rpc(int p_value) {
  523. last_rpc_arg = p_value;
  524. }
  525. void Example::test_send_rpc(int p_value) {
  526. rpc("test_rpc", p_value);
  527. }
  528. int Example::return_last_rpc_arg() {
  529. return last_rpc_arg;
  530. }
  531. void Example::callable_bind() {
  532. Callable c = Callable(this, "emit_custom_signal").bind("bound", 11);
  533. c.call();
  534. }
  535. // Properties.
  536. void Example::set_custom_position(const Vector2 &pos) {
  537. custom_position = pos;
  538. }
  539. Vector2 Example::get_custom_position() const {
  540. return custom_position;
  541. }
  542. Vector4 Example::get_v4() const {
  543. return Vector4(1.2, 3.4, 5.6, 7.8);
  544. }
  545. bool Example::test_post_initialize() const {
  546. Ref<ExampleRef> new_example_ref;
  547. new_example_ref.instantiate();
  548. return new_example_ref->was_post_initialized();
  549. }
  550. // Virtual function override.
  551. bool Example::_has_point(const Vector2 &point) const {
  552. Label *label = get_node<Label>("Label");
  553. label->set_text("Got point: " + Variant(point).stringify());
  554. return false;
  555. }
  556. void Example::_input(const Ref<InputEvent> &event) {
  557. const InputEventKey *key_event = Object::cast_to<const InputEventKey>(*event);
  558. if (key_event) {
  559. emit_custom_signal(String("_input: ") + key_event->get_key_label(), key_event->get_unicode());
  560. }
  561. }
  562. void ExampleBase::_bind_methods() {
  563. ClassDB::bind_method(D_METHOD("get_value1"), &ExampleBase::get_value1);
  564. ClassDB::bind_method(D_METHOD("get_value2"), &ExampleBase::get_value2);
  565. }
  566. void ExampleBase::_notification(int p_what) {
  567. if (p_what == NOTIFICATION_ENTER_TREE) {
  568. value1 = 11;
  569. value2 = 22;
  570. }
  571. }
  572. void ExampleChild::_notification(int p_what) {
  573. if (p_what == NOTIFICATION_ENTER_TREE) {
  574. value2 = 33;
  575. }
  576. }
  577. String Example::test_virtual_implemented_in_script(const String &p_name, int p_value) {
  578. String ret;
  579. if (GDVIRTUAL_CALL(_do_something_virtual, p_name, p_value, ret)) {
  580. return ret;
  581. }
  582. return "Unimplemented";
  583. }
  584. String Example::test_use_engine_singleton() const {
  585. return OS::get_singleton()->get_name();
  586. }
  587. String Example::test_library_path() {
  588. String library_path;
  589. internal::gdextension_interface_get_library_path(internal::library, library_path._native_ptr());
  590. return library_path;
  591. }
  592. void ExampleRuntime::_bind_methods() {
  593. ClassDB::bind_method(D_METHOD("set_prop_value", "value"), &ExampleRuntime::set_prop_value);
  594. ClassDB::bind_method(D_METHOD("get_prop_value"), &ExampleRuntime::get_prop_value);
  595. ADD_PROPERTY(PropertyInfo(Variant::INT, "prop_value"), "set_prop_value", "get_prop_value");
  596. }
  597. void ExampleRuntime::set_prop_value(int p_prop_value) {
  598. prop_value = p_prop_value;
  599. }
  600. int ExampleRuntime::get_prop_value() const {
  601. return prop_value;
  602. }
  603. ExampleRuntime::ExampleRuntime() {
  604. }
  605. ExampleRuntime::~ExampleRuntime() {
  606. }