[Solved] Default AppState fields

Good day,
I created new AppState instance for main game logic realization. Now I have some problems with accessing to BulletAppState instance from this app state.

In the bottom of this docs page there is a template about accessing to all default fields of SimpleApplication from different app states.
In this example there is default field physics - instance of BulletAppState, which I can access this way:

this.physics = this.stateManager.getState(BulletAppState.class);

But, when I do this inside my app state I get, that the variable physics is null

Can you explain me, is there a default BulletAppState instance in every AppState or I must create the one for every AppState I create?

An appstate is just a class that is put into an easily accessible list. It has an update/render loop that is called every frame - which is what you need mostly for game-based logic. It also has some handy initialization/cleanup logic. It’s a very useful class. It allows you to create “states” that you can initialize, cleanup and use the gameloop in a tidy class. Attaching them and detaching them is very much a “modular” type of action. I can attach a menu. I can detach a menu. I can attach a world and detach a world. On or off. Nice tidy little “states” of the game.

So since BulletAppState is an appstate (and you should name your appstates xxxAppState for easy recognition, too) it is accessible by getting the state from the appstatemanager.

If you want to access any state - call stateManager.getState(MyAppState.class).

If you are already in an appstate, you can just call getState(BulletAppState.class) anywhere except in the constructor.

BulletAppState bulletAppState = getState(BulletAppState.class);

Just note that it must be attached somewhere first, or it will return null. BulletAppState is not attached by default (because some game don’t require bullet physics simulation). You must attach it. I usually attach it in the simpleInit method of the main class.

stateManager.attach(new BulletAppState());

Well explained. You should consider to reference your post to the Wiki section about Appstates. Indeed it sometimes take some time to understand the concept but your explaination helps.

Ok, BulletAppState isn’t attached by default. And, if I’m already is in app state, I can find the others directly: getState(BulletAppState.class)
Can I mark your answer as solution?

I’ve done it for you.