Just what it says on the tin!
I’d like to have a generic initialization function for my components, similar to Start() in unity3d. OnNodeSet seems the best built-in option, but it’s not working out as I planned.
Here’s the code I’m using:
void TestComponent::OnNodeSet(Node *node)
{
//Calling Init here doesn't seem to properly create the rigidbody and/or collisionshape upon loading
if (node) Init(node);
}
void TestComponent::ApplyAttributes()
{
//Uncommenting this Init() call and commenting out the OnNodeSet one results in save/load working fine,
//but initialization of instantiated objects becomes obviously borked.
//Interestingly it seems to have no effect when both are uncommented
//Init(node_);
}
void TestComponent::Init(Node* node)
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
StaticModel* model = node->GetOrCreateComponent<StaticModel>();
model->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
model->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
model->SetCastShadows(true);
RigidBody* rigidbody = node->GetOrCreateComponent<RigidBody>();
rigidbody->SetMass(1.0f);
CollisionShape* shape = node->GetOrCreateComponent<CollisionShape>();
shape->SetBox(Vector3::ONE);
}
When I create objects, it works fine:
However, when I save/load the scene, the collisionshapes seem to become detached from the node, and the model falls through the ground.
My test scene is available here.
Is this a bug, or should I not be using OnNodeSet to call other components’ functions? Is there a recommended approach?
Thanks.