Módulos personalizados em C++

Módulos

O Godot permite estender a engine de uma forma modular. Novos módulos podem ser criados e então habilitado/desativado. Isto permite adicionar novas funcionalidades no mesmo nível da engine sem modificá-la, que pode ser separada para uso e reutilização em diferentes módulos.

Módulos são localizados no subdiretório modules/ do sistema de compilação. Por padrão, dezenas de módulos são habilitados, como GDScript (que, sim, não faz parte da engine básica), o tempo de execução (runtime) do Mono, um módulo de expressões regulares, dentre outros. Podem ser criados e combinados quantos módulos novos desejar. O sistema de compilação SCons vai cuidar disso de forma transparente.

Para que serve?

Embora seja recomendado que a maior parte do jogo seja escrito em script (já que economiza muito tempo), é perfeitamente possível usar C++ em seu lugar. Adicionar módulos C++ pode ser útil nos seguintes cenários:

  • Vincular uma biblioteca externa no Godot (como PhysX, FMOD, etc).

  • Otimizar partes críticas de um jogo.

  • Adicionar novas funcionalidades para a engine e/ou editor.

  • Portar um jogo existente.

  • Escrever um jogo totalmente novo em C++ porque você não pode viver sem C++.

Criando um novo módulo

Antes de criar um módulo, certifique-se de baixar o código fonte do Godot e compilá-lo.

Para criar um módulo, a primeira etapa é criar um diretório dentro de modules/. Se você deseja manter o módulo separado, você pode criar um checkout de um VCS diferente em modules/ e usá-lo.

The example module will be called "summator" (godot/modules/summator). Inside we will create a simple summator class:

/* summator.h */

#ifndef SUMMATOR_H
#define SUMMATOR_H

#include "core/reference.h"

class Summator : public Reference {
    GDCLASS(Summator, Reference);

    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;
}

Então, a nova classe precisa ser registrada de alguma forma, então mais dois arquivos precisam ser criados:

register_types.h
register_types.cpp

Importante

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.

Esses arquivos devem conter o seguinte:

/* register_types.h */

void register_summator_types();
void unregister_summator_types();
/* yes, the word in the middle must be the same as the module folder name */
/* register_types.cpp */

#include "register_types.h"

#include "core/class_db.h"
#include "summator.h"

void register_summator_types() {
    ClassDB::register_class<Summator>();
}

void unregister_summator_types() {
   // Nothing to do here in this example.
}

Em seguida, precisamos criar um arquivo``SCsub`` para que o sistema de compilação compile o módulo:

# SCsub

Import('env')

env.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build

Com múltiplas fontes, você também pode adicionar cada arquivo individualmente à uma lista de strings Python:

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.

E finalmente, o arquivo de configuração para o módulo, é um simples script python que deve ser nomeado 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).

E é isso. Espero que não tenha sido complexo demais! Seu módulo deveria ser parecer assim:

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

Você pode zipar (compactar em ZIP) e compartilhar o módulo com todo mundo. Quando gerando para cada plataforma (instruções na seção anterior), seu módulo deve ser incluído.

Nota

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.

Usando o módulo

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.

Ver também

The previous Summator example is great for small, custom modules, but what if you want to use a larger, external library? Refer to Binding de bibliotecas externas for details about binding to external libraries.

Aviso

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 pages for more information.

Compiling a module externally

Compiling a module involves moving the module's sources directly under the engine's modules/ directory. While this is the most straightforward way to compile a module, there are a couple of reasons as to why this might not be a practical thing to do:

  1. Having to manually copy modules sources every time you want to compile the engine with or without the module, or taking additional steps needed to manually disable a module during compilation with a build option similar to module_summator_enabled=no. Creating symbolic links may also be a solution, but you may additionally need to overcome OS restrictions like needing the symbolic link privilege if doing this via script.

  2. Depending on whether you have to work with the engine's source code, the module files added directly to modules/ changes the working tree to the point where using a VCS (like git) proves to be cumbersome as you need to make sure that only the engine-related code is committed by filtering changes.

So if you feel like the independent structure of custom modules is needed, lets take our "summator" module and move it to the engine's parent directory:

mkdir ../modules
mv modules/summator ../modules

Compile the engine with our module by providing custom_modules build option which accepts a comma-separated list of directory paths containing custom C++ modules, similar to the following:

scons custom_modules=../modules

The build system shall detect all modules under the ../modules directory and compile them accordingly, including our "summator" module.

Aviso

Any path passed to custom_modules will be converted to an absolute path internally as a way to distinguish between custom and built-in modules. It means that things like generating module documentation may rely on a specific path structure on your machine.

Melhorando o sistema de build para desenvolvimento

Aviso

This shared library support is not designed to support distributing a module to other users without recompiling the engine. For that purpose, use GDNative instead.

Até agora, definimos um SCsub limpo que nos permite acrescentar as fontes de nosso novo módulo como parte do binário Godot.

Essa estratégia estática é ótima quando nós queremos gerar uma versão de lançamento de nosso jogo, dado que queremos todos os módulos em um binário único.

However, the trade-off is that every single change requires a full recompilation of the game. Even though SCons is able to detect and recompile only the file that was changed, finding such files and eventually linking the final binary takes a long time.

A solução para evitar tal custo é gerar build de nossos próprios módulos como uma biblioteca compartilhada que será dinamicamente carregada quando iniciar o binário de nosso jogo.

# SCsub

Import('env')

sources = [
    "register_types.cpp",
    "summator.cpp"
]

# First, create a custom env for the shared library.
module_env = env.Clone()

# Position-independent code is required for a shared library.
module_env.Append(CCFLAGS=['-fPIC'])

# Don't inject Godot's dependencies into our shared library.
module_env['LIBS'] = []

# Define the shared library. By default, it would be built in the module's
# folder, however it's better to output it into `bin` next to the
# Godot binary.
shared_lib = module_env.SharedLibrary(target='#bin/summator', source=sources)

# Finally, notify the main build environment it now has our shared library
# as a new dependency.

# LIBPATH and LIBS need to be set on the real "env" (not the clone)
# to link the specified libraries to the Godot executable.

env.Append(LIBPATH=['#bin'])

# SCons wants the name of the library with it custom suffixes
# (e.g. ".x11.tools.64") but without the final ".so".
shared_lib_shim = shared_lib[0].name.rsplit('.', 1)[0]
env.Append(LIBS=[shared_lib_shim])

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 environment variable:

export LD_LIBRARY_PATH="$PWD/bin/"
./bin/godot*

Nota

You have to export the environment variable. Otherwise, you won't be able to run your project from the editor.

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:

# SCsub

Import('env')

sources = [
    "register_types.cpp",
    "summator.cpp"
]

module_env = env.Clone()
module_env.Append(CCFLAGS=['-O2'])

if ARGUMENTS.get('summator_shared', 'no') == 'yes':
    # Shared lib compilation
    module_env.Append(CCFLAGS=['-fPIC'])
    module_env['LIBS'] = []
    shared_lib = module_env.SharedLibrary(target='#bin/summator', source=sources)
    shared_lib_shim = shared_lib[0].name.rsplit('.', 1)[0]
    env.Append(LIBS=[shared_lib_shim])
    env.Append(LIBPATH=['#bin'])
else:
    # Static compilation
    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 speed up the build further by explicitly specifying your shared module as target in the SCons command:

scons summator_shared=yes platform=x11 bin/libsummator.x11.tools.64.so

Escrevendo documentação personalizada

Escrever documentação pode parecer uma tarefa enfadonha, mas é altamente recomendável documentar seu módulo recém-criado para tornar mais fácil outros usuários se beneficiarem dele. Sem mencionar que o código que você escreveu há um ano pode se tornar indistinguível do código que foi escrito por outra pessoa, então seja gentil com você mesmo quando ler no futuro!

Existem várias etapas para configurar documentos personalizados para o módulo:

  1. Crie um novo diretório na raiz do módulo. O nome do diretório pode ser qualquer um, mas usaremos o nome doc_classes em toda esta seção.

  2. Agora, precisamos editar config.py, acrescente o seguinte trecho:

    def get_doc_path():
        return "doc_classes"
    
    def get_doc_classes():
        return [
            "Summator",
        ]
    

The get_doc_path() function is used by the build system to determine the location of the docs. In this case, they will be located in the modules/summator/doc_classes directory. If you don't define this, the doc path for your module will fall back to the main doc/classes directory.

The get_doc_classes() method is necessary for the build system to know which registered classes belong to the module. You need to list all of your classes here. The classes that you don't list will end up in the main doc/classes directory.

Dica

You can use Git to check if you have missed some of your classes by checking the untracked files with git status. For example:

user@host:~/godot$ git status

Exemplo de saída:

Untracked files:
    (use "git add <file>..." to include in what will be committed)

    doc/classes/MyClass2D.xml
    doc/classes/MyClass4D.xml
    doc/classes/MyClass5D.xml
    doc/classes/MyClass6D.xml
    ...
  1. Agora podemos gerar a documentação:

We can do this via running Godot's doctool i.e. godot --doctool <path>, which will dump the engine API reference to the given <path> in XML format.

In our case we'll point it to the root of the cloned repository. You can point it to an another folder, and just copy over the files that you need.

Execute o comando:

user@host:~/godot/bin$ ./bin/<godot_binary> --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 Contribuindo para as referências de classe 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 <path>, you might encounter an error similar to the following:

ERROR: Can't write doc file: docs/doc/classes/@GDScript.xml
   At: editor/doc/doc_data.cpp:956

Adicionando ícones personalizados do editor

Similarly to how you can write self-contained documentation within a module, you can also create your own custom icons for classes to appear in the editor.

For the actual process of creating editor icons to be integrated within the engine, please refer to Editor icons first.

Once you've created your icon(s), proceed with the following steps:

  1. Make a new directory in the root of the module named icons. This is the default path for the engine to look for module's editor icons.

  2. Move your newly created svg icons (optimized or not) into that folder.

  3. Recompile the engine and run the editor. Now the icon(s) will appear in editor's interface where appropriate.

If you'd like to store your icons somewhere else within your module, add the following code snippet to config.py to override the default path:

def get_icons_path():
    return "path/to/icons"

Resumindo

Lembre-se de:

  • Use macro GDCLASS para herança, então Godot pode embrulhá-la

  • use `` _bind_methods`` para vincular suas funções ao script e permitir que funcionem como callbacks para os sinais.

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

  • Se você herdar de: ref: class_Node (ou qualquer tipo de nó derivado, como Sprite), sua nova classe aparecerá no editor, na árvore de herança na caixa de diálogo "Adicionar Nó ".

  • Se você herdar de: ref: class_Resource, ele aparecerá na lista de recursos, e todas as propriedades expostas podem ser serializadas quando salvas / carregadas.

  • Por esta mesma lógica, você pode estender o Editor e quase qualquer área da engine.