custom_modules_in_c++.rst 5.8 KB

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