Jump to content

[SOLVED] Managing nodes


photo

Recommended Posts

Hello. I am wondering how to process simple CRUD operations on nodes. Assume I have .node file with my vehicle. I wanna add it to scene, update it and eventually erase it.

 

Adding:

   Unigine::NodePtr nptr = w->loadNode("myFileName.node");
   if (!nptr.get())
   {
      std::cerr<<"ERROR "<<std::endl;
      return;
   }
   
   Unigine::dmat4 trans = (....);
   nptr->setTransform(trans);

Updating could be simply:

Unigine::dmat4 trans = (....);
nptr->setTransform(trans);


But how about erasing it. I know i can use setEnabled(0), but this only hides the stuff, so it will cause mem leak. The Node destructor is protected, so I feel like I have no way to delete it from scene. Any helpful ideas?

Link to comment

I have already tried this, but no success. I did:

const Unigine::Ptr<Unigine::Node> fooNull(0);
nptr = fooNull;

Also I took a look at UniginePtr.h and found that clear method is what I actually need. However calling:

nptr.clear();

did not cause my loaded node to dissappear, same as the test descrived above.

 

[edit]

 

After some tests I realized that my smart pointer is not the "right" smart pointer. I made some tests and printouts.

In creating method:

   int nid = nptr->getID();
   std::cout<<" new node ptr = "<<nptr.get()<<std::endl;

And in erasing method:

   Unigine::NodePtr nptr2 = w->getNode(nid);
   std::cout<<"pointer nptr2 = "<<nptr2.get()<<std::endl;

As a result I got two different pointers printed in my console.

new node ptr = 0x63061d0
pointer nptr2 = 0x6306570

This made me feel lost... :/

Link to comment
  • 2 weeks later...

Please check the attached sample. This example demonstrates how to control the owner flag of Node pointers.

Each node has an owner flag internally (the function ptr->isOwner() will be available in the next SDK update).

This flag controls the node smart pointer destructor behavior. The Node pointer will destroy the internal node when the reference counter is zero and owner flag is set. The ptr->grab() function sets the owner flag. The ptr->release() function removes it.

 

It's safe to call grab/release functions multiple times.

 

ptr.clear() function does nothing because the owner flag is zero.

ptr->grab(); ptr.clear(); will destroy node (but you should be aware about child nodes deletion).

World.zip

  • Like 1
Link to comment
×
×
  • Create New...