[SOLVED] Detaching an AppState inside an AppState

Hi guys! I have a question that makes me thinking: is it possible to detach an appstate inside an appstate?
An example: I have the GameplayAppState, and I want to switch to the menu. Something like stateManager.getState(GameplayAppState).detach(). I have been using the cleanup() method, but the update() loop keeps running. How do I do it?

Thanks,
Ev1lbl0w

stateManager.detach(appState) - this also calls your cleanup method.

Ok, thanks. But what should I do?

  • Create a class field of my appstate inside the appstate so I can detach it?
    (public GameplayAppstate {
    private GameplayAppState state;
    })

  • Create a mainAppState that will attach and detach all appstates in the game?

Its not my application, how am I supposed to know? You have access to the stateManager in each AppState.

Yeah, I know. But I can’t do stateManager.detach(GameplayAppState.class). I must declare a class field somewhere. Should I declare it inside the appstate or in another place?

Why do you need a field? A normal variable will do.

AppState state = stateManager.getState(MyState.class);
stateManager.detach(state);

or simply

stateManager.detach(stateManager.getState(MyState.class));

Ok, thanks! That solves my problem! Sorry for being so newbie XD

If the AppState needs to remove itself, then:

stateManager.detach(this);

I tried to do that, but I detach my appstate inside the onAction() method of the ActionListener. In that case, this refers to ActionListener. But can I make this refer to the appstate inside that method?

Show us code… then we won’t have to give you random answers until one sticks.

Sure!

private ActionListener actionListener = new ActionListener() { public void onAction(String name, boolean isPressed, float tpf) { if(name.equals(MAPPING_BUILDING) && isPressed) { stateManager.detach(this); } } };

The “this” points to the ActionListener, not the AppState itself.

Btw how to I post code nice? I use this type, but is ugly…

That’s not enough code. I can’t tell you how to reference the outer instance unless I know what the class is called. Err… maybe I should write my response like this:

“ugh code. I can’t tell you how to refere”

I’ve on this forum actively for like 4 years now and not ONCE has anyone complained about too much information… but 95% of posts request more information.

re: Code formatting… maybe we should put a stick post right on the front page of the forum… oh wait. We did. :wink:

Three back ticks ` before and after the code is the best way, though… I’ll save you some reading through irrelevant options.

you can do

stateManager.detach(stateManager.getState(GameplayAppState.class));

though.

How about this:

stateManager.detach(MyAppState.this);

MyAppState being your AppState class where I think you are currently in. Inside your AppState’s ActionListener.

My problem is already resolved, nehon. I just want to know how to make the class point outside the ActionListener. Like this.

public class GameplayAppState extends AbstractAppState {

    ... rest of the code

    private ActionListener actionListener = new ActionListener() {
        public void onAction(String name, boolean isPressedd, float tpf) {
            if(name.equal(MAPPING_BUILDING) && ispressed) {
                stateManager.detach(this);
            }
        }
    }

    ... rest of the code
}

I’m not posting the entire code because this is the part that matters. But if the whole code is needed, then I’ll post it.

Thanks pspeed for the button to show code :smile:

oh ok

stateManager.detach(GameplayAppState.this);

Yeah, it’s exactly that! Thanks nehon and tonihele!

or just implements the interface “ActionListerner” and you then you will be able to use “this” :

public class MyAppState extends AbstractAppState implements ActionListener{
    public void onAction(String name, boolean isPressed, float tpf) {
            stateManager.detach(this);
    }

You can do it this way but it’s kind of stinky.