Examples of code using states?


#1

Hello, I am currently in the process of learning how to use states so I can implement them in my game(not going to actually be released, just a game i’m making to help get familiar with jmonkey). I understand the basics of states, but I feel like seeing where and how people implemented them would help me understand optimal usage.

I’m new to this website, so i’m not sure where to post this sort of question, so sorry if it was not in the correct location.


#2

By “state” you mean AppState, correct?


#3

Tons of examples around…

Some more complicated ones here:

Some non-game ones but still illustrative here:

A complete single-player game example:

This is a relatively straight-forward example that replaces JME’s (ill-conceived) FlyByCamera with a Lemur InputMapper based AppState:


#4

Correct.


#5

#6

In case staring at other people’s code isn’t your thing, here’s an app-state dump of my current (closed-source) WIP.

The game currently uses 16 app states.

  • The 1st four seem to be built into SimpleApplication: I don’t use them much.
  • Then come 3 input modes: no more than one input mode can be enabled at any given time.
  • BindScreen, DsScreen, and MainHud each control one screen in the GUI: again, only one can be enabled at a time.
  • DumpTool and MapTool control non-modal subwindows in the GUI. They depend on MainHud, so they can be enabled only if MainHud is enabled.
  • BulletAppState: manages the game’s physics simulation.
  • View: manages the scene graph.
  • DynamicCamera: controls the rendering Camera.
  • PerformanceAppState: is my custom performance monitor.
AppStateManager with 16 states: 16 active
 active[0]: ResetStatsState enabled
 active[1]: StatsAppState enabled
 active[2]: AudioListenerState enabled
 active[3]: DebugKeysAppState enabled
 active[4]: DefaultInputMode enabled
 active[5]: DialogInputMode disabled
 active[6]: MenuInputMode disabled
 active[7]: BindScreen disabled
 active[8]: BulletAppState enabled NOdebug speed=1 bphase=DBVT
  PhysicsSpace #59ccdd30 contains 0 characters, 0 ghosts, 4 joints, 171 rigid bodies, and 0 vehicles
   accu=0.016667, bphase=DBVT, grav=[x=0 y=-9.81 z=0], maxStep=4
   iters=15, rayTest=(SubSimplexConvexCast), wMin=[xyz=-10000], wMax=[xyz=10000]
 active[9]: DsScreen disabled
 active[10]: DynamicCamera enabled
 active[11]: MainHud enabled
 active[12]: PerformanceAppState disabled
 active[13]: View enabled
 active[14]: DumpTool disabled
 active[15]: MapTool disabled