Niftygui Problems

I’m trying to figure out how Nifty works , but i ran into a problem.

I made a button that calls a function of the controller and it works. But as i can understand Nifty make his own instance of the controller(?) cause in this example the “i” variable created and increased in the update loop doesn’t got the same result when i press the button ,it only increase his “own” i variable. Is this how it works,or do i miss something?

[xml]<?xml version=“1.0” encoding=“UTF-8”?>

<nifty>

<useStyles filename=“nifty-default-styles.xml” />

<useControls filename=“nifty-default-controls.xml” />

<screen id=“welcomeScreen” controller=“mygame.Main”>

<layer id=“firstLayer” childLayout=“center”>

<panel id=“firstPanelParent” width=“100%” height=“100%” size=“40” backgroundColor="#fb8484" childLayout=“vertical”>

<text id=“deepestLabel” text="***************" font=“console.fnt” size=“50” align=“center”/>

<control name=“button” label=“Send”>

<interact onClick=“makeStage()” />

</control>

</panel>

</layer>



</screen>

</nifty>

[/xml]

[Java code]

[java] NiftyJmeDisplay niftyDisplay;

public static void main(String[] args) {

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp() {

i++;

niftyDisplay = new NiftyJmeDisplay(

assetManager, inputManager, audioRenderer, guiViewPort);

/
Create a new NiftyGUI object /

Nifty nifty = niftyDisplay.getNifty();

/
Read your XML and initialize your custom ScreenController */

nifty.fromXml(“Interface/test.xml”, “welcomeScreen”);

// attach the Nifty display to the gui view port as a processor

guiViewPort.addProcessor(niftyDisplay);

// disable the fly cam

flyCam.setDragToRotate(true);

System.out.println(i+“START”);



}



@Override

public void simpleUpdate(float tpf) {

//TODO: add update code

System.out.println(i+“UPDATE”);

i++;



}



@Override

public void simpleRender(RenderManager rm) {

//TODO: add render code

}



public void bind(Nifty nifty, Screen screen) {



}

public boolean inputEvent(final NiftyInputEvent inputEvent) {

return false;

}





public void onStartScreen() {



}

public void makeStage(){

System.out.println(i+“STAGE”);



i++;





}

public void onEndScreen() {



}

}

[/java]

Huh, I’m not sure what you’re expecting exactly…



I take it “i” is declared as a class global variable. From what I’m seeing, “i” will get incremented every frame and when you hit the button it’ll simply print the value of “i” then increase it, effectively increasing it twice in the same frame.

Yes,its a class global variable.But here is the problem:

Let’s say that the “i” variable is now 50 through the update function.

If i press the button to display the "i’ it will display 1 (the starting value) and the increase it by one.Then, whenever i press the button it will increase it by one so the next value is 2,3,4 etc. It just isn’t the same as the global one.

I’m not sure exactly what’s going on, but

  1. where are you declaring i?
  2. if it’s an int, try making it an Integer, perhaps? ints are not pass-by-reference, so maybe this has something to do with why the xml call of the function isn’t working.
  1. I am declaring it just after the Main class declaration,above the “NiftyJmeDisplay niftyDisplay;”.I tried with a boolean and integer too and same result.
  2. This is just an example. What i’m really trying to do is to press the button , then populate the stage of my game and remove the niftyDisplay . But i keep get some warnings and nothing happens,neither the population or the nifty removal.

Using i where it is is asking for troubles.



Make yourself an app state (check the wiki) that will keep track of what you want to do. Then use that to play with you “i”. Or if you don’t want to use an app state, use a separate class.