Where to put things that need to be done everytime a spatial is attached to a concrete node?


lets assume I have a node in my game that contains all utils laying on the floor (like torchlights, pistols, …). Everytime a new util gets attached to this node, it must be rescaled, an ID must be given to it and is must be added to the physics space.

So, my question is, where should I put this code?

a) I could extend Node (-> UtilNode) and override attachChild() / detachChild(), but I really don’t think that this is the right way, because Node shouldn’t be extended :slight_smile:

b) I could write a control like UtilNodeControl and add it to my node. Going this way, I’d have to first get the control, second check whether it really has the control and third call the attach / detach method on it everytime I want to attach or detach an util (so around 5 lines instead of 1 line of code).

c) I could write a manager providing (static) a method like “attachUtilToNode(Node, Spatial)”, but I assume that this isn’t very object oriented …

So what option should I choose (or is there a better way I didn’t cover)?

Another way:

Game object gets added to util container game object or whatever. The util node thing then has a control that sees that now it holds more objects and adds the appropriate child.

I’m a fan of MVC so I don’t put any game data in my scene graph objects at all. They are just a slave to the model state. Though I use an entity system also so that’s a pretty trivial leap after that.

Of the solutions you mentioned, (b) is the most JME-friendly way. You should be able to do it as one line if you are pretty sure that the node already has the control:

util.getControl( MyControlThing.class ).addIt( theChild )

…the addIt method then does the work of attaching and so on.


thanks for your reply.

Your MVC solution sounds interesting, but I guess I didn’t understand it.

So you only use the scene graph as view (and therefore as graphical representation of your game)? If yes, wouldn’t be controls obsolete, since they would operate on the spatials / node (-> part of the scene graph → view) instead of the model?

What’s about the objects’ positions? how do you synchronize them between model and spatial (the physics could change ones position, so the model must be informed about this)?

The controls update the spatial’s position based on the model, as an example.