Custom modules in C++ ===================== Modules ------- Godot allows extending the engine in a modular way. New modules can be created and then enabled/disabled. This allows for adding new engine functionality at every level without modifying the core, which can be split for use and reuse in different modules. Modules are located in the `modules/` subdirectory of the build system. By default, dozens of modules are enabled, such as GDScript (which, yes, is not part of the base engine), the Mono runtime, a regular expressions module, and others. As many new modules as desired can be created and combined. The SCons build system will take care of it transparently. What for? --------- While it's recommended that most of a game be written in scripting (as it is an enormous time saver), it's perfectly possible to use C++ instead. Adding C++ modules can be useful in the following scenarios: - Binding an external library to Godot (like PhysX, FMOD, etc). - Optimize critical parts of a game. - Adding new functionality to the engine and/or editor. - Porting an existing game. - Write a whole, new game in C++ because you can't live without C++. Creating a new module --------------------- Before creating a module, make sure to `download the source code of Godot and compile it (); } void unregister_summator_types() { // Nothing to do here in this example. } ``` Next, we need to create a `SCsub` file so the build system compiles this module: ``` # SCsub Import('env') env.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build ``` With multiple sources, you can also add each file individually to a Python string list: ``` src_list = ["summator.cpp", "other.cpp", "etc.cpp"] env.add_source_files(env.modules_sources, src_list) ``` This allows for powerful possibilities using Python to construct the file list using loops and logic statements. Look at some modules that ship with Godot by default for examples. To add include directories for the compiler to look at you can append it to the environment's paths: ``` env.Append(CPPPATH=["mylib/include"]) # this is a relative path env.Append(CPPPATH=["#myotherlib/include"]) # this is an 'absolute' path ``` If you want to add custom compiler flags when building your module, you need to clone `env` first, so it won't add those flags to whole Godot build (which can cause errors). Example `SCsub` with custom flags: ``` # SCsub Import('env') module_env = env.Clone() module_env.add_source_files(env.modules_sources, "*.cpp") # Append CCFLAGS flags for both C and C++ code. module_env.Append(CCFLAGS=['-O2']) # If you need to, you can: # - Append CFLAGS for C code only. # - Append CXXFLAGS for C++ code only. ``` And finally, the configuration file for the module, this is a simple python script that must be named `config.py`: ``` # config.py def can_build(env, platform): return True def configure(env): pass ``` The module is asked if it's OK to build for the specific platform (in this case, `True` means it will build for every platform). And that's it. Hope it was not too complex! Your module should look like this: ``` godot/modules/summator/config.py godot/modules/summator/summator.h godot/modules/summator/summator.cpp godot/modules/summator/register_types.h godot/modules/summator/register_types.cpp godot/modules/summator/SCsub ``` You can then zip it and share the module with everyone else. When building for every platform (instructions in the previous sections), your module will be included. Note: There is a parameter limit of 5 in C++ modules for things such as subclasses. This can be raised to 13 by including the header file `core/method_bind_ext.gen.inc`. Using the module ---------------- You can now use your newly created module from any script: ``` var s = Summator.new() s.add(10) s.add(20) s.add(30) print(s.get_total()) s.reset() ``` The output will be `60`. See also: The previous Summator example is great for small, custom modules, but what if you want to use a larger, external library? Refer to `doc_binding_to_external_libraries` for details about binding to external libraries. Warning: If your module is meant to be accessed from the running project (not just from the editor), you must also recompile every export template you plan to use, then specify the path to the custom template in each export preset. Otherwise, you'll get errors when running the project as the module isn't compiled in the export template. See the `Compiling ..." to include in what will be committed) doc/classes/MyClass2D.xml doc/classes/MyClass4D.xml doc/classes/MyClass5D.xml doc/classes/MyClass6D.xml ... ``` 3. Now we can generate the documentation: We can do this via running Godot's doctool i.e. `godot --doctool --doctool . ``` Now if you go to the `godot/modules/summator/doc_classes` folder, you will see that it contains a `Summator.xml` file, or any other classes, that you referenced in your `get_doc_classes` function. Edit the file(s) following `doc_updating_the_class_reference` and recompile the engine. Once the compilation process is finished, the docs will become accessible within the engine's built-in documentation system. In order to keep documentation up-to-date, all you'll have to do is simply modify one of the XML files and recompile the engine from now on. If you change your module's API, you can also re-extract the docs, they will contain the things that you previously added. Of course if you point it to your godot folder, make sure you don't lose work by extracting older docs from an older engine build on top of the newer ones. Note that if you don't have write access rights to your supplied `