example.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*************************************************************************/
  2. /* example.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "example.h"
  31. #include <godot_cpp/core/class_db.hpp>
  32. #include <godot_cpp/classes/global_constants.hpp>
  33. #include <godot_cpp/classes/label.hpp>
  34. #include <godot_cpp/variant/utility_functions.hpp>
  35. using namespace godot;
  36. ExampleRef::ExampleRef() {
  37. UtilityFunctions::print("ExampleRef created.");
  38. }
  39. ExampleRef::~ExampleRef() {
  40. UtilityFunctions::print("ExampleRef destroyed.");
  41. }
  42. void Example::_bind_methods() {
  43. // Methods.
  44. ClassDB::bind_method(D_METHOD("simple_func"), &Example::simple_func);
  45. ClassDB::bind_method(D_METHOD("simple_const_func"), &Example::simple_const_func);
  46. ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
  47. ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
  48. ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
  49. ClassDB::bind_method(D_METHOD("extended_ref_checks"), &Example::extended_ref_checks);
  50. ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
  51. ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
  52. {
  53. MethodInfo mi;
  54. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  55. mi.name = "varargs_func";
  56. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func", &Example::varargs_func, mi);
  57. }
  58. {
  59. MethodInfo mi;
  60. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  61. mi.name = "varargs_func_nv";
  62. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_nv", &Example::varargs_func_nv, mi);
  63. }
  64. {
  65. MethodInfo mi;
  66. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  67. mi.name = "varargs_func_void";
  68. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_void", &Example::varargs_func_void, mi);
  69. }
  70. // Properties.
  71. ADD_GROUP("Test group", "group_");
  72. ADD_SUBGROUP("Test subgroup", "group_subgroup_");
  73. ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
  74. ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
  75. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
  76. // Signals.
  77. ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
  78. ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
  79. // Constants.
  80. BIND_ENUM_CONSTANT(FIRST);
  81. BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
  82. BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
  83. }
  84. Example::Example() {
  85. UtilityFunctions::print("Constructor.");
  86. }
  87. Example::~Example() {
  88. UtilityFunctions::print("Destructor.");
  89. }
  90. // Methods.
  91. void Example::simple_func() {
  92. UtilityFunctions::print("Simple func called.");
  93. }
  94. void Example::simple_const_func() const {
  95. UtilityFunctions::print("Simple const func called.");
  96. }
  97. String Example::return_something(const String &base) {
  98. UtilityFunctions::print("Return something called.");
  99. return base;
  100. }
  101. Viewport *Example::return_something_const() const {
  102. UtilityFunctions::print("Return something const called.");
  103. if (is_inside_tree()) {
  104. Viewport *result = get_viewport();
  105. return result;
  106. }
  107. return nullptr;
  108. }
  109. ExampleRef *Example::return_extended_ref() const {
  110. return memnew(ExampleRef());
  111. }
  112. Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
  113. Ref<ExampleRef> ref;
  114. ref.instantiate();
  115. // TODO the returned value gets dereferenced too early and return a null object otherwise.
  116. ref->reference();
  117. UtilityFunctions::print("Example ref checks called with value: ", p_ref->get_instance_id(), ", returning value: ", ref->get_instance_id());
  118. return ref;
  119. }
  120. Variant Example::varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
  121. UtilityFunctions::print("Varargs (Variant return) called with ", String::num((double)arg_count), " arguments");
  122. return arg_count;
  123. }
  124. int Example::varargs_func_nv(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
  125. UtilityFunctions::print("Varargs (int return) called with ", String::num((double)arg_count), " arguments");
  126. return 42;
  127. }
  128. void Example::varargs_func_void(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
  129. UtilityFunctions::print("Varargs (no return) called with ", String::num((double)arg_count), " arguments");
  130. }
  131. void Example::emit_custom_signal(const String &name, int value) {
  132. emit_signal("custom_signal", name, value);
  133. }
  134. Array Example::test_array() const {
  135. Array arr;
  136. arr.resize(2);
  137. arr[0] = Variant(1);
  138. arr[1] = Variant(2);
  139. return arr;
  140. }
  141. Dictionary Example::test_dictionary() const {
  142. Dictionary dict;
  143. dict["hello"] = "world";
  144. dict["foo"] = "bar";
  145. return dict;
  146. }
  147. // Properties.
  148. void Example::set_custom_position(const Vector2 &pos) {
  149. custom_position = pos;
  150. }
  151. Vector2 Example::get_custom_position() const {
  152. return custom_position;
  153. }
  154. // Virtual function override.
  155. bool Example::_has_point(const Vector2 &point) const {
  156. Label *label = get_node<Label>("Label");
  157. label->set_text("Got point: " + Variant(point).stringify());
  158. return false;
  159. }