AppState

I’m trying to learn how to use AppStates. One question that arose immediately is whether the update() should get called automatically each time in the simpleUpdate() or whether I should call it myself.



This is what I have so far:



[java]

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    */

    package mygame;



    import com.jme3.app.Application;

    import com.jme3.app.SimpleApplication;

    import com.jme3.app.state.AbstractAppState;

    import com.jme3.app.state.AppStateManager;

    import com.jme3.scene.Node;



    /**

    *
  • @author Mamo

    */

    public class AppState extends AbstractAppState {



    private SimpleApplication app;



    private Node x = new Node("x");



    public Node getX(){

    return x;

    }



    @Override

    public void initialize(AppStateManager stateManager, Application app) {

    super.initialize(stateManager, app);

    this.app = (SimpleApplication)app;

    }



    @Override

    public void cleanup() {

    super.cleanup();

    }



    @Override

    public void setEnabled(boolean enabled) {

    super.setEnabled(enabled);

    if(enabled){



    } else {



    }

    }



    @Override

    public void update(float tpf) {

    super.update(tpf);

    if(isEnabled()){

    System.out.println("Enabled");

    } else {

    System.out.println("Not Enabled");

    }

    }



    }

    [/java]



    In the Main:



    [java]

    AppState x = new AppState();

    x.initialize(stateManager, this);

    x.setEnabled(true);

    [/java]

update is called automatically you do not have to care about that once it’s attached to the stateManager.

1 Like

stateManager.attachState(x)?

1 Like

That’s what was missing. I wasn’t attaching it. Thanks a lot.

1 Like

You do not need to initialize app states. It is done automagically if it isn’t when it is encountered by the AppStateManager the first time. That doesn’t mean you can’t though, but it should be left alone IMO.

1 Like

I didn’t understand what you said. When should it be initialized automatically?

[java]

AppState x = new AppState();

x.initialize(stateManager, this); // <-- This is not necessary since…

x.setEnabled(true);

[/java]



In AppStateManager, it will do this itself for you.

[java]

/**

  • Calls update for attached states, do not call directly.
  • @param tpf Time per frame.

    */

    public void update(float tpf){



    // Cleanup any states pending

    terminatePending();



    // Initialize any states pending

    initializePending(); // <

Just right here.

// Update enabled states
AppState[] array = getStates();
for (AppState state : array){
if (state.isEnabled()) {
state.update(tpf);
}
}
}
[/java]

If you're not too sure open AppStateManager yourself and check the code. It's pretty much self-explanatory.
2 Likes

OK. I moved the [java]

stateManager.attach(x);[/java] to the Main, because else it would never get initialized. It works well now. Thanks.



Btw, last question: I assume there can be more than one custom AppState running, right?

You can have any number of states attached and/or active at any one time.



What I’m doing (that seems to work well so far) is to have a few states that are “main” and only one of those is active at a time. I also have a number of other states that are always active running things that are always wanted.

Yes. It can have as many you need. Just remember that ordering could be important. Those that are attached first are updated first. So, if you have an app state that is dependent on another this could make a difference whether it’s updated before of after…



Enabling an appstate can be done pretty much anywhere, but it doesn’t have to be at creation/instantiation.



Also, if you have to modify the app state’s internals from another class you should do it by getting the app state, not using the class directly.



[java]

appStateManager.getState(SomeAppStateClass.class).doSomething();

[/java]



Edit: Removed code as it wasn’t relevant to the post.

1 Like

Yeah, saw that in a tutorial. Thanks :slight_smile:

@memonick said:
OK. I moved the [java]
stateManager.attach(x);[/java] to the Main, because else it would never get initialized. It works well now. Thanks.

Btw, last question: I assume there can be more than one custom AppState running, right?


You can attach an app state from anywhere that has access to the state manager. If it didn't get initialized then it didn't get attached or your approach for deciding if it was initialized was flawed.

Also, if you use app states then make sure you are using at least the latest stable updates.

I updated from the bottom-right corner. I installed 22 updates a month ago, but since then, no more updates.

@memonick said:
I updated from the bottom-right corner. I installed 22 updates a month ago, but since then, no more updates.


Yeah, that's fine. I was just making sure. I fixed a bunch of app-state related stuff in the last stable build.

So I should be notified by the program whenever there are updates?

@memonick said:
So I should be notified by the program whenever there are updates?


I've set my SDK to check for updates at every startup.

Tools->Plugins->Settings-> Check Interval: Every Startup.

Also, it depends on your situation, but here I update to nightly, in the top left panel make sure jME SDK Nightly is Active if you do want to use the nightly.
1 Like

Memonick, Updating to nightly is for advanced users and is very hard to come back from. Don’t do it unless you feel 100% comfortable trouble-shooting the issues it will cause for you because your only recourse if you go down that path is a full SDK uninstall and reinstall if you hit a dead end.

be wary about using nightly tho, as madjack will testify stuff can break ^^

Wait a little. The first part of @madjack’s post: Tools->Plugins->Settings-> Check Interval: Every Startup.



Is that Nightly, or is it safe?

@wezrule said:
be wary about using nightly tho, as madjack will testify stuff can break ^^


I have no idea what you're talking about. :P lol

Nightly can sometimes be a pain, but it's rare. But when it happens... You hate yourself for using bleeding edge. OTOH, you get the cool features right away. So it's up to you...