What is the best approach or equivalent approach to Node attached scripts in C++?

Godot Version

4.5

Question

Hello all,
The topic is a bit confusing, so I’ll try to explain.
Currently, I’m building my game entirely in C++ using GDExtension.
When I want to create a scene with a root node or hierarchy of nodes where each node (or some of the nodes) contains code, I create the nodes through code. For example:

#ifndef GLOBAL_MANAGER_H
#define GLOBAL_MANAGER_H

#include <godot_cpp/classes/node.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

using namespace godot;

class GlobalManager final : public Node {
    GDCLASS(GlobalManager, Node);

protected:
    static void _bind_methods();

public:
    void _ready() override {
        UtilityFunctions::print("GlobalManager autoload initialized.");
    }

    void doSomething() {
        UtilityFunctions::print("GlobalManager::doSomething called.");
    }
};

#endif // GLOBAL_MANAGER_H

I’m not sure if this is the best approach, as I’m creating many new nodes in Godot which I add from the “Add Node” window.
Maybe a better approach is to create the scene tree, and for each node in the hierarchy, I will add a child node which will contain the C++ code. I don’t know…
What do you think is the best approach? Should I just create the nodes in code and add them, or is there a better way?

Scripts are very useful, it is probably better for your project to leverage them to some degree. I’m not sure you really have a question, or you haven’t explained exactly what you are trying to do, what you expected to happen, and what goes wrong.

Don’t. Except as an exercise. There’s nothing to be gained. Delegate as much as possible to scripts.

1 Like

hehe , only c++ for me

1 Like

Fair enough. But why? It just makes it all more time consuming.

1 Like

It’s a hobby for me godot i mean . I understand and work very quickly with C++. I have 20 years of experience with C++ (none game-related, but in critical systems). I know the true power of C++. For me, using something other than C++ is just making life complicated. This is me (:

3 Likes

You may change your mind in the process of developing an actual game. Having an interpreted scripting language on your disposal is extremely convenient and time saving. Being stubborn about not using it is kinda counterproductive.

Just for the record, since we’re bragging here, fwiw; I code in C and C++ since I was a kid, have 30+ years of experience with them and a published game title done entirely in C++ with my own OpenGL framework/engine.

2 Likes