Multiple Classes, with only 1-3 extending SimpleApplication

Hi, I am wondering how you would make your project so that only one class extends SimpleApplication. There would be different classes loading assets, and I found out that the assetManager comes from extending SimpleApplication, but I don’t want every class to have a simpleInitApp() feature. So, I was wondering how you would do this.

via constructors. Are you a beginner java programmer?

[java]
public class myClass
{
public myClass(AssetManager assetManager)
{

}

}

[/java]

Yes, I am a beginning java programmer, but I am halfway through a Java All-in-one book for dummies. I thought that the assetManager was only available through extending SimpleApplication, and I got confused a bit, until I found out that it’s part of the com.jme3.assets.AssetManager class. Anyways, thanks for the help.

No worries - we all start somewhere. Feel free to ask away if you get stuck, and good luck!

1 Like

I know that you use the nodes to attach render the assets (I think), but where would I find the class for this? (Other than extended from SimpleApplication)

you attach them to the rootNode. You should only extend SimpleApplication once, else you will have multiple scenegraphs. the rootnode can be found in the main class via .getRootNode();

I have that down, and now that I figured that out, I understand jme3 a lot better. Just one more problem: I called the assetmanager perfectly, with AssetManager asset = game.getAssetManager();, with game being the main class object. But I can’t find the first part for the rootNode, so it would be something like <RootNode> root = game.getRootNode();. Or, do you just do this: game.getrootNode.attachChild(bunny);?

nvm, I figured that part out. :slight_smile:

I have a new problem, which is that I am getting a NullPointerException when I run it. Heres the code in the model loading class:
[java] public Init_Map() {

    Game game = new Game();
    AssetManager asset = game.getAssetManager();
    Spatial bunny = asset.loadModel("Models/bunny/bunny.j3o");
    game.getRootNode().attachChild(bunny);
} [/java]

I call it with a basic method: Init_Map i_map = new Init_Map();
(The location of the .j3o file is right, it’s in /assets/Models/bunny/bunny.j3o)

@HyphaDuke said: I have a new problem, which is that I am getting a NullPointerException when I run it. Heres the code in the model loading class: [java] public Init_Map() {
    Game game = new Game();
    AssetManager asset = game.getAssetManager();
    Spatial bunny = asset.loadModel("Models/bunny/bunny.j3o");
    game.getRootNode().attachChild(bunny);
} [/java]

I call it with a basic method: Init_Map i_map = new Init_Map();
(The location of the .j3o file is right, it’s in /assets/Models/bunny/bunny.j3o)

Well, there is no way that Game could possibly have been initialized properly and have an asset manager.

How many games do you have in your game? Maybe you really meant to pass it to this class.

…here is the part where I suggest learning Java (already hard) and more Object Oriented concepts (also really hard) before trying to write a 3D game (on the scale of things one of the single hardest things you can do in programming).

I have a main class called Game that extends SimpleApplication, and since I only need one that does that, I’m grabbing the main AssetManager and RootNode from that class. As I said earlier, I’m reading through a Java for dummies book already. If you want, here’s the main class code:
[java]public class Game extends SimpleApplication {

public static void main(String[] args) {
    
    Game game = new Game();
    game.start();
    
}
public void simpleInitApp() {
Init_Map i_map = new Init_Map();
}

}[/java]

Do it like this:

[java]
public class Game extends SimpleApplication {

private static Game game; 

public static void main(String[] args) {

    game = new Game();
    game.start();

}
public void simpleInitApp() {
Init_Map i_map = new Init_Map();
}

}
[/java]

Then use that private instance of Game to pass around or obtain objects such as the rootNode or assetManager. For example:

[java]
public Init_Map() {

    // Game game = new Game();
    // AssetManager asset = game.getAssetManager();
    Spatial bunny = game.getAssetManager().loadModel(“Models/bunny/bunny.j3o”);
    game.getRootNode().attachChild(bunny);
} 

[/java]

It still gives me a NullPointerException. I dropped the AssetManager asset = game.getAssetManager(); line, since I didn’t need that anymore. If it helps, heres the stack trace (I still needed the Game game = new Game(); part in the Init_Map class.):

at game.init.Init_Map.<init>(Init_Map.java:11) // Spatial bunny = game.getAssetManager().loadModel(“Models/bunny/bunny.j3o”);
at game.Game.simpleInitApp(Game.java:17) // Init_Map i_map = new Init_Map();
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:722)

You should probably learn java before you attempt to learn how to code a game. You need to pass the Game object to the Init_Map class via the constructor.

Thanks, I got it.