example.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. int Example::test_static(int p_a, int p_b) {
  43. return p_a + p_b;
  44. }
  45. void Example::test_static2() {
  46. UtilityFunctions::print(" void static");
  47. }
  48. int Example::def_args(int p_a, int p_b) {
  49. return p_a + p_b;
  50. }
  51. void Example::_notification(int p_what) {
  52. UtilityFunctions::print("Notification: ", String::num(p_what));
  53. }
  54. bool Example::_set(const StringName &p_name, const Variant &p_value) {
  55. if (p_name == StringName("property_from_list")) {
  56. property_from_list = p_value;
  57. return true;
  58. }
  59. return false;
  60. }
  61. bool Example::_get(const StringName &p_name, Variant &r_ret) const {
  62. if (p_name == StringName("property_from_list")) {
  63. r_ret = property_from_list;
  64. return true;
  65. }
  66. return false;
  67. }
  68. String Example::_to_string() const {
  69. return "[ GDExtension::Example <--> Instance ID:" + itos(get_instance_id()) + " ]";
  70. }
  71. void Example::_get_property_list(List<PropertyInfo> *p_list) const {
  72. p_list->push_back(PropertyInfo(Variant::VECTOR3, "property_from_list"));
  73. }
  74. bool Example::_property_can_revert(const StringName &p_name) const {
  75. if (p_name == StringName("property_from_list") && property_from_list != Vector3(42, 42, 42)) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. };
  81. bool Example::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  82. if (p_name == StringName("property_from_list")) {
  83. r_property = Vector3(42, 42, 42);
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. };
  89. void Example::_bind_methods() {
  90. // Methods.
  91. ClassDB::bind_method(D_METHOD("simple_func"), &Example::simple_func);
  92. ClassDB::bind_method(D_METHOD("simple_const_func"), &Example::simple_const_func);
  93. ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
  94. ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
  95. ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
  96. ClassDB::bind_method(D_METHOD("extended_ref_checks"), &Example::extended_ref_checks);
  97. ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
  98. ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
  99. ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
  100. ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
  101. ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
  102. {
  103. MethodInfo mi;
  104. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  105. mi.name = "varargs_func";
  106. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func", &Example::varargs_func, mi);
  107. }
  108. {
  109. MethodInfo mi;
  110. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  111. mi.name = "varargs_func_nv";
  112. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_nv", &Example::varargs_func_nv, mi);
  113. }
  114. {
  115. MethodInfo mi;
  116. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  117. mi.name = "varargs_func_void";
  118. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_void", &Example::varargs_func_void, mi);
  119. }
  120. // Properties.
  121. ADD_GROUP("Test group", "group_");
  122. ADD_SUBGROUP("Test subgroup", "group_subgroup_");
  123. ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
  124. ClassDB::bind_method(D_METHOD("get_v4"), &Example::get_v4);
  125. ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
  126. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
  127. // Signals.
  128. ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
  129. ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
  130. // Constants.
  131. BIND_ENUM_CONSTANT(FIRST);
  132. BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
  133. BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
  134. }
  135. Example::Example() {
  136. UtilityFunctions::print("Constructor.");
  137. }
  138. Example::~Example() {
  139. UtilityFunctions::print("Destructor.");
  140. }
  141. // Methods.
  142. void Example::simple_func() {
  143. UtilityFunctions::print(" Simple func called.");
  144. }
  145. void Example::simple_const_func() const {
  146. UtilityFunctions::print(" Simple const func called.");
  147. }
  148. String Example::return_something(const String &base) {
  149. UtilityFunctions::print(" Return something called.");
  150. return base;
  151. }
  152. Viewport *Example::return_something_const() const {
  153. UtilityFunctions::print(" Return something const called.");
  154. if (is_inside_tree()) {
  155. Viewport *result = get_viewport();
  156. return result;
  157. }
  158. return nullptr;
  159. }
  160. ExampleRef *Example::return_extended_ref() const {
  161. return memnew(ExampleRef());
  162. }
  163. Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
  164. Ref<ExampleRef> ref;
  165. ref.instantiate();
  166. // TODO the returned value gets dereferenced too early and return a null object otherwise.
  167. ref->reference();
  168. UtilityFunctions::print(" Example ref checks called with value: ", p_ref->get_instance_id(), ", returning value: ", ref->get_instance_id());
  169. return ref;
  170. }
  171. Variant Example::varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
  172. UtilityFunctions::print(" Varargs (Variant return) called with ", String::num((double)arg_count), " arguments");
  173. return arg_count;
  174. }
  175. int Example::varargs_func_nv(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
  176. UtilityFunctions::print(" Varargs (int return) called with ", String::num((double)arg_count), " arguments");
  177. return 42;
  178. }
  179. void Example::varargs_func_void(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
  180. UtilityFunctions::print(" Varargs (no return) called with ", String::num((double)arg_count), " arguments");
  181. }
  182. void Example::emit_custom_signal(const String &name, int value) {
  183. emit_signal("custom_signal", name, value);
  184. }
  185. Array Example::test_array() const {
  186. Array arr;
  187. arr.resize(2);
  188. arr[0] = Variant(1);
  189. arr[1] = Variant(2);
  190. return arr;
  191. }
  192. Dictionary Example::test_dictionary() const {
  193. Dictionary dict;
  194. dict["hello"] = "world";
  195. dict["foo"] = "bar";
  196. return dict;
  197. }
  198. // Properties.
  199. void Example::set_custom_position(const Vector2 &pos) {
  200. custom_position = pos;
  201. }
  202. Vector2 Example::get_custom_position() const {
  203. return custom_position;
  204. }
  205. Vector4 Example::get_v4() const {
  206. return Vector4(1.2, 3.4, 5.6, 7.8);
  207. }
  208. // Virtual function override.
  209. bool Example::_has_point(const Vector2 &point) const {
  210. Label *label = get_node<Label>("Label");
  211. label->set_text("Got point: " + Variant(point).stringify());
  212. return false;
  213. }