example.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*************************************************************************/
  2. /* example.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. // Properties.
  59. ADD_GROUP("Test group", "group_");
  60. ADD_SUBGROUP("Test subgroup", "group_subgroup_");
  61. ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
  62. ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
  63. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
  64. // Signals.
  65. ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
  66. ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
  67. // Constants.
  68. BIND_ENUM_CONSTANT(FIRST);
  69. BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
  70. BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
  71. }
  72. Example::Example() {
  73. UtilityFunctions::print("Constructor.");
  74. }
  75. Example::~Example() {
  76. UtilityFunctions::print("Destructor.");
  77. }
  78. // Methods.
  79. void Example::simple_func() {
  80. UtilityFunctions::print("Simple func called.");
  81. }
  82. void Example::simple_const_func() const {
  83. UtilityFunctions::print("Simple const func called.");
  84. }
  85. String Example::return_something(const String &base) {
  86. UtilityFunctions::print("Return something called.");
  87. return base;
  88. }
  89. Viewport *Example::return_something_const() const {
  90. UtilityFunctions::print("Return something const called.");
  91. if (is_inside_tree()) {
  92. Viewport *result = get_viewport();
  93. return result;
  94. }
  95. return nullptr;
  96. }
  97. ExampleRef *Example::return_extended_ref() const {
  98. return memnew(ExampleRef());
  99. }
  100. Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
  101. Ref<ExampleRef> ref;
  102. ref.instantiate();
  103. // TODO the returned value gets dereferenced too early and return a null object otherwise.
  104. ref->reference();
  105. UtilityFunctions::print("Example ref checks called with value: ", p_ref->get_instance_id(), ", returning value: ", ref->get_instance_id());
  106. return ref;
  107. }
  108. Variant Example::varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
  109. UtilityFunctions::print("Varargs called with ", String::num((double)arg_count), " arguments");
  110. return arg_count;
  111. }
  112. void Example::emit_custom_signal(const String &name, int value) {
  113. emit_signal("custom_signal", name, value);
  114. }
  115. Array Example::test_array() const {
  116. Array arr;
  117. arr.resize(2);
  118. arr[0] = Variant(1);
  119. arr[1] = Variant(2);
  120. return arr;
  121. }
  122. Dictionary Example::test_dictionary() const {
  123. Dictionary dict;
  124. dict["hello"] = "world";
  125. dict["foo"] = "bar";
  126. return dict;
  127. }
  128. // Properties.
  129. void Example::set_custom_position(const Vector2 &pos) {
  130. custom_position = pos;
  131. }
  132. Vector2 Example::get_custom_position() const {
  133. return custom_position;
  134. }
  135. // Virtual function override.
  136. bool Example::_has_point(const Vector2 &point) const {
  137. Label *label = get_node<Label>("Label");
  138. label->set_text("Got point: " + Variant(point).stringify());
  139. return false;
  140. }