0
\$\begingroup\$

I had two classes in my game, Player and ShootableObject. Both set up as such:

class Player : public RigidBody2D { private: GDCLASS(Player, RigidBody2D); ... implementation follows ... 
class ShootableObject : public RigidBody2D { private: GDCLASS(ShootableObject , RigidBody2D); ... implementation follows ... 

And registered as such in my module:

void initialize_example_module(godot::ModuleInitializationLevel p_level) { if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) { return; } godot::ClassDB::register_class<godot::Player>(); godot::ClassDB::register_class<godot::ShootableObject>(); } 

I wanted to extract some common behavior from the two and created a third class, as such:

class GravityBody2D : public RigidBody2D { private: GDCLASS(GravityBody2D, RigidBody2D); 

I then changed the inheritance of the other two:

class Player : public GravityBody2D { private: GDCLASS(Player, GravityBody2D); ... implementation follows ... 

And added it to the exports:

void initialize_example_module(godot::ModuleInitializationLevel p_level) { if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) { return; } godot::ClassDB::register_class<godot::Player>(); godot::ClassDB::register_class<godot::ShootableObject>(); godot::ClassDB::register_class<godot::GravityBody2D>(); } 

I now get an error that the classes are not found:

 Cannot get class 'Player'. Cannot get class 'ShootableObject'. Cannot get class 'ShootableObject'. 

I can however see GravityBody2D in the node list:

enter image description here

How can I inherit my own class?

\$\endgroup\$
2
  • \$\begingroup\$ Does the order of registration matter? Do you get different results if you register the base class before the derived classes? \$\endgroup\$ Commented Nov 5, 2023 at 12:23
  • \$\begingroup\$ @DMGregory Just like you said, as I just found out while at my wits end. It makes little sense to me as if I were implementing such system, I'd collect metadata first and turn it into a tree later. I would also probably print a message if I was missing something, like a parent declaration. \$\endgroup\$ Commented Nov 5, 2023 at 12:39

1 Answer 1

1
\$\begingroup\$

The order of registration matters, the parent classes MUST be registered first. Correct order in this example:

void initialize_example_module(godot::ModuleInitializationLevel p_level) { if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) { return; } godot::ClassDB::register_class<godot::GravityBody2D>(); godot::ClassDB::register_class<godot::Player>(); godot::ClassDB::register_class<godot::ShootableObject>(); } 
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.