AppStates and initialization

Have been refactoring my latest jME3 efforts to use AppStates, but now I’m getting some NullPointerExceptions when I attach the AbstractAppState to the StateManager. After doing a bit of tracing I’ve found that the the initialize method for the AbstractAppState is not being called before it is being attached. I know the AbstractAppState class is a fairly non-functional skeleton class, but I figured initialize would automatically be called before the class is used and it doesn’t appear to be. Does anyone know where and when initialize should be called?



I was thinking of adding in a:

[java]

if (!isInitialized()) initialize(stateManager, app);

[/java]

Right at the top of the stateAttached method. Is this wise?



Thanks in advance!

I think I’ve spotted the issue and it may be jME3 itself. I’d like someone else to look this over to confirm. I’m actually using BulletAppState (not AbstractAppState as I had previously mentioned), so after looking in the BulletAppState source I come across this:

[java]

public void startPhysics() {

//start physics thread(pool)

if (threadingType == ThreadingType.PARALLEL) {

startPhysicsOnExecutor();

} else if (threadingType == ThreadingType.DETACHED) {

startPhysicsOnExecutor();

executor.submit(detachedPhysicsUpdate);

} else {

pSpace = new PhysicsSpace(worldMin, worldMax, broadphaseType);

}

pSpace.addTickListener(this);

initialized = true;

}



public void initialize(AppStateManager stateManager, Application app) {

if (!initialized) {

startPhysics();

}

initialized = true;

}



public boolean isInitialized() {

return initialized;

}



public void stateAttached(AppStateManager stateManager) {

if (!initialized) {

startPhysics();

}

if (threadingType == ThreadingType.PARALLEL) {

PhysicsSpace.setLocalThreadPhysicsSpace(pSpace);

}

}

[/java]

Notice how the test to see if the state has been initialized in stateAttached calls startPhysics and not initialize? Also, the startPhysics method sets the initialized variable to true at the end of it. This means if you extend BulletAppState and override the stateAttached as I have done, your state initializing code will not get executed.



Thing is, I’m not sure if it is supposed to work this way and I’m doing something wrong. But I would have thought if stateAttached is testing to whether it is initialized and finds out it isn’t, it should then initialize the state… not call startPhysics (which the initialize method does anyway).



What do people think?

I think call to intialize(AppStateManager, Application) cannot be done from stateAttached(AppStateManager) because the lack of the Application pointer.

I agree line 19 in your snipset is useless.



But if you override the stateAttached method and have a custom intialize method, you should be able to initialize whenever you want and rewrite those tests to behave as you whish.

Hmmmm, you raise a good point, didn’t notice the additional required parameter for initialize.



As I don’t have a whole lot of experience with jME3 I was relying on super.initialize and super.stateAttached to do the important stuff (in regards to starting up the physics stuff), which is actually where this issue popped up. Since I know exactly what it is doing, yea, I can remove those super.method() lines and do it myself to get around it now.



Thanks for you input :slight_smile: