Issue with screen freezing when trying to change appstates/screens

Hello all,

I basically have a start screen with a button to load a new scene. When I click this button the screen freezes until the new appstate loads, and then it changes to the new screen.

I am curious if there is a way to prevent this from happening, preferably with some sort of loading bar to show progress of the world that’s loading.

I am using “T0neGodGui” and I originally had this code in the button click method, which calls this.stateManager.detach(this);, but now it’s in the method below. I’m not sure if I just need to switch some lines of code around… or what?

  @Override
    public void cleanup() 
    {
        super.cleanup();
        gui.removeControl(screen);
        gui.detachAllChildren();
        gui.updateModelBound();
        this.stateManager.attachAll(new StoreAppState2(), new CamAppState());
     }

Thanks for any help, let me know if any more information is needed :).

Are you creating an new appstate or just attaching detaching ?
The right way is to create the appstates only once, so you could load all the relevant objects in the appstate constructor, then when you attach / detach, it will have fast load.

Well, something will take the time if time is going to be taken. If you are only going to attach the app state once then put the initialize logic in initialize… and it will only be done once.

To OP, how long does your screen freeze and have you done some basic debugging to figure out exactly where it’s freezing? Maybe put some System.out.println() calls around or whatever to try to see where it’s hanging.

It normally takes time to load the data into the world, but I would like it to load in the background, and not have my screen freeze. On my laptop it freezes for maybe 30 seconds to a minute, but a faster computer might be a lot faster on load.

It basically freezes as I click the mouse button down, and then when the new appstate is loaded it runs smoothly with no issues.

I will try to do some debugging again, as it’s been awhile since I’ve come back to this issue, and report back.

So basically I’m just looking to stop the freezing and allow some sort of progress bar before the screen switch.

Then load the data on a background thread. Just don’t modify (or access) any attached scene stuff from the other thread.

Ill check it out, thanks. I do need to send some information, because at some point they will click a button and access an option menu in order to load data into the world. Also, how would I setup a progress bar? Do I see certain points in my code to certain values of the progress bar?

http://wiki.jmonkeyengine.org/doku.php/jme3:advanced:loading_screen

This example shows a loading screen with nifty. All of the code would be the same but you will have to adapt it to use tonegod’s GUI.

Much appreciated, i’ll take a look.

Sorry to get back to this so late, but just to make sure, by saying “don’t modify/access any attached scene stuff” that means I shouldn’t try to access the current scene from the background thread/appstate?

Can I access the background thread/appstate data from the current appstate(s)?

Basically what I’m thinking about is. Since I let users create their own worlds, I wanted to load that into the background, without having to load it from file.

Loading them from file takes a few second to a min+ depending on your computer speed/world size. This laptop is about 5 years old, and when the laptop has been on awhile it’s very slow, and it takes about a min to load the world. After a nice shutdown/restart the laptop runs a bit faster so I’ve noticed about a 10s load or so for the test world. On an Asus desktop we got last year it took about 3 seconds to load the world.

So my plan now is to have a folder pop up with the list of worlds, which each world would be loaded into it’s own appstate through a background thread, or multiple background threads (I haven’t done much Multithreading, and none with JMonkey, so I’m curious if there is any limits to the amount of threads we should load at once, or if there is a “nice number” people like to use, and then just reuse threads when the time comes)?

While that’s going on, a user will be clicking on a world(which will still be loading, or be loaded), and it would show a demo of the world in a side panel/container. If they want that world, they can click yes, and be transported into the world, or else pick a different world.

So for this I would either need to take a screen shot of the world, or show a more advanced demo, going through the world.

I would need to be able to load the finished background appstate into a panel or some sort of container and load the data there.

I’ve seen this done on some Nintendo games, so I would assume this is possible with JMonkey?

Thanks as always pspeed.

Data that is not thread safe (ie: the JME scene graph) cannot be accessed simultaneously from multiple threads without protection. The direction doesn’t matter.

The typical process (and covered well in the threading page on the wiki, I think) is to use a background process to load some spatial/thing/whatever… then pass that to a queue that the render thread will poll once a frame. Or just enqueue a Callable with Application to do the work of adding it to the scene on the render thread… as covered by the threading page on the wiki.