GUI Interaction Tutorial Question

Hi…first post. I’ve been working through some of the tutorials, most recently this Nifty GUI one.

A couple of simple questions:

  1. The Start button is working fine, advancing to the hud screen. But the quit button is not doing anything. The behavior of calling app.stop() should be to exit the game, correct? I’m initializing app per the instructions and everything looks as it should be, but nothing is happening.

  2. And this is a really silly question, but I can’t seem to get logging to work. I create a logger and set the log level, but don’t see any output for log messages. They should appear in the console of the SDK when the game is running, yes?

Thanks.

That is correct. Maybe you broke something in your logging configuration?

Try just doing a system.out.println and verify you can see that. Logging should output to the same location.

Thanks. Yes, system.out.println appears to be generating output. It was a bit hard to see at times due to the volume of default info logging. I set the logging level to severe and was able to see my output.

Per my first question, app.stop() still does not appear to be doing anything. In the example, this is being called from a class that implements ScreenController and extends AbstractAppState, which the tutorial states " that you have access to the application app object because you made the ScreenController extend AbstractAppState." The tutorial also states that the app object needs to be initialized before its methods can be called. I’m not sure what this means. I’ve tried calling app.initialize() in various places, and still nothing happens when app.stop() is called.

app is your application. The one that subclasses SimpleApplication. It has to be running before it can be stopped.

How are you getting the app reference that you are calling stop on? app.stop() should stop the application unless something else strange is going on.

Per the tutorial I linked to in the first post, I have two classes:

  1. Main, which extends SimpleApplication, calls app.start(), and lays out all the nifty gui stuff.
  2. MyStartScreen, which extends AbstractAppState, implements ScreenController, and has the custom methods that are called when the nifty buttons are clicked.

The tutorial says: “Pro Tip: Since you are writing a jME3 application, you can additionally make the ScreenController class extend the AbstractAppState class! This gives the ScreenController access to the application object and to the update loop!”

I’ve implemented everything as stated through part 5 of the tutorial. I know the quitGame() method is being called when the button is clicked because my logging is now working properly and I put a line of code to log when the button is clicked. :slight_smile:

However, thought app.stop() is in the quitGame method, it is having no effect on the state of the game. Has this tutorial been updated recently? Is there something missing, or am I just doing something wrong?

@polyclef said: Per the tutorial I linked to in the first post, I have two classes:
  1. Main, which extends SimpleApplication, calls app.start(), and lays out all the nifty gui stuff.
  2. MyStartScreen, which extends AbstractAppState, implements ScreenController, and has the custom methods that are called when the nifty buttons are clicked.

The tutorial says: “Pro Tip: Since you are writing a jME3 application, you can additionally make the ScreenController class extend the AbstractAppState class! This gives the ScreenController access to the application object and to the update loop!”

I’ve implemented everything as stated through part 5 of the tutorial. I know the quitGame() method is being called when the button is clicked because my logging is now working properly and I put a line of code to log when the button is clicked. :slight_smile:

However, thought app.stop() is in the quitGame method, it is having no effect on the state of the game. Has this tutorial been updated recently? Is there something missing, or am I just doing something wrong?

I haven’t looked at the tutorial.

One issue with nifty is that it swallows all exceptions in the handler methods. So if app.stop() is actually throwing a null pointer exception then you will never see it. Just in case, try putting a println after the app.stop() to see if it gets called.

My suspicion is that intiailize() was never called on your app state because it was never registered. Looking at the tutorial, I see that it offers that advice but on quick glance I don’t see it attaching it to the state manager anywhere.

If you want to use that approach (and it is a good one) then you will have to keep a reference to:
new tutorial.MyStartScreen()
…pass it to the nifty controller thing and also stateManager.attach() it.

Thanks, it’s working now.

I created an instance of MyStartScreen at the beginning of simpleInitApp(), then called initialize on it before passing it into the controller. At first I initialized it like this:

myStartScreen.initialize(null, this);

I wasn’t sure what the StateManager was or where I had access to it, so I tried initializing without passing it in, and it still worked, though I’m guessing this is not a good idea in general? I then looked at the Simple App States demo to try to understand StateManager a little better, and figured out where to pass it into my initialize method.

So anyway, it works. I’d suggest updating the tutorial, though, since it would only take a couple of lines of code and it is really confusing when you implement the tutorial code and it just doesn’t work. There is the “advice” to initialize the app state, which really isn’t advice since it’s not optional for the example to function, and it’s also not obvious how or why to do this to those new to the SDK.

I’ll keep plugging away through the tutorials. I’m sure I’ll have more questions. Thanks for the timely feedback.

You shouldn’t call initialize() on app states yourself. You should attach them to the state manager and that will then call initialize.

@polyclef said: Thanks, it's working now.

I created an instance of MyStartScreen at the beginning of simpleInitApp(), then called initialize on it before passing it into the controller. At first I initialized it like this:

myStartScreen.initialize(null, this);

I wasn’t sure what the StateManager was or where I had access to it, so I tried initializing without passing it in, and it still worked, though I’m guessing this is not a good idea in general? I then looked at the Simple App States demo to try to understand StateManager a little better, and figured out where to pass it into my initialize method.

So anyway, it works. I’d suggest updating the tutorial, though, since it would only take a couple of lines of code and it is really confusing when you implement the tutorial code and it just doesn’t work. There is the “advice” to initialize the app state, which really isn’t advice since it’s not optional for the example to function, and it’s also not obvious how or why to do this to those new to the SDK.

I’ll keep plugging away through the tutorials. I’m sure I’ll have more questions. Thanks for the timely feedback.

stateManager.attach( theState )

Never call initialize() directly on an AppState. Either make your class not an app state and call initialize (basically rolling your own support) or attach the state to state manager and let it call initialize.

AppStateManager is a field on Appliction so you have access to it from in your SimpleApplication subclass:
http://hub.jmonkeyengine.org/javadoc/com/jme3/app/Application.html
http://hub.jmonkeyengine.org/javadoc/com/jme3/app/Application.html#stateManager

Got it…thanks. I am now attaching myStartScreen like so:

stateManager.attach(myStartScreen);

Works fine.