Nifty problem with burron controller

Hello

I got button in my GUI .xml code which should call quit() method from MenuState class but when i press that button nothing happen.

Here’s GUI.xml code:
[java]<?xml version=“1.0” encoding=“UTF-8”?>
<nifty>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />
<screen id=“start” controller=“MenuState”>
<layer id=“bgLayer” backgroundImage=“Textures/GFX/Interface/menuElements/bg.jpg” childLayout=“center”>
<panel id=“logoPanel” height=“536” width=“728” backgroundImage=“Textures/GFX/Interface/menuElements/tlt.png” childLayout=“center” visibleToMouse=“true”>
</panel>
<control name=“button” label=“Start” id=“StartButton” align=“center” valign=“center” visibleToMouse=“true”>
<interact onClick=“quit()”/>
</control>
</layer>
</screen>
<screen id=“end” controller=“MenuState”>
</screen>
</nifty>
[/java]
And my MenuState.java class:
[java]public class MenuState extends AbstractAppState implements ScreenController{

private AssetManager assetManager;
private Nifty nifty;
private Screen screen;
private InputManager inputManager;
private AudioRenderer audioRenderer;
private ViewPort guiViewPort;
private final Node rootNode;
private final ViewPort viewPort;
private final Node guiNode;
private NiftyJmeDisplay niftyDisplay; 
private Game game;

public MenuState(SimpleApplication sapp){
this.rootNode     = sapp.getRootNode();
this.viewPort      = sapp.getViewPort();
this.guiNode       = sapp.getGuiNode();

}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);

assetManager = app.getAssetManager();
inputManager = app.getInputManager();
audioRenderer = app.getAudioRenderer();
guiViewPort = app.getGuiViewPort();
niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);

nifty = niftyDisplay.getNifty();
nifty.fromXml(“Interface/GUI.xml”, “start”, this);
guiViewPort.addProcessor(niftyDisplay);
inputManager.setCursorVisible(true);
}

@Override
public void update(float tpf) {
/** the action happens here */
}

public void quit() {
game = new Game();
game.detachMenu();
game.attachGame();
}

@Override
public void stateAttached(AppStateManager stateManager) {
}

@Override
public void stateDetached(AppStateManager stateManager) {
}

    public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
nifty.registerScreenController(this);
nifty.addControls();
}

public void onStartScreen() {
}

public void onEndScreen() {
}

}[/java]
There’s also a warning in console:

WARNING: class [MenuState] could not be instantiated (java.lang.ClassNotFoundException: MenuState) lis 10, 2013 2:23:51 PM de.lessvoid.nifty.screen.Screen <init> WARNING: Missing ScreenController for screen [start] using DefaultScreenController() instead but this might not be what you want. lis 10, 2013 2:23:52 PM de.lessvoid.xml.tools.ClassHelper getInstance WARNING: class [MenuState] could not be instantiated (java.lang.ClassNotFoundException: MenuState) lis 10, 2013 2:23:52 PM de.lessvoid.nifty.screen.Screen <init> WARNING: Missing ScreenController for screen [end] using DefaultScreenController() instead but this might not be what you want.

are you using a default package? (no package)

if not, the controller needs the full length, i.e com.mygame.MenuState

I changed to sourceGame.MenuState and now when i press button i get a lot of:

[java]WARNING: Exception: java.lang.reflect.InvocationTargetException
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:57)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:43)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.Method invoke (Method.java:601)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:104)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:197)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:261)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sourceGame.MenuState startGame (MenuState.java:71)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)
lis 10, 2013 4:19:51 PM de.lessvoid.nifty.NiftyMethodInvoker logException
[/java]

and nothing happening

replace (temporary) the code of your method quit by this :

[java]

System.exit(0);

[/java]

or even

[java]

System.out.println(“i leave”);

[/java]

If it works (i.e. if the app quit with the first code or display “i leave” with the second) it means that you have a bug with your actual code. For exemple, when you detach something it throws a nullpointerexception or something like that.

As nifty eat exceptions, you doesn’t have a clear trace of it - but it’s somewhere in the log anyway.

So, if the temporary replacement works as expected, do back into your code and find the source of the problem.

I think that a try-catch in your method + a full log of the exception will “re-enable” the stack trace. But remember that your are very lucky to don’t be in c++ : there, is you do an exception your program crashes and you don’t know from where the bug comes (no line, no method nothing). Here, you have the log somewhere in the stack (even if a bit distorded by nifty)…

Okey i’ll try but do i have my MenuState correct? I mean all nifty’s methods lines etc

I commented line [java]stateManager.attach(gameState);[/java] and only left: [java] public void detachMenu() {
stateManager.detach(menuState);
System.out.println(“i leave”);
}[/java]
And when i press button it print i leave with no errors but Menu is not dettached lol

@Skatty said: I commented line [java]stateManager.attach(gameState);[/java] and only left: [java] public void detachMenu() { stateManager.detach(menuState); System.out.println("i leave"); }[/java] And when i press button it print i leave with no errors but Menu is not dettached lol

How do you know? Do you mean that the visuals are still there or that you’ve somehow determined that the app state is still there?

I can guarantee you one thing: the app state is definitely detached.

Your app state may not be cleaning itself up properly but it is definitely detached and cleanup() is definitely called. Just stick some println() in there and you’ll see.

Should I put some code into

[java] @Override
public void stateDetached(AppStateManager stateManager) {
}[/java]
in my MenuState to manually remove visual objects?

@Skatty said: Should I put some code into

[java] @Override
public void stateDetached(AppStateManager stateManager) {
}[/java]
in my MenuState?

No. Never override that method. Never. Nor stateAttached().

I’m not really clear what you want to happen here, actually. Your MenuState sets up all of nifty right inside of it. So technically it should be undoing that in cleanup()… but then you can’t use nifty anymore.

The assumption is that you want to detach the MenuState and do other things that might need nifty later… in which case you should be setting nifty up in a more central location and just use it from other states (like MenuState).

Ok but i got same nifty code in my GameState. And i get all the strange warnings when pressing button and also GameState components (game) is doesn’t show up.

EDIT: for test i copied all code from MenuState and created new class called TestState.

in GUI i created new screen and set controller to sourceGame.TestState. Game.java now should attach TestState and i get also warnings and WARNING: Missing ScreenController for screen [startTest]

Should i delete somehow controlls after detaching state?

@Skatty said: Ok but i got same nifty code in my GameState. And i get all the strange warnings when pressing button and also GameState components (game) is doesn't show up

I don’t have time to teach you game architecture at the moment so you will have to track down one of the examples posted. And debugging nifty apps is a special kind of hell for me.

It’s especially hard because only you can see what you’ve actually done. But it makes sense that our game state never appears since the last posts I saw said you commented out attaching it. I stepped in because you said your state wasn’t being detached… but it is.

Hopefully those who live nifty every day can help your further sort your architecture out.

Here’s what pressing button in menu do:

[java] public void startGame() {
game = new Game();
game.detachMenu();
nifty.gotoScreen(“game”);
game.attachGame();
}[/java]

Menu detach work. It also change screen into “game” which display simple game’s logo. Attach game also work because it print [java]System.out.print(“HELLO WORLD”);[/java] from bind method in GameState.

Only game doesn’t show up and i get all that warnings. ;/