custom_modules_in_c++.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. .. _doc_custom_modules_in_c++:
  2. Custom modules in C++
  3. =====================
  4. Modules
  5. -------
  6. Godot allows extending the engine in a modular way. New modules can be
  7. created and then enabled/disabled. This allows for adding new engine
  8. functionality at every level without modifying the core, which can be
  9. split for use and reuse in different modules.
  10. Modules are located in them modules/ subdirectory of the build system.
  11. By default, two modules exist, GDScript (which, yes it's not part of the
  12. core engine), and the GridMap. As many new modules as desired can be
  13. created and combined, and the SCons build system will take care of it
  14. transparently.
  15. What for?
  16. ---------
  17. While it's recommended that most of a game is written in scripting (as
  18. it is an enormous time saver), it's perfectly possible to use C++
  19. instead. Adding C++ modules can be useful in the following scenarios:
  20. - Binding an external library to Godot (like Bullet, Physx, FMOD, etc).
  21. - Optimize critical parts of a game.
  22. - Adding new functionality to the engine and/or editor.
  23. - Porting an existing game.
  24. - Write a whole, new game in C++ because you can't live without C++.
  25. Creating a new module
  26. ---------------------
  27. Before creating a module, make sure to download the source code of Godot
  28. and manage to compile it. There are tutorials in the wiki for this.
  29. To create a new module, the first step is creating a directory inside
  30. modules. If you want to maintain the module separately, you can checkout
  31. a different VCS into modules and use it.
  32. The example module will be called "sumator", and is placed inside the
  33. Godot source tree (C:\\\\godot refers to wherever the Godot sources are
  34. located):
  35. ::
  36. c:\\godot> cd modules
  37. c:\\godot\\modules> mkdir sumator
  38. c:\\godot\\modules> cd sumator
  39. c:\\godot\\modules\\sumator>
  40. Inside we will create a simple sumator class:
  41. .. code:: cpp
  42. /* sumator.h */
  43. #ifndef SUMATOR_H
  44. #define SUMATOR_H
  45. #include "reference.h"
  46. class Sumator : public Reference {
  47. OBJ_TYPE(Sumator,Reference);
  48. int count;
  49. protected:
  50. static void _bind_methods();
  51. public:
  52. void add(int value);
  53. void reset();
  54. int get_total() const;
  55. Sumator();
  56. };
  57. #endif
  58. And then the cpp file.
  59. .. code:: cpp
  60. /* sumator.cpp */
  61. #include "sumator.h"
  62. void Sumator::add(int value) {
  63. count+=value;
  64. }
  65. void Sumator::reset() {
  66. count=0;
  67. }
  68. int Sumator::get_total() const {
  69. return count;
  70. }
  71. void Sumator::_bind_methods() {
  72. ObjectTypeDB::bind_method("add",&Sumator::add);
  73. ObjectTypeDB::bind_method("reset",&Sumator::reset);
  74. ObjectTypeDB::bind_method("get_total",&Sumator::get_total);
  75. }
  76. Sumator::Sumator() {
  77. count=0;
  78. }
  79. Then, the new class needs to be registered somehow, so two more files
  80. need to be created:
  81. ::
  82. register_types.h
  83. register_types.cpp
  84. With the following contents
  85. .. code:: cpp
  86. /* register_types.h */
  87. void register_sumator_types();
  88. void unregister_sumator_types();
  89. /* yes, the word in the middle must be the same as the module folder name */
  90. .. code:: cpp
  91. /* register_types.cpp */
  92. #include "register_types.h"
  93. #include "object_type_db.h"
  94. #include "sumator.h"
  95. void register_sumator_types() {
  96. ObjectTypeDB::register_type<Sumator>();
  97. }
  98. void unregister_sumator_types() {
  99. //nothing to do here
  100. }
  101. Next, we need to create a SCsub so the build system compiles this
  102. module:
  103. .. code:: python
  104. # SCsub
  105. Import('env')
  106. env.add_source_files(env.modules_sources,"*.cpp") # just add all cpp files to the build
  107. And finally, the configuration file for the module, this is a simple
  108. python script that must be named 'config.py'
  109. .. code:: python
  110. # config.py
  111. def can_build(platform):
  112. return True
  113. def configure(env):
  114. pass
  115. The module is asked if it's ok to build for the specific platform (in
  116. this case, True means it will build for every platform).
  117. The second function allows to customize the build process for the
  118. module, like adding special compiler flags, options etc. (This can be
  119. done in SCSub, but configure(env) is called at a previous stage). If
  120. unsure, just ignore this.
  121. And that's it. Hope it was not too complex! Your module should look like
  122. this:
  123. ::
  124. godot/modules/sumator/config.py
  125. godot/modules/sumator/sumator.h
  126. godot/modules/sumator/sumator.cpp
  127. godot/modules/sumator/register_types.h
  128. godot/modules/sumator/register_types.cpp
  129. godot/modules/sumator/SCsub
  130. You can then zip it and share the module with everyone else. When
  131. building for every platform (instructions in the previous section), your
  132. module will be included.
  133. Using the module
  134. ----------------
  135. Using your newly created module is very easy, from any script you can
  136. do:
  137. .. code:: python
  138. var s = Sumator.new()
  139. s.add(10)
  140. s.add(20)
  141. s.add(30)
  142. print( s.get_total() )
  143. s.reset()
  144. And the output will be ``60``.
  145. Summing up
  146. ----------
  147. As you see, it's really easy to develop Godot in C++. Just write your
  148. stuff normally and remember to:
  149. - use ``OBJ_TYPE`` macro for inheritance, so Godot can wrap it
  150. - use ``_bind_methods`` to bind your functions to scripting, and to
  151. allow them to work as callbacks for signals.
  152. But this is not all, depending what you do, you will be greeted with
  153. some surprises.
  154. - If you inherit from :ref:`class_Node` (or any derived node type, such as
  155. Sprite), your new class will appear in the editor, in the inheritance
  156. tree in the "Add Node" dialog.
  157. - If you inherit from :ref:`class_Resource`, it will appear int the resource
  158. list, and all the exposed properties can be serialized when
  159. saved/loaded.
  160. - By this same logic, you can extend the Editor and almost any area of
  161. the engine.