Nifty GUI – Variable gets reset after OnClick()

Hi, I really have a strange situation. I got a Nifty GUI Text Element and if i click it, it behaves strange.





My Screencontroller:

[java]

package psiffect.menustates;



import org.lwjgl.input.Keyboard;





public class MM_TitleScreenState extends MM_MenuState{



private int gotoNext = 0;



@Override

public void logic() {

if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)

|| Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {

appMain.itsMainMenuState.start();

}



System.out.println("<PRESSED> Main Check: gotoNext ->" + gotoNext);

}



public void incVar() {

System.out.println("<PRESSED> 1st Check: gotoNext ->" + gotoNext);

gotoNext++;

System.out.println("<PRESSED> 2nd Check: gotoNext ->" + gotoNext);

this.checkVar();

}



public void checkVar(){

System.out.println("<PRESSED> 3rd Check: gotoNext ->" + gotoNext);

}



}

[/java]



MM_MenuState

[java]

package psiffect.menustates;



import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;

import psiffect.gamestates.MainMenuState;

import psiffect.gamestates.MainMenuState.MenuStates;





public class MM_MenuState implements ScreenController{

public Nifty appNifty;

public MainMenuState appMain;

public Screen itsScreen;

public String itsScreenName;

public MenuStates itsMenuState;



public void bind(final Nifty tempNifty,

final MainMenuState tempMenu,

final String tempScreenName,

final MenuStates tempMenuState) {

// appNifty = tempNifty;

appMain = tempMenu;

itsScreenName = tempScreenName;

itsMenuState = tempMenuState;

}



public void start() {

appMain.setMenuState(itsMenuState);

appNifty.gotoScreen(itsScreenName);

this.init();

}



public void init() { }



public void logic(){ }



public void bind(Nifty nifty, Screen screen) {

appNifty = nifty;

itsScreen = screen;

}



public void onStartScreen() { }



public void onEndScreen() { }

}

[/java]



My XML-Element

[xml] <controlDefinition name = “titlescreenPoint”>

<text id=“titletext” color="#ffff" text=“default” font=“Interface/Fonts/abstract.fnt” textHAlign=“center” visibleToMouse=“true” />

</controlDefinition>[/xml]



[xml] <control id=“titlescreen_text” name=“titlescreenPoint” text="[Press.Spacebar]" width=“100%” height=“50%”>

<interact onClick=“incVar()” />

</control>[/xml]



Okay so far, so good. Now when i start my Application it loads everything fine. Then if i click the Text i get the following output:

(The logic methd is called continuously in evry frame)



[java]<PRESSED> Main Check: gotoNext ->0

<PRESSED> Main Check: gotoNext ->0

<PRESSED> 1st Check: gotoNext ->0

<PRESSED> 2nd Check: gotoNext ->1

<PRESSED> 3rd Check: gotoNext ->1

<PRESSED> Main Check: gotoNext ->0

<PRESSED> Main Check: gotoNext ->0[/java]





I don’t know how this can happen :confused:



EDIT: But somewhere gotoNext is saved, because i also gotthis in my Output:

[java]<PRESSED> Main Check: gotoNext ->0

<PRESSED> Main Check: gotoNext ->0

<PRESSED> 1st Check: gotoNext ->1

<PRESSED> 2nd Check: gotoNext ->2

<PRESSED> 3rd Check: gotoNext ->2

<PRESSED> Main Check: gotoNext ->0

<PRESSED> Main Check: gotoNext ->0[/java]



[java]<PRESSED> Main Check: gotoNext ->0

<PRESSED> Main Check: gotoNext ->0

<PRESSED> 1st Check: gotoNext ->2

<PRESSED> 2nd Check: gotoNext ->3

<PRESSED> 3rd Check: gotoNext ->3

<PRESSED> Main Check: gotoNext ->0

<PRESSED> Main Check: gotoNext ->0[/java]

How do you instantiate your ScreenController and how do you access it when you call logic()?

Ok, I don’t want to spam this forum with all my classes. I uploaded my whole project here: www.daftgaming.de/website/myProjects/Psiffect.zip



And to simplify my problem: The main issue seems to be something with calling functions inside the “MM_MenuStates”. For example in the “MM_MainMenuState”. If i click in the mainmenu on exit, the output is displayed, but the method “startExit()” doesnt seem to get called.



The MainStructure:


  • - "Psiffect.java" contains all GameStates. (e.g. "IntroState.java", "MainMenuState.java")

  • - All GameStates extend the class "GameState.java"


  • - "MainMenuState.java" contains all MenuStates. (e.g. "MM_TitleScreenState", "MM_MainMenuState"

  • - All MenuStates extend the class "MM_MenuState.java"



Thanks in advance for bothering with my problem!

Ok, that’s really more code than I asked for. :stuck_out_tongue:

However, after a quick look in your MainMenuState, I found that you first load the Nifty screen from XML without giving it a ScreenController - thus Nifty instantiates an instance of the ScreenController specified in you screen tag (you now have 1 instance).

Then you do this:

[java]itsMainMenuState = new MM_MainMenuState();[/java]

You create an additional instance of the ScreenController (you now have 2 instances).

You should either load your screen with a specified ScreenController instance:

[java]itsMainMenuState = new MM_MainMenuState();

itsNifty.fromXml(“Interface/mainmenu.xml”, “titlescreen”, itsMainMenuState);[/java]

or get the instance Nifty created:

[java]nifty.getCurrentScreen().getScreenController();[/java]

1 Like

Ah ok, i missunderstood the concept of Nifty.



I though you can initiate all screens and then give them via xml different ScreenController.



Now i changed it that way, that everytime a “MenuState’s” “start()” method is called this is executed too:

[java] appNifty.fromXml(“Interface/” + itsScreenName + “.xml”, itsScreenName, this);[/java]

The related XML-File is initiated and delivered with the ScreenController, the class itself, and voilá, its working!

Another sideeffect is that now every menu will get its own xml, and i don’t think thats too bad. Keeps things structured.



Many Thanks Tumaini for helping me solving this problem :smiley: (was sitting 2 days in front of this)

That won’t work either.



Nifty keeps a list of screen controllers internally. The XML specified the screen controller to use for that screen but it always uses the same one. If there is already one in the list it uses it - otherwise it creates one and uses that. Once controllers are added to the list they are never removed and the first matching one is always used. If you keep adding controllers they will keep adding into the list making it longer but the first matching one will always be the one used…



(Void applied my patch a few days ago to allow removing of screen controllers but I don’t know when that patch will make it into the JME version of the libs)



Essentially each XML screen is associated with one and only one screen controller and will always be associated with that one. Just set up/modify values etc in that controller accordingly.