example.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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_node_argument"), &Example::test_node_argument);
  167. ClassDB::bind_method(D_METHOD("test_string_ops"), &Example::test_string_ops);
  168. ClassDB::bind_method(D_METHOD("test_str_utility"), &Example::test_str_utility);
  169. ClassDB::bind_method(D_METHOD("test_string_is_forty_two"), &Example::test_string_is_forty_two);
  170. ClassDB::bind_method(D_METHOD("test_string_resize"), &Example::test_string_resize);
  171. ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
  172. ClassDB::bind_method(D_METHOD("test_vector_init_list"), &Example::test_vector_init_list);
  173. ClassDB::bind_method(D_METHOD("test_object_cast_to_node", "object"), &Example::test_object_cast_to_node);
  174. ClassDB::bind_method(D_METHOD("test_object_cast_to_control", "object"), &Example::test_object_cast_to_control);
  175. ClassDB::bind_method(D_METHOD("test_object_cast_to_example", "object"), &Example::test_object_cast_to_example);
  176. ClassDB::bind_method(D_METHOD("test_variant_vector2i_conversion", "variant"), &Example::test_variant_vector2i_conversion);
  177. ClassDB::bind_method(D_METHOD("test_variant_int_conversion", "variant"), &Example::test_variant_int_conversion);
  178. ClassDB::bind_method(D_METHOD("test_variant_float_conversion", "variant"), &Example::test_variant_float_conversion);
  179. ClassDB::bind_method(D_METHOD("test_add_child", "node"), &Example::test_add_child);
  180. ClassDB::bind_method(D_METHOD("test_set_tileset", "tilemap", "tileset"), &Example::test_set_tileset);
  181. ClassDB::bind_method(D_METHOD("test_variant_call", "variant"), &Example::test_variant_call);
  182. ClassDB::bind_method(D_METHOD("test_callable_mp"), &Example::test_callable_mp);
  183. ClassDB::bind_method(D_METHOD("test_callable_mp_ret"), &Example::test_callable_mp_ret);
  184. ClassDB::bind_method(D_METHOD("test_callable_mp_retc"), &Example::test_callable_mp_retc);
  185. ClassDB::bind_method(D_METHOD("test_callable_mp_static"), &Example::test_callable_mp_static);
  186. ClassDB::bind_method(D_METHOD("test_callable_mp_static_ret"), &Example::test_callable_mp_static_ret);
  187. ClassDB::bind_method(D_METHOD("test_custom_callable"), &Example::test_custom_callable);
  188. ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
  189. ClassDB::bind_method(D_METHOD("test_variant_iterator", "input"), &Example::test_variant_iterator);
  190. ClassDB::bind_method(D_METHOD("test_rpc", "value"), &Example::test_rpc);
  191. ClassDB::bind_method(D_METHOD("test_send_rpc", "value"), &Example::test_send_rpc);
  192. ClassDB::bind_method(D_METHOD("return_last_rpc_arg"), &Example::return_last_rpc_arg);
  193. ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
  194. ClassDB::bind_method(D_METHOD("callable_bind"), &Example::callable_bind);
  195. ClassDB::bind_method(D_METHOD("test_post_initialize"), &Example::test_post_initialize);
  196. GDVIRTUAL_BIND(_do_something_virtual, "name", "value");
  197. ClassDB::bind_method(D_METHOD("test_virtual_implemented_in_script"), &Example::test_virtual_implemented_in_script);
  198. GDVIRTUAL_BIND(_do_something_virtual_with_control, "control");
  199. ClassDB::bind_method(D_METHOD("test_use_engine_singleton"), &Example::test_use_engine_singleton);
  200. ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
  201. ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
  202. {
  203. MethodInfo mi;
  204. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  205. mi.name = "varargs_func";
  206. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func", &Example::varargs_func, mi);
  207. }
  208. {
  209. MethodInfo mi;
  210. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  211. mi.name = "varargs_func_nv";
  212. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_nv", &Example::varargs_func_nv, mi);
  213. }
  214. {
  215. MethodInfo mi;
  216. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  217. mi.name = "varargs_func_void";
  218. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_void", &Example::varargs_func_void, mi);
  219. }
  220. // Properties.
  221. ADD_GROUP("Test group", "group_");
  222. ADD_SUBGROUP("Test subgroup", "group_subgroup_");
  223. ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
  224. ClassDB::bind_method(D_METHOD("get_v4"), &Example::get_v4);
  225. ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
  226. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
  227. // Signals.
  228. ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
  229. ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
  230. // Constants.
  231. BIND_ENUM_CONSTANT(FIRST);
  232. BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
  233. BIND_BITFIELD_FLAG(FLAG_ONE);
  234. BIND_BITFIELD_FLAG(FLAG_TWO);
  235. BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
  236. BIND_ENUM_CONSTANT(OUTSIDE_OF_CLASS);
  237. }
  238. bool Example::has_object_instance_binding() const {
  239. return internal::gdextension_interface_object_get_instance_binding(_owner, internal::token, nullptr);
  240. }
  241. Example::Example() :
  242. object_instance_binding_set_by_parent_constructor(has_object_instance_binding()) {
  243. // Test conversion, to ensure users can use all parent class functions at this time.
  244. // It would crash if instance binding still not be initialized.
  245. Variant v = Variant(this);
  246. Object *o = (Object *)v;
  247. //UtilityFunctions::print("Constructor.");
  248. }
  249. Example::~Example() {
  250. //UtilityFunctions::print("Destructor.");
  251. }
  252. // Methods.
  253. void Example::simple_func() {
  254. emit_custom_signal("simple_func", 3);
  255. }
  256. void Example::simple_const_func() const {
  257. ((Example *)this)->emit_custom_signal("simple_const_func", 4);
  258. }
  259. int Example::custom_ref_func(Ref<ExampleRef> p_ref) {
  260. return p_ref.is_valid() ? p_ref->get_id() : -1;
  261. }
  262. int Example::custom_const_ref_func(const Ref<ExampleRef> &p_ref) {
  263. return p_ref.is_valid() ? p_ref->get_id() : -1;
  264. }
  265. String Example::image_ref_func(Ref<Image> p_image) {
  266. return p_image.is_valid() ? String("valid") : String("invalid");
  267. }
  268. String Example::image_const_ref_func(const Ref<Image> &p_image) {
  269. return p_image.is_valid() ? String("valid") : String("invalid");
  270. }
  271. String Example::return_something(const String &base) {
  272. return base + String("42");
  273. }
  274. Viewport *Example::return_something_const() const {
  275. if (is_inside_tree()) {
  276. Viewport *result = get_viewport();
  277. return result;
  278. }
  279. return nullptr;
  280. }
  281. Ref<ExampleRef> Example::return_empty_ref() const {
  282. Ref<ExampleRef> ref;
  283. return ref;
  284. }
  285. ExampleRef *Example::return_extended_ref() const {
  286. // You can instance and return a refcounted object like this, but keep in mind that refcounting starts with the returned object
  287. // and it will be destroyed when all references are destroyed. If you store this pointer you run the risk of having a pointer
  288. // to a destroyed object.
  289. return memnew(ExampleRef());
  290. }
  291. Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
  292. // This is therefore the preferred way of instancing and returning a refcounted object:
  293. Ref<ExampleRef> ref;
  294. ref.instantiate();
  295. return ref;
  296. }
  297. Variant Example::varargs_func(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  298. return arg_count;
  299. }
  300. int Example::varargs_func_nv(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  301. return 42 + arg_count;
  302. }
  303. void Example::varargs_func_void(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  304. emit_custom_signal("varargs_func_void", arg_count + 1);
  305. }
  306. void Example::emit_custom_signal(const String &name, int value) {
  307. emit_signal("custom_signal", name, value);
  308. }
  309. bool Example::is_object_binding_set_by_parent_constructor() const {
  310. return object_instance_binding_set_by_parent_constructor;
  311. }
  312. Array Example::test_array() const {
  313. Array arr;
  314. arr.resize(2);
  315. arr[0] = Variant(1);
  316. arr[1] = Variant(2);
  317. return arr;
  318. }
  319. String Example::test_string_ops() const {
  320. String s = String("A");
  321. s += "B";
  322. s += "C";
  323. s += char32_t(0x010E);
  324. s = s + "E";
  325. return s;
  326. }
  327. String Example::test_str_utility() const {
  328. return UtilityFunctions::str("Hello, ", "World", "! The answer is ", 42);
  329. }
  330. bool Example::test_string_is_forty_two(const String &p_string) const {
  331. return strcmp(p_string.utf8().ptr(), "forty two") == 0;
  332. }
  333. String Example::test_string_resize(String p_string) const {
  334. int orig_len = p_string.length();
  335. p_string.resize(orig_len + 3);
  336. char32_t *data = p_string.ptrw();
  337. data[orig_len + 0] = '!';
  338. data[orig_len + 1] = '?';
  339. data[orig_len + 2] = '\0';
  340. return p_string;
  341. }
  342. int Example::test_vector_ops() const {
  343. PackedInt32Array arr;
  344. arr.push_back(10);
  345. arr.push_back(20);
  346. arr.push_back(30);
  347. arr.push_back(45);
  348. int ret = 0;
  349. for (const int32_t &E : arr) {
  350. ret += E;
  351. }
  352. return ret;
  353. }
  354. int Example::test_vector_init_list() const {
  355. PackedInt32Array arr = { 10, 20, 30, 45 };
  356. int ret = 0;
  357. for (const int32_t &E : arr) {
  358. ret += E;
  359. }
  360. return ret;
  361. }
  362. Callable Example::test_callable_mp() {
  363. return callable_mp(this, &Example::unbound_method1);
  364. }
  365. Callable Example::test_callable_mp_ret() {
  366. return callable_mp(this, &Example::unbound_method2);
  367. }
  368. Callable Example::test_callable_mp_retc() const {
  369. return callable_mp(this, &Example::unbound_method3);
  370. }
  371. Callable Example::test_callable_mp_static() const {
  372. return callable_mp_static(&Example::unbound_static_method1);
  373. }
  374. Callable Example::test_callable_mp_static_ret() const {
  375. return callable_mp_static(&Example::unbound_static_method2);
  376. }
  377. Callable Example::test_custom_callable() const {
  378. return Callable(memnew(MyCallableCustom));
  379. }
  380. void Example::unbound_method1(Object *p_object, String p_string, int p_int) {
  381. String test = "unbound_method1: ";
  382. test += p_object->get_class();
  383. test += " - " + p_string;
  384. emit_custom_signal(test, p_int);
  385. }
  386. String Example::unbound_method2(Object *p_object, String p_string, int p_int) {
  387. String test = "unbound_method2: ";
  388. test += p_object->get_class();
  389. test += " - " + p_string;
  390. test += " - " + itos(p_int);
  391. return test;
  392. }
  393. String Example::unbound_method3(Object *p_object, String p_string, int p_int) const {
  394. String test = "unbound_method3: ";
  395. test += p_object->get_class();
  396. test += " - " + p_string;
  397. test += " - " + itos(p_int);
  398. return test;
  399. }
  400. void Example::unbound_static_method1(Example *p_object, String p_string, int p_int) {
  401. String test = "unbound_static_method1: ";
  402. test += p_object->get_class();
  403. test += " - " + p_string;
  404. p_object->emit_custom_signal(test, p_int);
  405. }
  406. String Example::unbound_static_method2(Object *p_object, String p_string, int p_int) {
  407. String test = "unbound_static_method2: ";
  408. test += p_object->get_class();
  409. test += " - " + p_string;
  410. test += " - " + itos(p_int);
  411. return test;
  412. }
  413. int Example::test_tarray_arg(const TypedArray<int64_t> &p_array) {
  414. int sum = 0;
  415. for (int i = 0; i < p_array.size(); i++) {
  416. sum += (int)p_array[i];
  417. }
  418. return sum;
  419. }
  420. TypedArray<Vector2> Example::test_tarray() const {
  421. TypedArray<Vector2> arr;
  422. arr.resize(2);
  423. arr[0] = Vector2(1, 2);
  424. arr[1] = Vector2(2, 3);
  425. return arr;
  426. }
  427. Dictionary Example::test_dictionary() const {
  428. Dictionary dict;
  429. dict["hello"] = "world";
  430. dict["foo"] = "bar";
  431. return dict;
  432. }
  433. Example *Example::test_node_argument(Example *p_node) const {
  434. return p_node;
  435. }
  436. bool Example::test_object_cast_to_node(Object *p_object) const {
  437. return Object::cast_to<Node>(p_object) != nullptr;
  438. }
  439. bool Example::test_object_cast_to_control(Object *p_object) const {
  440. return Object::cast_to<Control>(p_object) != nullptr;
  441. }
  442. bool Example::test_object_cast_to_example(Object *p_object) const {
  443. return Object::cast_to<Example>(p_object) != nullptr;
  444. }
  445. Vector2i Example::test_variant_vector2i_conversion(const Variant &p_variant) const {
  446. return p_variant;
  447. }
  448. int Example::test_variant_int_conversion(const Variant &p_variant) const {
  449. return p_variant;
  450. }
  451. float Example::test_variant_float_conversion(const Variant &p_variant) const {
  452. return p_variant;
  453. }
  454. void Example::test_add_child(Node *p_node) {
  455. add_child(p_node);
  456. }
  457. void Example::test_set_tileset(TileMap *p_tilemap, const Ref<TileSet> &p_tileset) const {
  458. p_tilemap->set_tileset(p_tileset);
  459. }
  460. Variant Example::test_variant_call(Variant p_variant) {
  461. return p_variant.call("test", "hello");
  462. }
  463. BitField<Example::Flags> Example::test_bitfield(BitField<Flags> flags) {
  464. return flags;
  465. }
  466. Variant Example::test_variant_iterator(const Variant &p_input) {
  467. Array output;
  468. Variant iter;
  469. bool is_init_valid = true;
  470. if (!p_input.iter_init(iter, is_init_valid)) {
  471. if (!is_init_valid) {
  472. return "iter_init: not valid";
  473. }
  474. return output;
  475. }
  476. bool is_iter_next_valid = true;
  477. bool is_iter_get_valid = true;
  478. do {
  479. if (!is_iter_next_valid) {
  480. return "iter_next: not valid";
  481. }
  482. Variant value = p_input.iter_get(iter, is_iter_get_valid);
  483. if (!is_iter_get_valid) {
  484. return "iter_get: not valid";
  485. }
  486. output.push_back(((int)value) + 5);
  487. } while (p_input.iter_next(iter, is_iter_next_valid));
  488. if (!is_iter_next_valid) {
  489. return "iter_next: not valid";
  490. }
  491. return output;
  492. }
  493. void Example::test_rpc(int p_value) {
  494. last_rpc_arg = p_value;
  495. }
  496. void Example::test_send_rpc(int p_value) {
  497. rpc("test_rpc", p_value);
  498. }
  499. int Example::return_last_rpc_arg() {
  500. return last_rpc_arg;
  501. }
  502. void Example::callable_bind() {
  503. Callable c = Callable(this, "emit_custom_signal").bind("bound", 11);
  504. c.call();
  505. }
  506. // Properties.
  507. void Example::set_custom_position(const Vector2 &pos) {
  508. custom_position = pos;
  509. }
  510. Vector2 Example::get_custom_position() const {
  511. return custom_position;
  512. }
  513. Vector4 Example::get_v4() const {
  514. return Vector4(1.2, 3.4, 5.6, 7.8);
  515. }
  516. bool Example::test_post_initialize() const {
  517. Ref<ExampleRef> new_example_ref;
  518. new_example_ref.instantiate();
  519. return new_example_ref->was_post_initialized();
  520. }
  521. // Virtual function override.
  522. bool Example::_has_point(const Vector2 &point) const {
  523. Label *label = get_node<Label>("Label");
  524. label->set_text("Got point: " + Variant(point).stringify());
  525. return false;
  526. }
  527. void Example::_input(const Ref<InputEvent> &event) {
  528. const InputEventKey *key_event = Object::cast_to<const InputEventKey>(*event);
  529. if (key_event) {
  530. emit_custom_signal(String("_input: ") + key_event->get_key_label(), key_event->get_unicode());
  531. }
  532. }
  533. void ExampleBase::_bind_methods() {
  534. ClassDB::bind_method(D_METHOD("get_value1"), &ExampleBase::get_value1);
  535. ClassDB::bind_method(D_METHOD("get_value2"), &ExampleBase::get_value2);
  536. }
  537. void ExampleBase::_notification(int p_what) {
  538. if (p_what == NOTIFICATION_ENTER_TREE) {
  539. value1 = 11;
  540. value2 = 22;
  541. }
  542. }
  543. void ExampleChild::_notification(int p_what) {
  544. if (p_what == NOTIFICATION_ENTER_TREE) {
  545. value2 = 33;
  546. }
  547. }
  548. String Example::test_virtual_implemented_in_script(const String &p_name, int p_value) {
  549. String ret;
  550. if (GDVIRTUAL_CALL(_do_something_virtual, p_name, p_value, ret)) {
  551. return ret;
  552. }
  553. return "Unimplemented";
  554. }
  555. String Example::test_use_engine_singleton() const {
  556. return OS::get_singleton()->get_name();
  557. }
  558. void ExampleRuntime::_bind_methods() {
  559. ClassDB::bind_method(D_METHOD("set_prop_value", "value"), &ExampleRuntime::set_prop_value);
  560. ClassDB::bind_method(D_METHOD("get_prop_value"), &ExampleRuntime::get_prop_value);
  561. ADD_PROPERTY(PropertyInfo(Variant::INT, "prop_value"), "set_prop_value", "get_prop_value");
  562. }
  563. void ExampleRuntime::set_prop_value(int p_prop_value) {
  564. prop_value = p_prop_value;
  565. }
  566. int ExampleRuntime::get_prop_value() const {
  567. return prop_value;
  568. }
  569. ExampleRuntime::ExampleRuntime() {
  570. }
  571. ExampleRuntime::~ExampleRuntime() {
  572. }