My button isn't working

I’m experimenting with Nifty so I can make a weapon design program for one of my games, and I didn’t want to make a(nother) mess in my main project.

So basically I took the jme planet demo, and attempted to add 2 buttons to it.

A “create planets button” and a quit button

Here’s the XML

                <!-- add your content here -->

[java]public void CreatePlanets(){
System.out.println(“The planets should be created!”);
app.initPlanets();
nifty.gotoScreen(“blank”);
}[/java]
where app is the planet application
[java]public void initPlanets(){
// Add planet app state
stateManager.detach(startState);
stateManager.attach(planetAppState);

    // Add planet
    Planet planet = Utility.createEarthLikePlanet(getAssetManager(), 63710.0f, 800f, 4);
    planetAppState.addPlanet(planet);
    rootNode.attachChild(planet);
    
    // Add moon
    Planet moon = Utility.createMoonLikePlanet(getAssetManager(), 20000, 300, 5);
    planetAppState.addPlanet(moon);
    rootNode.attachChild(moon);
    moon.setLocalTranslation(-150000f, 0f, 0f);
    started=true;
}[/java]

Here’s the quit one.
[java]
public void Quit(){
app.destroy();
}
[/java]

Anyway neither works and I’ve fiddled with it for a while it’s starting to annoy me.

If CreatePlanets() and Quit() are on your screen controller then how does it get its reference to app?

Have you put a println in these methods to see if it is even called?

My guess, since nifty swallows all exceptions is that you are getting a NullPointerException in these methods because your controller doesn’t have a proper reference to the application.

@pspeed said: If CreatePlanets() and Quit() are on your screen controller then how does it get its reference to app?

Have you put a println in these methods to see if it is even called?

My guess, since nifty swallows all exceptions is that you are getting a NullPointerException in these methods because your controller doesn’t have a proper reference to the application.

I did not include this when I posted, but in theory this gets called?
I do not see any manual calling of initialize in the nifty demo but they have an app object that gets initialized in the same way. I merely added (PlanetSimpleTest) instead of (SimpleApplication) or whatever.

[java]
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app=(PlanetSimpleTest)app;
}
[/java]

Also I am trying to post the XML but it wont let me :confused:

That only gets called if the app state is attached to the state manager. I’m assuming it’s a screen controller + app state. If you let nifty create it on its own then the initialize() will never be called because it does not get attached to the state manager. You have to create it in your code, attach it to the state manager, and give it to nifty when the XML is loaded.

@pspeed said: That only gets called if the app state is attached to the state manager. I'm assuming it's a screen controller + app state. If you let nifty create it on its own then the initialize() will never be called because it does not get attached to the state manager. You have to create it in your code, attach it to the state manager, and give it to nifty when the XML is loaded.

I am doing that too.

[java]
startState = new StartScreen();
stateManager.attach(startState);
/**
* Åctivate the Nifty-JME integration:
*/
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(
assetManager, inputManager, audioRenderer, guiViewPort);
Nifty nifty = niftyDisplay.getNifty();
guiViewPort.addProcessor(niftyDisplay);
nifty.fromXml(“Interface/newbutton.xml”, “start”, startState);

    flyCam.setDragToRotate(true);
    inputManager.setCursorVisible(true);[/java] 

The buttons are showing, they are highlighting on mouseover, and they are changing color when I click them, but nothing happens and my printouts don’t print out.

Then I don’t know. Put a println in the methods to see if they are even being called… then see what they aren’t working.

1 Like
@pspeed said: Then I don't know. Put a println in the methods to see if they are even being called... then see what they aren't working.

That’s hard to do because of the other problem I am having: I cant turn off the damn nifty logger

I’ll try a System.out.println(“Place 1”) place 2 place 3 sort of thing and see if I can figure anything out.

@pspeed said: Then I don't know. Put a println in the methods to see if they are even being called... then see what they aren't working.

Do I have to call bind manually? I didn’t see it anywhere in the tutorial demo thing.

[java]
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
System.out.println(“Bind happens”);
}[/java]

That “bind happens” never printed out.

Bind is called by Nifty automatically when you register the screen controller. (I think it actually happens just before the first call to tell you the screen has started).

Double check your xml has the correct full name (including package) for your controller class. Your registering of the state and controller is correct so that’s the only other thing I can think of that would stop bind being called.

1 Like
@zarch said: Bind is called by Nifty automatically when you register the screen controller. (I think it actually happens just before the first call to tell you the screen has started).

Double check your xml has the correct full name (including package) for your controller class. Your registering of the state and controller is correct so that’s the only other thing I can think of that would stop bind being called.

Lol… jmeplanet.test.StartScreen
Instead of test.StartScreen

Okay that’s one bug down, thanks a lot.

1 Like

Process of elimination - and I’ve been caught by that one myself once - good luck with the rest of your project :slight_smile: