Attempting to go abstract -- need a little advice

Hey, just wanted to say after going through all the tutorials – they were great, really helped me learn how the engine operates.

My problem, is that I’m a really slow learner I guess and I’m just trying to wrap my head around everything. I can make changes to the tutorial levels just fine - and I can combine different elements from them together, but due to how I seem to be inheriting so many objects from the SimpleApplication class, it’s hard for me to understand how it all comes together and works.

Now I’m trying to build a simple game from scratch and I’ve tried to simplify things for myself by trying to separate some of the things I need into abstract object classes (so I don’t have one really really long main file). The problem is, well… I don’t know how to do it right, lol. I feel so stupid because I’m obviously missing something simple here.

Here’s my particular problem:

I have a scene (We’ll say the tutorial town for example) and I want to create a player controller object from another class, then add it to the rootNode and use collision (Basically the hellocollision tutorial with all player-related stuff in a different class).

If I extend the SimpleApplication in the PlayerObject class, it wants me to use “simpleInitApp” method, which is not really what I want - I want to use my own constructor, so I instead extended the Application class. However, in both instances I’m running into problems:

Firstly, it’s telling me that when I try to bind it to the PhysicsSpace that it’s unable to bind that object (I am passing the correct information with a method: “testPlayer.getController()” → returns the CharacterController)

Secondly, if I try to set up the movement keys from the PlayerObject it tells me I have a nullpointer exception on a line containing:

" inputManager.addListener(new ActionListener() " which obviously means inputManager must not be initialized somehow.

So I guess my question is, where would I start? I’ve been going over the JMonkey Docs but it’s not really mentioned how to do this.

Sorry for the wall of text, just wanted to be thorough - thanks.

Your application class should be very tiny, and just used to bootstrap the rest of the pieces together.

Look at AppStates. These attach to your main app class and get update() called on them. They also let you attach spatials to the node. Think of having several of them:

UIAppState: draws the UI components and lets you interact with them.

GameLogicAppState: holds the game info, time remaining, when the game ends, etc.

GameDataAppState: loads the actual scene: terrain, trees, boxes, houses etc.

PlayerAppState: initialize the player class, register a player and hook it up to input.

Then in your main app class you can add and remove app states as you need. It also lets you specifically subclass particular ones without having to worry about all the others.

Controls allow you to separate the logic of a spatial from the geometry. Say you have a model of a car. Just as-is, it would sit there. Attach a CarAIControl to it (one you would build) and it would move around. You only create the model once, but it can act differently depending on what controls you add to it.

Hope that helps a bit.


Your main application should extend SimpleApplication. Nothing else should. Zero, nothing, not at all. One class.

If you find yourself extending SimpleApplication all over the place then you really are doing something wrong. The application is the application. There should be only one. It sounds like you are doing the equivalent to having every object in your house extend House.

All of your problems are related to this as the only way for the variables in SimpleApplication to have been initialized is if it has actually started as an application.

So, you may need to take a step back. Explain why you want to have many applications in your application.

1 Like
Explain why you want to have many applications in your application.

Lol, I don't - it was just a bit of stupidity on my part. The only reason I was extending the Application class was to get access to the input manager and camera objects - but since these are already 'running' in the main application - that's what I should be using.

I appreciate the fast response, app states and controls is obviously the way to do things. Now I just gotta wrap my head around how all that works exactly.

I think if there was any feedback I could give; I'd say it would be nice to have this as a final tutorial or something so that people understand how to break their programs up rather than throwing everything into the main class all the time - just to better understand how things operate. Perhaps I'll write one up for anyone else to get an idea once I feel comfortable doing it myself.
1 Like

It already exist :

1 Like
@glaucomardano said:
It already exist :

Ahhh. Fantastic - Thanks (Don't know how I missed that honestly).