Game levels - how to manage game states

Hi Guys,
I would like to know how do you manage your game’s states for example when moving to another game level. regarding:
freeing assets / loading new assets
switching to a different game logic

I’m thinking maybe the easiest way is to simply restart the engine and load a whole “new” game for a given level but it definitely has a performance cost and I’m not sure if JME has a restart method which cleans up everything.

What are you doing in your games?

It sounds like you don’t really use app states.

…you should look into them if not and then come back and ask questions.

Because my answer would be “use app states”. GameSessionAppState or whatever. MainMenuAppState… starts the GameSessioAppState, GameSessionAppState returns to MainMenuAppState.

All init/cleanup nicely handled, etc… numerous examples around.

2 Likes

I use a few app states for camera modes, minimap, physics, input etc. but not dynamically I mean I’m not using them for switching game logic / assets.
Will investigate it more.

JME assetManager cache models itself for example, if you want clear that too, you would need access its cache and clear i guess.

IMO there are many ways to switch from one level to another, if you want using appstates, you can have Level1AppState or general LevelAppState(int level) and unattach and attach when switching levels using manager GameAppState.

JVM will clean-up mostly, while what you need clean-up additionally, do in cleanup() method.

Ye, just use them dynamically, and use init/cleanup properly like Paul said.

1 Like

yes this is the most important part of cleanup and can be an easy cause of memory leaks if not done correctly I think.

In my game I use appStates how others have already mentioned. So when the player goes from the game back to the menu, I do cleanup in my Map class to clear the memory from all of the cached assets that were loaded for that map/level :

         public void cleanUp(){
           
              for(int x = 0; x < totalX; x ++){
                    for(int z = 0; z < totalZ; z ++){
                        Node tile = staticTiles[x][z];
                        if(tile != null){
                            app.getRootNode().detachChild(tile); 
                            app.getAssetManager().deleteFromCache(tile.getKey());
                            staticTiles[x][z] = null;
                        }

                    }
                }
           }

And it is also important to clear all of your references to the asset and make sure to detach all copies of that asset from the rootNode prior to removing its assetKey from the cache, otherwise the memory space will not actually be freed up if there’s still references to it floating around. But if done right, then you should be able to go back and forth between the main menu and a bunch of different big levels without any memory issues.

1 Like

Note: the asset cache will still allow things to be GC’ed normally… so unless you are being super aggressive in loading a ton of unique things many times a second, things should work themselves out as long as they are really not in the scene anymore.

I think 9 times out of 10 when someone experiences out of memory errors it’s probably because something is still attached somewhere that it shouldn’t be.

4 Likes