godotengine / godotengine/godot-cpp

class factory for self-registration of classes

Open
#548 6 comments 0 reactions 0 assignees View on GitHub
discussion enhancement
Dominant language
C++
Stars
2.7k
Forks
809
Avg merge
1d 3h
Merged PRs (30d)
8

Description

Hi, I've started working on a class factory for my own project that would allow classes to self register without having to manually always add register_class instructions to godot_nativescript_init, and I was actually wondering if submitting it to the official godot-cpp repository would be considered a good thing

as I feel like this could be useful for a lot of users but I don't know if something like this is considered as not wanted in the official repository

right now the way I implemented it makes it so to register a class it must inherit from a generic class and call the REGISTER_CLASS(className) macro in the cpp

for instance for a GDTest class

header
```
#ifndef GDTEST_H
#define GDTEST_H
#include "ClassFactory.h"
#include
#include
namespace godot
{
class GDTest : public Sprite, public ClassFactory
{
GODOT_CLASS(GDTest, Sprite)
public:
static void _register_methods();
GDTest() = default;
~GDTest() = default;
void _init(); // our initializer called by Godot
private:
float time_passed;
};
}
#endif
```
cpp
```
#include "gdtest.h"

REGISTER_CLASS(godot::GDTest);

void GDTest::_register_methods()
{
register_method("_init", &godot::GDTest::_init);
}

void GDTest::_init()
{
Godot::print("Hello World !");
}
```
the class GDTest will register itself without a need to modify the godot_nativescript_init

the godot_nativescript_init just needs to contain this

```
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
godot::Godot::nativescript_init(handle);
ClassFactoryManager::register_classes();
}
```
If it is wanted I could work on a pull request to integrate it to godot-cpp

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.