= Main Update Loop
:revnumber: 2.0
:revdate: 2020/07/15
:keywords: basegame, control, input, init, keyinput, loop, states, state


Extending your application from xref:jme3/intermediate/simpleapplication.adoc[com.jme3.app.SimpleApplication] provides you with an update loop. This is where you implement your game logic (game mechanics).

Some usage examples: Here you remote-control NPCs (computer controlled characters), generate game events, and respond to user input.

To let you see the main update loop in context, understand that the SimpleApplication does the following:

*  *Initialization* – Execute `simpleInitApp()` method once.
*  *Main Update Loop*
..  Input listeners respond to mouse clicks and keyboard presses – xref:jme3/advanced/input_handling.adoc[Input handling]
..  Update game state:
...  Update overall game state – Execute xref:jme3/advanced/application_states.adoc[Application States]
...  User code update – Execute `simpleUpdate()` method
...  Logical update of entities – Execute xref:jme3/advanced/custom_controls.adoc[Custom Controls]

..  Render audio and video
...  xref:jme3/advanced/application_states.adoc[Application States] rendering.
...  Scene rendering.
...  User code rendering – Execute `simpleRender()` method.

..  Repeat loop.

*  *Quit* – If user requests `exit()`, execute `cleanup()` and `destroy()`. +
The jME window closes and the loop ends.


== Usage

In a trivial xref:jme3/intermediate/simpleapplication.adoc[SimpleApplication] (such as a xref:tutorials:beginner/beginner.adoc[Hello World tutorial]), all code is either in the `simpleInitApp()` (initialization) or `simpleUpdate()` (behaviour) method – or in a helper method/class that is called from one of these two. This trivial approach will make your main class very long, hard to read, and hard to maintain. You don't need to load the whole scene at once, and you don't need to run all conditionals tests all the time.

It's a best practice to modularize your game mechanics and spread out initialization and update loop code over several Java objects:

*  Move modular code blocks from the `simpleInitApp()` method into xref:jme3/advanced/application_states.adoc[AppStates]. Attach AppStates to initialize custom subsets of “one dungeon, and detach it when the player exits this “dungeon. +
Examples: Weather/sky audio and visuals, physics collision shapes, sub-rootnodes of individual dungeons including dungeon NPCs, etc.
*  Move modular code blocks from the `simpleUpdate()` method into the update loops of xref:jme3/advanced/custom_controls.adoc[Custom Controls] to control individual entity behavior (NPCs), and into the update method of xref:jme3/advanced/application_states.adoc[AppStates] to control world events. +
Examples: Weather behaviour, light behaviour, physics behaviour, individual NPC behaviour, trap behaviour, etc.
