How to access AbstractAppState with nifty?

Hey Guys. I’ve searched the forum, but can’t find answer to this question. How can I access abstract app state variables (say stateManager) in order to manipulate game states with GUI? At the moment I register my screen controllers - everything works fine to a moment where I actually have to take user away from GUI interface to actual game - how can I do that? This is the code that I use. You can clearly see I’m extending AbstractAppState so I should have access to global vars no? Don’t worry about the comments - they’re just pointers for the optimization that will happen later on.



My main Class:

[java]package simulation;



import com.jme3.app.SimpleApplication;

import graphics.Scene;

import graphics.Train;

import java.security.AccessControlException;

import java.util.logging.Level;

import java.util.logging.Logger;

import views.Map;

import views.StartScreen;



public class Simulation extends SimpleApplication

{

StartScreen startScreen = new StartScreen();



public static void main(String[] args)

{

Simulation app = new Simulation();

app.setDisplayStatView(false);

app.setDisplayFps(false);

app.start();

}



@Override

public void simpleInitApp()

{

flyCam.setMoveSpeed(100);

stateManager.attach(startScreen);



}





}

[/java]



StartScreen Class:



[java]package views;



import com.jme3.app.Application;

import com.jme3.app.SimpleApplication;

import com.jme3.app.state.AbstractAppState;

import com.jme3.app.state.AppStateManager;

import com.jme3.asset.AssetManager;

import com.jme3.bullet.BulletAppState;

import com.jme3.input.InputManager;

import com.jme3.renderer.ViewPort;

import com.jme3.scene.Node;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.NiftyEventSubscriber;

import de.lessvoid.nifty.controls.ListBox;

import de.lessvoid.nifty.controls.ListBoxSelectionChangedEvent;

import de.lessvoid.nifty.elements.Element;

import de.lessvoid.nifty.elements.events.NiftyMousePrimaryClickedEvent;

import de.lessvoid.nifty.elements.render.ImageRenderer;

import de.lessvoid.nifty.elements.render.PanelRenderer;

import de.lessvoid.nifty.elements.render.TextRenderer;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;

import de.lessvoid.nifty.tools.Color;

import java.util.List;

import models.LinesDB;



public class SimulationLinesScreen extends AbstractAppState implements ScreenController

{



private SimpleApplication app;

private Node rootNode;

private AssetManager assetManager;

private AppStateManager stateManager;

private InputManager inputManager;

private ViewPort viewPort;

private ViewPort guiViewPort;

private BulletAppState bullet;



private Nifty nifty;

private Screen screen;



// simulationLinesScreen

private LinesDB lineDB = new LinesDB();

private PanelRenderer lineNameBackground;

private TextRenderer lineName;

private ImageRenderer lineImage;

private TextRenderer lineDescription;



@Override

public void initialize(AppStateManager stateManager, Application app) {

super.initialize(stateManager, app);

this.app = (SimpleApplication) app; // can cast Application to something more specific

this.rootNode = this.app.getRootNode();

this.assetManager = this.app.getAssetManager();

this.stateManager = this.app.getStateManager();

this.inputManager = this.app.getInputManager();

this.viewPort = this.app.getViewPort();

this.bullet = this.stateManager.getState(BulletAppState.class);

this.guiViewPort = this.app.getGuiViewPort();

}



public void bind(Nifty nifty, Screen screen) {

this.nifty = nifty;

this.screen = screen;

setUndergroundList();



}



public void onStartScreen() {



}



public void onEndScreen() {



}



@Override

public void update(float tpf) {



}



// Screen listeners

@NiftyEventSubscriber(id=“backToStartScreen”)

public void onBackToStartScreenClick(String id, NiftyMousePrimaryClickedEvent event) {

nifty.gotoScreen(“startScreen”);

}



@NiftyEventSubscriber(id=“startSimulationParameters”)

public void onStartSimulationParametersClick(String id, NiftyMousePrimaryClickedEvent event) {

nifty.gotoScreen(“simulationParametersScreen”);

}



@NiftyEventSubscriber(id=“linesList”)

public void onListBoxSelectionChanged(final String id, final ListBoxSelectionChangedEvent<String> event) {



// NEED OPTIMIZATION!!!

// START

Element lineNameElement = this.screen.findElementByName(“lineName”);

lineName = lineNameElement.getRenderer(TextRenderer.class);



Element lineImageElement = this.screen.findElementByName(“lineImage”);

lineImage = lineImageElement.getRenderer(ImageRenderer.class);



Element lineDescriptionElement = this.screen.findElementByName(“lineDesc”);

lineDescription = lineDescriptionElement.getRenderer(TextRenderer.class);



Element lineNameBackgroundElement = this.screen.findElementByName(“lineNameBackground”);

lineNameBackground = lineNameBackgroundElement.getRenderer(PanelRenderer.class);

// END



List<String> selection = event.getSelection();

for (String selectedItem : selection) {



lineName.setText(this.lineDB.getLineByName(selectedItem).getName());

lineNameBackground.setBackgroundColor(new Color(this.lineDB.getLineByName(selectedItem).getLineColor()));

lineDescription.setText(this.lineDB.getLineByName(selectedItem).getDescription());

lineImage.setImage(this.nifty.getRenderEngine().createImage(this.lineDB.getLineByName(selectedItem).getImage(), false));



}

}



private void setUndergroundList() {

ListBox linesList = nifty.getScreen(“simulationLinesScreen”).findNiftyControl(“linesList”, ListBox.class);

for (int i=0;i<lineDB.getLinesDB().size();i++) {

linesList.addItem(lineDB.getLineByIndex(i).getName());

}

}



}

[/java]



I’m probably missing something obvious but please bear with me - I’m just starting my work with jMonkey. Any help much appreciated!

Have you registered the app state as a screen controller with nifty? (Either before or at the same time as loading the Xml).

I’ve tried that zarch, but whenever I’m trying to load something (say, with a click of a button) I’m constantly getting null pointer exception - how come? It seems like nifty gui components can’t access anything from jme vars? help me here…?

Have you tried breakpointing the event callback thread and looking to see what its situation is? Could also check that its being called in the same instance of the controller that you registered…



Which variable is triggering the null pointer and why is that variable not being initialized? What is your stack trace?

I got this every time - I seriously don’t know what the problem is. I’m instancing my variables correctly - according to tuts…

[java]

WARNING: failed to invoke method [public void views.StartScreen.onStartNewSimulationClick(java.lang.String,de.lessvoid.nifty.elements.events.NiftyMousePrimaryClickedEvent)] with Exception [null][java.lang.NullPointerException]

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at de.lessvoid.nifty.NiftyEventAnnotationProcessor$Subscriber.onEvent(NiftyEventAnnotationProcessor.java:118)

at org.bushe.swing.event.ThreadSafeEventService.publish(ThreadSafeEventService.java:980)

at org.bushe.swing.event.ThreadSafeEventService.publish(ThreadSafeEventService.java:920)

at de.lessvoid.nifty.Nifty.publishEvent(Nifty.java:193)

at de.lessvoid.nifty.elements.PrimaryClickMouseMethods.publishEvent(PrimaryClickMouseMethods.java:53)

at de.lessvoid.nifty.elements.PrimaryClickMouseMethods.onClick(PrimaryClickMouseMethods.java:26)

at de.lessvoid.nifty.elements.ElementInteractionClickHandler.onClickMouse(ElementInteractionClickHandler.java:105)

at de.lessvoid.nifty.elements.ElementInteractionClickHandler.process(ElementInteractionClickHandler.java:70)

at de.lessvoid.nifty.elements.ElementInteraction.process(ElementInteraction.java:79)

at de.lessvoid.nifty.elements.Element.mouseEvent(Element.java:1405)

at de.lessvoid.nifty.screen.MouseOverHandler.processMouseEvent(MouseOverHandler.java:93)

at de.lessvoid.nifty.screen.Screen.forwardMouseEventToLayers(Screen.java:360)

at de.lessvoid.nifty.screen.Screen.mouseEvent(Screen.java:336)

at de.lessvoid.nifty.Nifty.forwardMouseEventToScreen(Nifty.java:266)

at de.lessvoid.nifty.Nifty.access$1400(Nifty.java:73)

at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processEvent(Nifty.java:1370)

at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processMouseEvent(Nifty.java:1329)

at com.jme3.niftygui.InputSystemJme.onMouseButtonEventQueued(InputSystemJme.java:161)

at com.jme3.niftygui.InputSystemJme.forwardEvents(InputSystemJme.java:236)

at de.lessvoid.nifty.Nifty.update(Nifty.java:248)

at com.jme3.niftygui.InputSystemJme.endInput(InputSystemJme.java:92)

at com.jme3.input.InputManager.processQueue(InputManager.java:787)

at com.jme3.input.InputManager.update(InputManager.java:851)

at com.jme3.app.Application.update(Application.java:598)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:233)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:182)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)

at java.lang.Thread.run(Thread.java:680)

Caused by: java.lang.NullPointerException

at views.StartScreen.onStartNewSimulationClick(StartScreen.java:63)

… 33 more

[/java]

Perhaps I should hit it from different angle - let me ask you this - what is the common procedure of switching between nifty and game state? Let’s say I click a button and it will switch to a background with a blue cube (like the first tutorial cube)?

Ahhhh I’m so stupid! I forgot about attaching my main appState to stateManager beforehand!