Class design

I will first explain the situation.
There is a room where the player walks around. In that room are (loaded) objects.
The objects are selectable and the user can pick it up to do things with it.
The first problem I had was that loaded objects from the assetManager are always a Spatial.
What I want is a good class design for the items in the rooms. Each item has a name and a description and other pieces of data.
My first idea was to make a subclass like RoomItem and extending Spatial. But I’ve read much things about this and came to the conclusion that I’d better not do this.

Now my solution is: Create a class RoomItem, which has the Spatial as property. Is that a good choice?
When I want to load an item, I create a RoomItem object, give it the Spatial with a setter, and set the data like name, description, etc.

But, the items can be picked up, and if i show the items in a gui list, I don’t need the sptial. So I’ve created a new class InventoryItem, which only accepts a RoomItem when it’s created. The data like name and description are take over from the RoomItem, and become stored in the InventoryItem.

When an item is placed from the inventory to the room, then again I need a RoomItem (with a Spatial, and attach this to a node)

Is this a good class design? How are you doing this?

You are better off in the long run completely decoupling your game objects from the scene graph. Some manager class can pick which spatial to use when adding a GameObject to the scene or to the inventory or whatever.

…but even within the structure you are using, I see no reason to have two separate InventoryObject and RoomItem. They could just as easily be one class that has both a spatial and an icon or whatever. I still think it’s not a great design, though for the above reasons.

I won’t even mention an ES (Entity System) because at this point it would probably make your head explode. :slight_smile:

1 Like

And if I don’t store a spatial in a RoomItem object, but just store the url of the spatial, or an id which refers to a spatial. Would that be better? So it’s not really much coupled with the scene graph.

@Andres said: And if I don't store a spatial in a RoomItem object, but just store the url of the spatial, or an id which refers to a spatial. Would that be better? So it's not really much coupled with the scene graph.

Your game objects don’t need to know anything about the spatial. The Spatial probably needs a reference to the game item. You can either add a control to the spatial (useful if you also need to update the spatial relative to the game object in some way) or set the game object ID into the spatial using setUserData().