Custom modules in C++

    Modules are located in the subdirectory of the build system. By default, many different modules exist, 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, and 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++.

    Before creating a module, make sure to download the source code of Godot and manage to compile it. There are tutorials in the documentation for this.

    To create a new module, the first step is creating a directory inside modules/. If you want to maintain the module separately, you can checkout a different VCS into modules and use it.

    The example module will be called “summator”, and is placed inside the Godot source tree (C:\godot refers to wherever the Godot sources are located):

    Inside we will create a simple summator class:

    1. /* summator.h */
    2. #ifndef SUMMATOR_H
    3. #define SUMMATOR_H
    4. #include "core/reference.h"
    5. class Summator : public Reference {
    6. GDCLASS(Summator, Reference);
    7. int count;
    8. protected:
    9. static void _bind_methods();
    10. public:
    11. void add(int p_value);
    12. void reset();
    13. int get_total() const;
    14. Summator();
    15. };
    16. #endif // SUMMATOR_H

    And then the cpp file.

    1. /* summator.cpp */
    2. #include "summator.h"
    3. void Summator::add(int p_value) {
    4. count += p_value;
    5. }
    6. void Summator::reset() {
    7. count = 0;
    8. }
    9. int Summator::get_total() const {
    10. return count;
    11. }
    12. void Summator::_bind_methods() {
    13. ClassDB::bind_method(D_METHOD("add", "value"), &Summator::add);
    14. ClassDB::bind_method(D_METHOD("reset"), &Summator::reset);
    15. ClassDB::bind_method(D_METHOD("get_total"), &Summator::get_total);
    16. }
    17. Summator::Summator() {
    18. }

    Then, the new class needs to be registered somehow, so two more files need to be created:

    1. register_types.h
    2. register_types.cpp

    With the following contents:

    1. /* register_types.h */
    2. void register_summator_types();
    3. void unregister_summator_types();
    4. /* yes, the word in the middle must be the same as the module folder name */
    1. /* register_types.cpp */
    2. #include "register_types.h"
    3. #include "core/class_db.h"
    4. #include "summator.h"
    5. void register_summator_types() {
    6. ClassDB::register_class<Summator>();
    7. }
    8. void unregister_summator_types() {
    9. // Nothing to do here in this example.
    10. }

    Next, we need to create a SCsub file so the build system compiles this module:

    With multiple sources, you can also add each file individually to a Python string list:

    1. src_list = ["summator.cpp", "other.cpp", "etc.cpp"]
    2. 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 of the other 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:

    1. env.Append(CPPPATH=["mylib/include"]) # this is a relative path
    2. env.Append(CPPPATH=["#myotherlib/include"]) # this is an 'absolute' path
    1. # SCsub
    2. Import('env')
    3. module_env = env.Clone()
    4. module_env.add_source_files(env.modules_sources, "*.cpp")
    5. module_env.Append(CCFLAGS=['-O2']) # Flags for C and C++ code
    6. module_env.Append(CXXFLAGS=['-std=c++11']) # Flags for C++ code only

    And finally, the configuration file for the module, this is a simple python script that must be named config.py:

    1. # config.py
    2. def can_build(env, platform):
    3. return True
    4. def configure(env):
    5. 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:

    1. godot/modules/summator/config.py
    2. godot/modules/summator/summator.h
    3. godot/modules/summator/summator.cpp
    4. godot/modules/summator/register_types.h
    5. godot/modules/summator/register_types.cpp
    6. 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.

    Using the module

    You can now use your newly created module from any script:

    And 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 for details about binding to external libraries.

    So far we defined a clean and simple SCsub that allows us to add the sources of our new module as part of the Godot binary.

    This static approach is fine when we want to build a release version of our game given we want all the modules in a single binary.

    However the trade-off is every single change means a full recompilation of the game. Even if SCons is able to detect and recompile only the file that have changed, finding such files and eventually linking the final binary is a long and costly part.

    The solution to avoid such a cost is to build our own module as a shared library that will be dynamically loaded when starting our game’s binary.

    1. # SCsub
    2. Import('env')
    3. sources = [
    4. "register_types.cpp",
    5. "summator.cpp"
    6. ]
    7. # First, create a custom env for the shared library.
    8. module_env = env.Clone()
    9. module_env.Append(CCFLAGS=['-fPIC']) # Needed to compile shared library
    10. module_env['LIBS'] = []
    11. # Now define the shared library. Note that by default it would be built
    12. # into the module's folder, however it's better to output it into `bin`
    13. # next to the Godot binary.
    14. shared_lib = module_env.SharedLibrary(target='#bin/summator', source=sources)
    15. # Finally notify the main env it has our shared lirary as a new dependency.
    16. # To do so, SCons wants the name of the lib with it custom suffixes
    17. # We pass this along with the directory of our library to the main env.
    18. shared_lib_shim = shared_lib[0].name.rsplit('.', 1)[0]
    19. env.Append(LIBS=[shared_lib_shim])
    20. env.Append(LIBPATH=['#bin'])

    Once compiled, we should end up with a bin directory containing both the godot* binary and our libsummator*.so. However given the .so is not in a standard directory (like /usr/lib), we have to help our binary find it during runtime with the LD_LIBRARY_PATH environ variable:

    1. user@host:~/godot$ export LD_LIBRARY_PATH=`pwd`/bin/
    2. user@host:~/godot$ ./bin/godot*

    On top of that, it would be nice to be able to select whether to compile our module as shared library (for development) or as a part of the Godot binary (for release). To do that we can define a custom flag to be passed to SCons using the ARGUMENT command:

    1. # SCsub
    2. Import('env')
    3. sources = [
    4. "register_types.cpp",
    5. "summator.cpp"
    6. ]
    7. module_env = env.Clone()
    8. module_env.Append(CCFLAGS=['-O2'])
    9. module_env.Append(CXXFLAGS=['-std=c++11'])
    10. if ARGUMENTS.get('summator_shared', 'no') == 'yes':
    11. # Shared lib compilation
    12. module_env.Append(CCFLAGS=['-fPIC'])
    13. module_env['LIBS'] = []
    14. shared_lib = module_env.SharedLibrary(target='#bin/summator', source=sources)
    15. shared_lib_shim = shared_lib[0].name.rsplit('.', 1)[0]
    16. env.Append(LIBS=[shared_lib_shim])
    17. env.Append(LIBPATH=['#bin'])
    18. else:
    19. # Static compilation
    20. module_env.add_source_files(env.modules_sources, sources)

    Now by default scons command will build our module as part of Godot’s binary and as a shared library when passing summator_shared=yes.

    Finally you can even speedup build further by explicitly specifying your shared module as target in the scons command:

    1. user@host:~/godot$ scons summator_shared=yes platform=x11 bin/libsummator.x11.tools.64.so

    Writing custom documentation

    Writing documentation may seem like a boring task, but it is highly recommended to document your newly created module in order to make it easier for users to benefit from it. Not to mention that the code you’ve written one year ago may become indistinguishable from the code that was written by someone else, so be kind to your future self!

    There are several steps in order to setup custom docs for the module:

    1. Make a new directory in the root of the module. The directory name can be anything, but we’ll be using the doc_classes name throughout this section.

    2. Append the following code snippet to config.py:

      1. def get_doc_classes():
      2. return [
      3. "ClassName",
      4. ]
      5. def get_doc_path():
      6. return "doc_classes"

    The get_doc_classes() method is necessary for the build system to know which documentation classes of the module must be merged, since the module may contain several classes. Replace ClassName with the name of the class you want to write documentation for. If you need docs for more than one class, append those as well.

    The get_doc_path() method is used by the build system to determine the location of the docs. In our case, they will be located in the doc_classes directory.

    1. Run command:

    This will dump the engine API reference to the given <path> in XML format. Notice that you’ll need to configure your PATH to locate Godot’s executable, and make sure that you have write access rights. If not, you might encounter an error similar to the following:

    1. ERROR: Can't write doc file: docs/doc/classes/@GDScript.xml
    2. At: editor/doc/doc_data.cpp:956
    1. Get generated doc file from godot/doc/classes/ClassName.xml
    2. Copy this file to doc_classes, optionally edit it, then compile the engine.

    The build system will fetch the documentation files from the doc_classes directory and merge them with the base types. 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 ClassName.xml files and recompile the engine from now on.

    • use GDCLASS macro for inheritance, so Godot can wrap it
    • use to bind your functions to scripting, and to allow them to work as callbacks for signals.

    But this is not all, depending what you do, you will be greeted with some (hopefully positive) surprises.

    • If you inherit from Node (or any derived node type, such as Sprite), your new class will appear in the editor, in the inheritance tree in the “Add Node” dialog.
    • If you inherit from , it will appear in the resource list, and all the exposed properties can be serialized when saved/loaded.