Saving a scene including its controls

Hello guys,

is it possible to save a scene with your costum controls (especially when they extend GhostControl)? I’ve tried that days ago but I had some problems. Here’s my previous post for more details:

You call after your own read method…

I did that after the problem but then I changed it back.

What do you mean by “don’t react”? You still have to add the GhostControl to a physics space after loading it.

I ve created a new project to find out what the problem is. So in my new project i created a blue cube and a new class (ObjectControl for example ) that extends GhostControl and in the update() method i rotate the cube. so when the cube is rotating I know tha the control is attached. but when i load the model the cube is’nt rotating. Also when I run the debugger I see that the cube has one control attached but its type is GhostControl and not ObjectControl.

Ah, you don’t have a cloneforspatial method, I guess thats the issue. The original method from GhostControl will create a GhostControl and not your “ObjectControl”.

So do I have to override this method? I tried this:
public Control cloneForSpatial(Spatial spatial) {
try {
ObjectControl c = (ObjectControl)clone();
c.spatial = null; // to keep setSpatial() from throwing an exception
return c;
} catch(CloneNotSupportedException e) {
throw new RuntimeException( “Can’t clone control for spatial”, e );
but I get the above exception

Not quite, and you shouldn’t use clone for that. Look at the source of GhostControl here, you shouldn’t call setSpatial (this was a relatively fundamental change before 3.0 came out, at some point calling setSpatial was actually needed)

I changed the first line in the code that you sent me and it works! Thank you!

public Control cloneForSpatial(Spatial spatial) {
ObjectControl control = new ObjectControl(collisionShape);
return control;

This was hard for me. I couldn’t have found it by myself. So if I may ask: how does an experienced programmer deals with these things? I mean, if he stucks in a problem like this, what steps does he follow to find the solution?

Like you did now, UTSL (Use the source, Luke!) :wink: You can see the source of any jME class by right-clicking its name in the editor btw. And don’t forget to set your own variables as well on the new object.

Ok! thank you! just one last thing: for some classes (like GhostControl) I can’t see its code, it’s not attached (“No sources are attached to class’ JAR file”). Should I reinstall jmonkey or smth?