init.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*************************************************************************/
  2. /* init.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 <Godot.hpp>
  31. #include <Reference.hpp>
  32. using namespace godot;
  33. class SimpleClass : public Reference {
  34. GODOT_CLASS(SimpleClass, Reference);
  35. public:
  36. SimpleClass() {}
  37. /** `_init` must exist as it is called by Godot. */
  38. void _init() {
  39. _name = String("SimpleClass");
  40. _value = 0;
  41. }
  42. void test_void_method() {
  43. Godot::print("This is test");
  44. }
  45. Variant method(Variant arg) {
  46. Variant ret;
  47. ret = arg;
  48. return ret;
  49. }
  50. static void _register_methods() {
  51. register_method("method", &SimpleClass::method);
  52. /**
  53. * The line below is equivalent to the following GDScript export:
  54. * export var _name = "SimpleClass"
  55. **/
  56. register_property<SimpleClass, String>("name", &SimpleClass::_name, String("SimpleClass"));
  57. /** Alternatively, with getter and setter methods: */
  58. register_property<SimpleClass, int>("value", &SimpleClass::set_value, &SimpleClass::get_value, 0);
  59. /** Registering a signal: **/
  60. register_signal<SimpleClass>("signal_name0"); // windows: error C2668: 'godot::register_signal': ambiguous call to overloaded function
  61. register_signal<SimpleClass>("signal_name1", "string_argument", GODOT_VARIANT_TYPE_STRING);
  62. }
  63. String _name;
  64. int _value;
  65. void set_value(int p_value) {
  66. _value = p_value;
  67. }
  68. int get_value() const {
  69. return _value;
  70. }
  71. };
  72. /** GDNative Initialize **/
  73. extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
  74. godot::Godot::gdnative_init(o);
  75. }
  76. /** GDNative Terminate **/
  77. extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
  78. godot::Godot::gdnative_terminate(o);
  79. }
  80. /** NativeScript Initialize **/
  81. extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
  82. godot::Godot::nativescript_init(handle);
  83. godot::register_class<SimpleClass>();
  84. }