Correct use for UserData on spatials

I think I tend to use UserData differently from how its supposed to be used. I use it to couple my objects to spatials at run time so I can look them up later (E.g. picking for a geometry and then pull off the userdata to get the actual stuff I picked for).

shipsGeometry.setUserData("SHIP", ship);

But user data must implement Savable so I tend to have an interface like this that anything to be used as user data has to implement.

public interface UserDataSavable extends Savable{
    default void write(JmeExporter ex){
        throw new UnsupportedOperationException();
    }

    default void read(JmeImporter im){
        throw new UnsupportedOperationException();
    }
}

And that makes everything happy. But I feel like I’m missing something. It feels like it would be very wrong for the geometry of a ship to be in any way included in a saved game file, so the idea of a geometry being saved feels strange to me (outside of I guess producing assets to be bundled with the application). My geometry and my “state” tends to have some separation and only the state is saved.

But this page suggests saving the whole scene graph down into j3o format is a good idea.

This has worried me for a while. Am I missing something, is saving the whole scene graph into j3o format as a saved game a good idea?

1 Like

Absolutely not. It’s a bad idea for dozens of reasons. It’s only benefit is that it is really simple… and users who need that to be really simple probably also aren’t storing complicated things in their UserData.

Personally, the only thing I ever put in a spatial’s UserData is its EntityId… and for exactly the reason you state: if I’m picking or debugging, I want to know what entity that spatial belongs to. The other exception is stuff I’ve added as custom properties in Blender… that I expect to be in UserData so I can do whatever post-load processing that I need to do.

So if you really want to store complex objects in your spatial’s UserData, your approach is valid. Then just save/load your save game a different way (and it’s bound to be 100x smaller that way, too).

3 Likes