Dynamic Node mesh swap problem

Hello :slight_smile:

I have a problem with swaping a mesh for my objects. I initialize each enemy mesh like this:

Code:
pig[i].setMesh((Node)app.getAssetManager().loadModel("Models/pig/pig.j3o"));

I want to change the mesh dynamicly for enemis when f.ex. they are killed so I use once again:
Code:
pig[i].setMesh((Node)app.getAssetManager().loadModel("Models/baleron/baleron2.j3o"));
but that doesn't work and the still have the old mesh. How can I swap the mesh for my object?

dettach the old one from its parent and attach the new one. Also, just load your assets once, if you wanna have a copy from the original asset, just use the “clone()” method.

Ok, so I tried to detach the object from its parent node “enemies”, set the new mesh and add it once again:

Code:
enemies.detachChild(pig[i].getMesh()); pig[i].setMesh((Node)app.getAssetManager().loadModel("Models/baleron/baleron2.j3o")); enemies.attachChild(pig[i].getMesh());
but on my android device I receive an exception from GL renderer. I think this might be caused by using simple arrays to store my objects. I iterate through all of the in the simpleUpdate loop so calling one when it is detached may cause that exception (that is only my theory...) Do you think that using lists instead of arrays would fix the problem?

No. I think it’s no the problem. But, what does your “setMesh()” method? We are talking about spatials, not meshes ok? Also, why are you loading the model each iteration? Load it just once and call “clone()” from the original one. Posting here the strack trace could be nice too!

Thanks for the tip with the clone() method. My setMesh() method initializes a Node field in each “Pig” object instance. I attach these nodes to “enemies” Node and then attach enemies Node to rootNode.

Btw could you post the stack trace?

Here is what DDMS says:

Code:
12-20 14:25:06.956: E/AndroidHarness(27464): com.jme3.renderer.RendererException: Shader link failure, shader:Shader[language=GLSL100, numSources=2, numUniforms=17, shaderSources=[ShaderSource[name=Common/MatDefs/Light/Lighting.vert, defines, type=Vertex], ShaderSource[name=Common/MatDefs/Light/Lighting.frag, defines, type=Fragment]]] info:Error: varying variables do not fit in 8 vectors. 12-20 14:25:06.956: E/AndroidHarness(27464): Error: varying variables do not fit in 8 vectors. 12-20 14:25:06.956: E/AndroidHarness(27464): SEVERE AndroidHarness 14:25:06 Exception thrown in Thread[GLThread 16,5,main]: at com.jme3.renderer.android.OGLESShaderRenderer.updateShaderData(1195) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.renderer.android.OGLESShaderRenderer.setShader(1239) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.material.Material.renderMultipassLighting(799) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.material.Material.render(1026) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.renderer.RenderManager.renderGeometry(657) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.renderer.queue.RenderQueue.renderGeometryList(299) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.renderer.queue.RenderQueue.renderQueue(351) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.renderer.RenderManager.renderViewPortQueues(894) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.renderer.RenderManager.flushQueue(850) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.renderer.RenderManager.renderViewPort(1126) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.renderer.RenderManager.render(1168) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.app.SimpleApplication.update(266) 12-20 14:25:06.956: E/AndroidHarness(27464): at com.jme3.system.android.OGLESContext.onDrawFrame(418) 12-20 14:25:06.956: E/AndroidHarness(27464): at android.opengl.GLSurfaceView$GLThread.guardedRun(1337) 12-20 14:25:06.956: E/AndroidHarness(27464): at android.opengl.GLSurfaceView$GLThread.run(1121)

Yeah! I fixed it! It was a lack of defined material for new mesh :wink: