Archive 17/01/2023.

Deleting old models and nodes which are a part of a class

spenland

I have a class called BasicTree which has a node and a model. I want to be able to put these trees all over the map. I have achieved that but now I want to clear the map and put them all in new positions.

How do I delete the node and model and (maybe?) the entire class instance so that if the world repopulates many times, these tree instances don’t get left and cause a problem later?

New to c++ and new to Urho3d…

Thanks!

Dave82

You can simply use

yourNode->Remove();

This call will remove the node and all it’s children and all components in the hierarchy.
If you want to remove just components from the node but keep the node “alive” , use

yourNode->RemoveComponent(yourModel);

Technically if you frequently remove/add nodes and components to the scene it is recommended to add a root node to the scene adn add everything removable stuff there so you can remove everything with just one call. Something like :

Node* forestRootNode = scene->CreateChild();
// populate forestRootNode with what ever you want and if you want to remove everything from the scene
//just call
forestRootNode->RemoveAllChildren(true); // the true value indicates to remove all children recursively
Modanung

If your trees consist only of StaticModels you may want to consider using a StaticModelGroup as demonstrated in sample 20.