Outsourcing code from SimpleApplication

Hi,



i’m currently developing a little game using jme3. To manage 3D contents I’m using a class that extends SimpleApplication. By now this class got blown up pretty much so i thought i’d outsource some contents in different classes, e.g. all 3D-models in one class, the keystroke-events in another and so on…



The problem is that i can’t get the required objects out of the SimpleApplication. Whenever I call the AssetManager, the InputManager, or any other object from another class than the one that extends SimpleApplication, i get a Nullpointer Exception.



[java]Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at c4.gui.game.implementation.UserInput.initKeys(UserInput.java:102) // here i try to access the inputmanager from outside

at c4.gui.game.implementation.UserInput.initializeInput(UserInput.java:47)

at c4.gui.GameGUI.simpleInitApp(GameGUI.java:69)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:230)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)

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

at java.lang.Thread.run(Thread.java:722)[/java]



The class “UserInput” was created by the class “GameGUI”, which extends the SimpleApplication. UserInput gets a reference to the InputManager in the constructor. But somehow I can’t access it…



hope you can help, i hate it when a class goes over 1000 loc :wink:

well those objects are protected instance variables of SimpleApplication/Application, and they all have getter methods. I find it easiest just to have a static variable reference of the SimpleApplication available anywhere.

[java]class MyApp extends SimpleApplication {

private static MyApp thisApp;



public void simpleInitApp() {

thisApp = this;

}

public static MyApp getApp() {

return thisApp;

}

}

[/java]

then you can access it like this for example:

[java]class anotherClass {

private MyApp myApp = MyApp.getApp();

private AssetManager assetManager = myApp.getAssetManager();

}[/java]

ah, of course, i’ve already created the subclasses in the constructor of the main class, where the instance variables of SimpleApplication could not have been initialized.

When I initialize them in the simpleInitApp() it all works fine. Then I don’t even need the static variable.



Thanks :slight_smile:

There are several “managers” in jME3 that you might end up using. I made a simple interface that is implemented in my main game class.



That way it’s much easier to reference those manager classes throughout your game. Just reference those in the constructor of the class that needs a certain manager.



Short list of managers that should be included if you need them:



assetManager;

stateManager;

audioRenderer;

listener;

renderManager;

inputManager;



[java]

static AssetManager gameAssetManager;



// in game’s main class during simpleInitApp()

gameAssetManager = assetManager;



// Overrides/Implements the Interface’s method.

@Override

public AssetManager getAppAssetManager() {

return gameAssetManager;

}

[/java]



Variable names should be properly done, this is just an example. :slight_smile:

And if you get to the point where you architect your game around AppStates then you shouldn’t need these static references so much because you will already have a reference to Application and can get them when needed.



I started out with a static MainApp style reference in my game, too but I don’t think it’s even used anymore.

wezrule why do I get this error trying your example??



[java]class MyApp extends SimpleApplication {

private static MyApp thisApp;

public void simpleInit() {

thisApp = this;

}

public MyApp getApp() {

return MyApp; <<<<<<<<<<<<<<<<<<<< MyApp cannnot be resolved

}

}[/java]

should be [java]return thisApp;[/java] it was just a quick draw up, but i updated the post, thx, and to be pedantic it should also be

[java]simpleInitApp()[/java]

There is a whole section in the wiki about this…

garnaout said:
wezrule why do I get this error trying your example??

[java]class MyApp extends SimpleApplication {
private static MyApp thisApp;
public void simpleInit() {
thisApp = this;
}
public MyApp getApp() {
return MyApp; &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; MyApp cannnot be resolved
}
}[/java]


Because you changed his example. Look again. In the example that line wasn't return MyApp;

If you call anything on your Application before simpleInit has run then nothing will have been initialized.

http://www.javabeginner.com ? :smiley:

normen maybe you can point us to that wiki?



pspeed the code was edited by wezrule.



Wezrule even with the changes when I do make the call, assetManager still doesn’t get initialized and I get a null ptr exception. Are you sure the code is correct?



[java]private Jme3Adapter myApp = Jme3Adapter.getApp();

private AssetManager assetManager = myApp.getAssetManager();[/java]



myApp is always null.



obviously myApp = this in SimpleInit() is not being accessed first… but why is it not being called first?

yes the code is fine, did you put [java]thisApp = this;[/java] inside simpleInitApp();? use the debugger to see if the code is getting called

myApp.function(arg1, arg2)?

oh my god… I deserve to be executed… sorry yes yes… finally understand it…

www.jguru.com xD