How to add a custom editor plugin via modules using C++

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Xrayez

So I’ve gone through this doc article on how to create a basic editor plugin using GDScript and have made a few simple plugins. I’ve also developed a custom module in C++. What I was wondering is whether it’s possible to write another editor plugin but using C++ instead without modifying the Godot’s source.

It seems like I’ve already figured out how to write an editor plugin by looking how other built-in plugins are implemented as in here.

In editor_node.cpp I discovered that plugins are registered during initialization of EditorNode, for instance:

add_editor_plugin(memnew(VisualShaderEditorPlugin(this)));

So it appears to me that I could use EditorNode singleton and add my plugin like this within a module:

EditorNode *en = EditorNode::get_singleton()
en->add_editor_plugin(memnew(AnlNoiseEditorPlugin(en))

The problem is that I don’t know when and where to do this. I’ve tried doing so in register_types:

void register_my_module_types() {

    ClassDB::register_class<MyModule>();

    EditorNode *en = EditorNode::get_singleton();
    en->add_editor_plugin(memnew(MyModuleEditorPlugin(en)));
}

And en is NULL here because it seems like EditorNode is not even initialized yet, so I wonder what to do now.

:bust_in_silhouette: Reply From: Xrayez

The solution is fairly straightforward, I just had to use EditorPlugins to add a plugin:

void register_my_module_types() {

    ClassDB::register_class<MyModule>();

#ifdef TOOLS_ENABLED
    EditorPlugins::add_by_type<MyModuleEditorPlugin>();
#endif
}