CloneCreator

I noticed that CloneCreator didn't exist anymore. So I was wondering how I would go about create a clone of a node? Also I need to copy animation information as it is a model.



Thanks.

What I've been using is my ObjectPool system I wrote for jME to handle it via the ByteArraySpatialGenerator.  I load my model into a byte[] and then create the generator and set it to the pool so when I need a model the pool creates it from the byte[] for me.  However, if you want a more manual approach you can always just do what ByteArraySpatialGenerator does in your code directly.

is there an easier way?

Easier?  I don't think it can be much easier. :wink:


ByteArraySpatialGenerator<Node> generator = new ByteArraySpatialGenerator<Node>(ResourceFactory.getModelBytes("planet01.jme"));
ObjectPool<Node> pool = new ObjectPool<Node>(generator, 0);



That's an example usage.  I have something similar to that in every "space bean" in my game that gets statically defined on first execution and then can be used via:

pool.get();



To create new instances of it.  Then when I'm done with the object I can return it to the pool.

CloneImportExport in jme 0.11 works fine !!  :slight_smile:

Not in all cases…for me it explodes throwing shards everywhere…okay, not really, but it does error out.  :stuck_out_tongue:

I must agree you the frog, It's ok with 3d models i tried, but I tried to clone a particle and can't display it.

Any ideas about informations I lost that should be restored?



Your pool's idea is a good one, especially as you told when managing bullets and that kind of quick life objects.    :wink:




…or maybe even particles? :wink:

darkfrog said:

Not in all cases...for me it explodes throwing shards everywhere....okay, not really, but it does error out.  :P


I had the same issue... it didn't work for me in all cases, especially when I had a created class extending Node or a few other cases.

The SpatialPool has worked great!

yep it works fine, even if I can't bring back my particles to the pool ( cannot cast Node to ParticlesMesh …)



… Sure instanceOf(DarkFrog) … ()



PS: Your Test site is great, but I'm not  experienced enough to discuss the goodness/badness of your system…

You should be able to put them back if you make it a pool of Nodes instead of ParticlesMeshes.

it took me a few tried to get what i believed to be a working objectpool copying system, but i think i'm close now.

it seems that copying the root node of my scene successfully copies over all the children of that node, including cameranodes and lightnodes. however, it seems that even though the cameras and lights are being copied over with the camera and light nodes, something is happening to them to prevent the scene from rendering.

for example, if i do this:


      if ( initialScene == null ) {
         rootNode = ProgressColladaImporter.getModel();
         ProgressColladaImporter.cleanUp();
      }

      ProgressColladaScene cs = new ProgressColladaScene( rootNode );

      initObjects(cs);
      incorporatePhysics(cs);


everything works fine.  progresscolladascene is a class that loops through the objects in the model returned from the colladaimporter (the progresscolladaimporter basially does the same thing, but is adapted to work with the iprogressmonitor abilities of eclipse), and stores the cameranodes, lightnodes, and objects locally for easy access.

unfortunately, simply adding one line to make the following doesnt work.


      if ( initialScene == null ) {
         rootNode = ProgressColladaImporter.getModel();
         ProgressColladaImporter.cleanUp();
      }

      initialScene = NodeHelper.nodeCopy( rootNode );
      ProgressColladaScene cs = new ProgressColladaScene( rootNode );

      initObjects(cs);
      incorporatePhysics(cs);



nodeCopy is essentially the same as your object pool stuff, and is attached below:


   public static Node nodeCopy( Node n ) {
      return (Node) copySpatial(n);
   }

   public static Spatial copySpatial( Spatial from ) {
      Spatial r = null;
      if ( from != null ) {
         try {
            JMEExporter exp = new BinaryExporter();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            exp.save(from, bos);
            byte[] bytes = bos.toByteArray();
            ByteArraySpatialGenerator<Spatial> generator = new ByteArraySpatialGenerator<Spatial>(bytes);
            ObjectPool<Spatial> pool = new ObjectPool<Spatial>(generator, 0);
            r = pool.get();
         } catch (IOException e) {
            e.printStackTrace();
         } catch (Exception e) {
            e.printStackTrace();
         }
      }

      r.updateGeometricState(0,true);
      r.updateRenderState();
      return r;
   }



am i doing something wrong with the object pool, or is there something that isn't correctly being copied over during the nodecopy?
:?

nevermind, it was a silly threading issue.

hope the nodecopy method helps someone

The only problem I see with using a method like your copySpatial is there's a lot of extra weight to it as compared to the object pooling.  With the object pooling it generates that byte[] once, but with the copySpatial method you are doing that every time it's called as well as instantiating additional objects that then have to be garbage collected later.

hmm, i see what you're saying

this isn't really being called that often, but that might be an idea for some changes in the future

thanks!