Correct use of Controls/Appstates


I’ve now spend some time wrapping my head around controls and appstates

In my experience every framework, no matter if aimed at game-development or roboter-programming, has a unique way to solve problems. If you’ve come up with an architecture that aligns to principles of the framework you’ll be rewarded with high productivity and ease of use. I’m new to jme and haven’t discovered the right approach yet. I intended to structure my project as follows:

data in special classes

logic in controls

general state in appstate.

This may appear obvious and redundant but I’ve followed these ideas in a first draft of my project. Then implementing a few of the design decisions I realized that all the built in functions are meant to be used with Spatials or Geometries. Sure conversions and polymorphism would solve this problem but I found myself doing the same typecasts over and over again. Accessing data from a custom class requires a lot of work. Classes that inherit from Spatial or Geometry are rarely efficient. You have to write many methods to pass around the right references. On the other hand getting a specific control is more than comfortable. Its reference is easily available. Therefore I decided to redesign the architecture:

data and logic in controls

general state in appstate

All objects are stored in standard nodes, spatials and geometries

To do this the entire game is initialized in simpleInitApp and references to the rootNode, cam, and other objects are passed to each control and the one custom appstate I consider necessary. Each control and the appstate are now storing all information they need. The whole application is split into nodegraph and behaviours. The behaviours enable and disable each other according to user input and appstate.

I know this question is very abstract and hard to answer without reading code but just in general:

Is this the right way ?

What would you recommend ?

seems you already know all the answers lol, its good you did a lot of research before hand to get proper organization done. Everyone has their own style when it comes to programming, and theres no wrong answer and I see no problem with the design you’ve chosen, seems very efficient and scalable.

1 Like

Since all these classes have the getControl() method, just handle them as equal and “judge” them by the Controls that are attached. You can/could also save data inside controls… But it depends where that data needs to be accessible most of the time. You can also create controls that have no specific function but only tell the application what this spatial is, e.g. instead of if(spatial instanceof MyNode) you have if(spatial.getControl(NodeControl.class)!=null).

@wezrule Thank you. In fact I just wrote a few lines of code and didn’t know how to get up with a decent structure. Therefore I tried to figure out the most convenient approach and posted this question.

@normen It sounds like you’re not convinced of the idea to store data and logic at the same place. Neither was I when I made the first few sketches but if properties are stored within new classes that extend spatial passing the data around and casting all the results gets uncomfortable in comparison to the well supported use of getControl and the standard classes. You also recommend identifying spatials over their controls which implies a very control-centered architecture.

Plainly: How would you do it ?

A healthy mix of custom classes and controls ? Might be very flexible but net really intuitive and problematic for huge applications

Data in Objects, Behaviour in Controls ? Problems as already described.

Data and Behaviour in Controls ? I don’t know

Something else ?

Normen was never suggesting to subclass Spatial. Believe me.

But Spatial has setUserData/getUserData… though really if you already have controls that would be using that data it makes just as much sense to go there.

It’s data that is shared between controls that can be put in user data on the spatial.


Well it depends :slight_smile: (Edit: was not an answer to paul, he posted right before I hit send :), I definitely do not suggest to extend Spatial) You can look at the code of MonkeyZone where I wrote up a basic networking game framework. This is basically how I would do a “simple” game, combining logic and data in the Controls and AppStates. The only part of the code that is a bit flawed is where I tried to apply OOP extension smartness to the otherwise clean system (where I’m extending from base networking classes to have some functions)… Another proof that extension in java should never be used as a programming pattern like its done in Swing (where it fits “by accident”). ^^

For more extended systems like MMO’s you might want to look into “Entity Systems” which is a completely non-OOP based pattern. It mainly separates into

a) Entities - just a number identifying the Entity.

b) Components - only data sets that are registered for some Entity and that identify functions of the entity, say if it has the warrior component its a warrior with the stats stored in the component. Could be a database for example.

c) Systems - the actual code that does stuff, almost all functions just use entity id’s as parameters so they are very inter-usable.

however it takes some time to get into this pattern as it happens time and again that you try to apply OOP “smartness” to it and that path leads directly to programming hell :wink:

This is exactly what I needed. I’ll try to center on controls and make my data easy to access from them. setUserData/getUserData sound interesting I’ll take a look.

Thank you for your help.


@normen Although my post still fits I experienced the same you did. Your post was posted after I started typing and I didn’t read it. Up to now I’ll follow MonkeyZone and equip controls with their own data. Yet you made me curious and I’ll look at entity patterns as fast as possible which is actually not that soon. I’m using a surfstick and as the day comes to its end (flatrate for a day) I’ll have to live without internet for some time. I’m glad the question yould be answered so fast and thank you all for your help

Update#2 :

@normen Ich sehe gerade, dass du auch aus Deutschland kommst. Ich wünsche dir noch eine schöne Woche, viele Grüße aus dem Schwarzwald.

Gruß zurück aus dem kühlen Norden :wink:

Grüße aus Brasilien :wink:

glaucomardano said:
Grüße aus Brasilien ;)

Heh, not bad, way better than my portugese ;)