I have a Component subclass called Planet. It looks like this:
class Planet : public Component {
public:
bool has_gravity = false;
};
It’s added to the planet’s node like this:
Planet* rock_planet = rock_node->CreateComponent<Planet>();
rock_planet->has_gravity = true;
During each update, I check each planet for gravity:
PODVector<RigidBody2D*> all_nodes;
scene_->GetComponents(all_nodes, StringHash::Calculate("RigidBody2D"));
for (PODVector<RigidBody2D*>::Iterator body = all_nodes.Begin(); body != all_nodes.End(); body++)
{
Node* node = (*body)->GetNode();
Planet* planet = node->GetComponent<Planet>();
if (planet)
{
// do stuff
}
However this always returns null. If I swap for any other built in component type, it works perfectly:
// For debug rendering - this code works just fine.
PhysicsWorld2D* physics_world = scene_->GetComponent<PhysicsWorld2D>();
Inside GetComponent is a rabbit hole of templates, typedefs, and string hashes. It is not obvious to me what I need to do to make this work, or more specifically which parts of these templates my Planet is missing.
Other Components have #defined things in their class definitions that I’m not familiar with:
class URHO3D_API StaticSprite2D : public Drawable2D
{
URHO3D_OBJECT(StaticSprite2D, Drawable2D);
Straight copying those into my class has not helped:
"URHO3D_OBJECT(Planet, Component)")
Visual studio doesn’t like it, and I don’t know what it does anyway, but it’s the most obvious difference between my class and other components.
Any help?