custom_modules_in_c++.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 the
  32. Godot source tree (C:\\\\godot refers to wherever the Godot sources are
  33. 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. .. code:: cpp
  41. /* sumator.h */
  42. #ifndef SUMATOR_H
  43. #define SUMATOR_H
  44. #include "reference.h"
  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. And then the cpp file.
  58. .. code:: cpp
  59. /* sumator.cpp */
  60. #include "sumator.h"
  61. void Sumator::add(int value) {
  62. count+=value;
  63. }
  64. void Sumator::reset() {
  65. count=0;
  66. }
  67. int Sumator::get_total() const {
  68. return count;
  69. }
  70. void Sumator::_bind_methods() {
  71. ObjectTypeDB::bind_method("add",&Sumator::add);
  72. ObjectTypeDB::bind_method("reset",&Sumator::reset);
  73. ObjectTypeDB::bind_method("get_total",&Sumator::get_total);
  74. }
  75. Sumator::Sumator() {
  76. count=0;
  77. }
  78. Then, the new class needs to be registered somehow, so two more files
  79. need to be created:
  80. ::
  81. register_types.h
  82. register_types.cpp
  83. With the following contents
  84. .. code:: cpp
  85. /* register_types.h */
  86. void register_sumator_types();
  87. void unregister_sumator_types();
  88. /* yes, the word in the middle must be the same as the module folder name */
  89. .. code:: cpp
  90. /* register_types.cpp */
  91. #include "register_types.h"
  92. #include "object_type_db.h"
  93. #include "sumator.h"
  94. void register_sumator_types() {
  95. ObjectTypeDB::register_type<Sumator>();
  96. }
  97. void unregister_sumator_types() {
  98. //nothing to do here
  99. }
  100. Next, we need to create a SCsub so the build system compiles this
  101. module:
  102. .. code:: python
  103. # SCsub
  104. Import('env')
  105. env.add_source_files(env.modules_sources,"*.cpp") # just add all cpp files to the build
  106. And finally, the configuration file for the module, this is a simple
  107. python script that must be named 'config.py'
  108. .. code:: python
  109. # config.py
  110. def can_build(platform):
  111. return True
  112. def configure(env):
  113. pass
  114. The module is asked if it's ok to build for the specific platform (in
  115. this case, True means it will build for every platform).
  116. The second function allows to customize the build process for the
  117. module, like adding special compiler flags, options etc. (This can be
  118. done in SCSub, but configure(env) is called at a previous stage). If
  119. unsure, just ignore this.
  120. And that's it. Hope it was not too complex! Your module should look like
  121. this:
  122. ::
  123. godot/modules/sumator/config.py
  124. godot/modules/sumator/sumator.h
  125. godot/modules/sumator/sumator.cpp
  126. godot/modules/sumator/register_types.h
  127. godot/modules/sumator/register_types.cpp
  128. godot/modules/sumator/SCsub
  129. You can then zip it and share the module with everyone else. When
  130. building for every platform (instructions in the previous section), your
  131. module will be included.
  132. Using the module
  133. ----------------
  134. Using your newly created module is very easy, from any script you can
  135. do:
  136. .. code:: python
  137. var s = Sumator.new()
  138. s.add(10)
  139. s.add(20)
  140. s.add(30)
  141. print( s.get_total() )
  142. s.reset()
  143. And the output will be ``60``.
  144. Summing up
  145. ----------
  146. As you see, it's really easy to develop Godot in C++. Just write your
  147. stuff normally and remember to:
  148. - use ``OBJ_TYPE`` macro for inheritance, so Godot can wrap it
  149. - use ``_bind_methods`` to bind your functions to scripting, and to
  150. allow them to work as callbacks for signals.
  151. But this is not all, depending what you do, you will be greeted with
  152. some surprises.
  153. - If you inherit from [[API:Node]] (or any derived node type, such as
  154. Sprite), your new class will appear in the editor, in the inheritance
  155. tree in the "Add Node" dialog.
  156. - If you inherit from [[API:Resource]], it will appear int the resource
  157. list, and all the exposed properties can be serialized when
  158. saved/loaded.
  159. - By this same logic, you can extend the Editor and almost any area of
  160. the engine.