Issue with detaching appstates?

Hello all!

I am trying to detach an appstate that is added originally to the game, and seem to be having trouble with detaching it

Right now I am attaching my gui, then trying to remove it with no luck. I try things like detach(new AppState()); detech(stateManager.getState(AppState.class); making a new instance of the class, etc…

So what is the correct way to do this, and why am I having such difficulties?

I attach a screen to my gui node, and I make sure to remove it during cleanup.

the other appstates attach fine, this one just wont detach.

Thanks.

@KonradZuse said: Hello all!

I am trying to detach an appstate that is added originally to the game, and seem to be having trouble with detaching it

Right now I am attaching my gui, then trying to remove it with no luck. I try things like detach(new AppState()); detech(stateManager.getState(AppState.class); making a new instance of the class, etc…

So what is the correct way to do this, and why am I having such difficulties?

I attach a screen to my gui node, and I make sure to remove it during cleanup.

the other appstates attach fine, this one just wont detach.

Thanks.

If you instantiate the AppState like so:

[java]
SomeAppState someAppState = new SomeAppState();
stateManager.attach(someAppState);
[/java]

You can remove it a couple of different ways:

[java]
// By reference:
stateManager.detach(someAppState);
// By class:
stateManager.detach(SomeAppState.class);
[/java]

Both should work fine. I don’t know if you can have two instances of the same AppState (likely you can)… so, if more than one exist, the second method will remove the first instance returned.

@KonradZuse said: Hello all!

I am trying to detach an appstate that is added originally to the game, and seem to be having trouble with detaching it

Right now I am attaching my gui, then trying to remove it with no luck. I try things like detach(new AppState()); detech(stateManager.getState(AppState.class); making a new instance of the class, etc…

So what is the correct way to do this, and why am I having such difficulties?

I attach a screen to my gui node, and I make sure to remove it during cleanup.

the other appstates attach fine, this one just wont detach.

Thanks.

This:
detach(new AppState());

Is a sign that you are very new to Java. That won’t detach an existing app state because you are creating a brand new app state right there… one that has never been attached. It really is a good idea to know more Java before attempting something advanced like 3D programming in a Java game engine.

This:
detech(stateManager.getState(AppState.class);

…will detach the first app state… regardless of what it is. The first app state is likely one that JME added for you. For this to work you’d actually have to use your app state class in that method call.

Beyond this, we can’t help without actually seeing the code… but really I strongly suggest getting a little more up to speed on Java until things like the difference between an existing object and a new object are clearer.

Hi there,

You definitely don’t want to detach a new AppState

You’d do

stateManager.detach(stateManager.getState(YOURSTATEMANAGER.class));’

This is the method I used.

@t0neg0d said: [java] // By class: stateManager.detach(SomeAppState.class); [/java]

Note: this method does not exist… though I’ve meant to add it about 400 times.

You must detach a specific reference… either one kept around or one retrieved with getState(SomeAppState.class).

@pspeed said: Note: this method does not exist... though I've meant to add it about 400 times.

You must detach a specific reference… either one kept around or one retrieved with getState(SomeAppState.class).

Thanks… not sure why I thought it was there. Appreciate you clarifying this. This is what happens when you try and remember more than … breath in… breath out… well… for me at least.

Oh! it was the getState I was thinking of… yep… you even said it and it escaped me >.<

@t0neg0d said: Thanks... not sure why I thought it was there. Appreciate you clarifying this. This is what happens when you try and remember more than ... breath in... breath out... well... for me at least.

I’ve refactored this class in a major way some time back and I still forget and try to use that method. I keep going back and forth on whether to actually add it, though. Convenient for 90% of the cases and really dangerous for anyone adding multiple states of the same class. AppStateManager totally supports that case but it doesn’t make it very nice to manage… someday I will fix that and then maybe I will add the detach(class) method.

@pspeed said: This: detach(new AppState());

Is a sign that you are very new to Java. That won’t detach an existing app state because you are creating a brand new app state right there… one that has never been attached. It really is a good idea to know more Java before attempting something advanced like 3D programming in a Java game engine.

This:
detech(stateManager.getState(AppState.class);

…will detach the first app state… regardless of what it is. The first app state is likely one that JME added for you. For this to work you’d actually have to use your app state class in that method call.

Beyond this, we can’t help without actually seeing the code… but really I strongly suggest getting a little more up to speed on Java until things like the difference between an existing object and a new object are clearer.

Just did it a few ways to see what would happen, I know about what creating a new instance should do.

AS for the other methods I have tried those with no luck

@t0neg0d said: If you instantiate the AppState like so:

[java]
SomeAppState someAppState = new SomeAppState();
stateManager.attach(someAppState);
[/java]

You can remove it a couple of different ways:

[java]
// By reference:
stateManager.detach(someAppState);
// By class:
stateManager.detach(SomeAppState.class);
[/java]

Both should work fine. I don’t know if you can have two instances of the same AppState (likely you can)… so, if more than one exist, the second method will remove the first instance returned.

I tried both but no go on them, I’ll try again another way and see if maybe I have more than 1 attached.

as to pspeed, I know what creating a new instance should do, It was just to see if it would work, because you know, weirder things have worked :p.

@KonradZuse said: as to pspeed, I know what creating a new instance should do, It was just to see if it would work, because you know, weirder things have worked :p.

Not really. It was the equivalent of running a program on someone else’s computer and having it fix a problem on yours. I hope nothing stranger than that has ever happened to you.

Since you are kind of new-ish to Java, I will offer some debugging advice.

First:
MyAppState state = stateManager.getState(MyAppState.class);

…where “MyAppState” is the exact class of your app state. Not AppState… but your class name.

Second:
System.out.println(“State:” + state);

Then:
stateManger.detach(state);

…if this doesn’t detach the state then you are testing to see if the state is detached incorrectly or you have multiple states attached of the same class… in which case you can try getState(MyAppState.class) + println again to see if that’s true.

But if there is only one state and detach seems not to detach it… then your method of detection of what is detached is busted.

@pspeed said: Not really. It was the equivalent of running a program on someone else's computer and having it fix a problem on yours. I hope nothing stranger than that has ever happened to you.

Since you are kind of new-ish to Java, I will offer some debugging advice.

First:
MyAppState state = stateManager.getState(MyAppState.class);

…where “MyAppState” is the exact class of your app state. Not AppState… but your class name.

Second:
System.out.println(“State:” + state);

Then:
stateManger.detach(state);

…if this doesn’t detach the state then you are testing to see if the state is detached incorrectly or you have multiple states attached of the same class… in which case you can try getState(MyAppState.class) + println again to see if that’s true.

But if there is only one state and detach seems not to detach it… then your method of detection of what is detached is busted.

Well… There was this one time in Computer Camp…

Okay so one of my examples was, again stateManager.getState(StartScreenAppState.class);

using this method this.stateManager.detach(stateManager.getState(StartScreenAppState.class));

I didn’t debug on this one method (since it did what the others did), so I see now my approach was correct, but something isn’t getting removed.

inside my AppState I do.

[java]gui.addControl(screen);
//screen.initialize();

    Window panel = new Window(screen, new Vector2f(0,0), new Vector2f(settings.getWidth(), settings.getHeight()));
    //screen.addElement(panel);
    panel.setIsMovable(false);
    panel.setIsResizable(false);
    screen.addElement(panel);
    

    ButtonAdapter openP = new ButtonAdapter(screen, new Vector2f((settings.getWidth()/2f), settings.getHeight() * 0.45f))
    {
       @Override
       public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled)
       {
           openPlan();
       }
       
    };
    openP.setPosition(openP.getPosition().x-openP.getWidth()/2f, openP.getPosition().y);
    openP.setText("Open Plan");

    panel.addChild(openP);

    
}[/java]

Then for the cleanup which is where the problem lies

[java] @Override
public void cleanup() {
super.cleanup();
gui.removeControl(screen);
//TODO: clean up what you initialized in the initialize method,
//e.g. remove all spatials from rootNode
//this is called on the OpenGL thread after the AppState has been detached
}[/java]

From checking the debugger the screen is being removed, so something else must be hanging around…?

Also Pspeed, no need to treat me like a noob, nor are the comments needed, just because I posted something you don’t approve of.

I didn’t make myself clear when I said AppState.class or new AppState, as I meant my own AppState class (StartScreenAppState), and not something else.

I guess I should have looked at my last approach more before posting, since I obviously caused a bit of confusion, but at least I’m on the right path to figuring this out.

I do appreciate your help regardless, as well as everyone else’s.

Thank you.

EDIT: So I needed to call gui.detachAllChildren(); Just this will remove my start menu; however I need to ask about the t0neG0dGUIScreen. It is still attached if I remove all the children, but the question is.

  1. Should I remove the screen still?

  2. The Screen is where all of my buttons are added it seems (in this case I only posted one), so why does nothing happen when it’s removed (Is it because the window is added to the gui node hierarchy separate from the Screen)?

[java]
gui.addControl(screen);

screen.addElement(panel);

    panel.addChild(register); 
    panel.addChild(openP);
    panel.addChild(options);
    panel.addChild(about);
    panel.addChild(exit);[/java]

It’s weird because there is a single node under “gui,” and from there 5 additional nodes. 3 are “AudioNodes” (stopped), 1 is the background, and then comes the window, which is what I want.

Thanks