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 to Godot.
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.
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" (godot/modules/summator
).
Inside we will create a summator class:
/* summator.h */
#ifndef SUMMATOR_H
#define SUMMATOR_H
#include "core/object/ref_counted.h"
class Summator : public RefCounted {
GDCLASS(Summator, RefCounted);
int count;
protected:
static void _bind_methods();
public:
void add(int p_value);
void reset();
int get_total() const;
Summator();
};
#endif // SUMMATOR_H
And then the cpp file.
/* summator.cpp */
#include "summator.h"
void Summator::add(int p_value) {
count += p_value;
}
void Summator::reset() {
count = 0;
}
int Summator::get_total() const {
return count;
}
void Summator::_bind_methods() {
ClassDB::bind_method(D_METHOD("add", "value"), &Summator::add);
ClassDB::bind_method(D_METHOD("reset"), &Summator::reset);
ClassDB::bind_method(D_METHOD("get_total"), &Summator::get_total);
}
Summator::Summator() {
count = 0;
}
Then, the new class needs to be registered somehow, so two more files need to be created:
register_types.h
register_types.cpp
Important
These files must be in the top-level folder of your module (next to your
SCsub
and config.py
files) for the module to be registered properly.
These files should contain the following:
/* register_types.h */
#include "modules/register_module_types.h"
void initialize_summator_module(ModuleInitializationLevel p_level);
void uninitialize_summator_module(ModuleInitializationLevel p_level);
/* yes, the word in the middle must be the same as the module folder name */
/* register_types.cpp */
#include "register_types.h"
#include "core/object/class_db.h"
#include "summator.h"
void initialize_summator_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
ClassDB::register_class<Summator>();
}
void uninitialize_summator_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
// 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()
modu