Loading spatials from an outside class

I am working on a 3d fps, and before I end up like last time with one class and 1500+ lines of code I would like to learn how to instantiate outer classes that load spatials, create animations etc. into the main class in which the game is run. I have just started by trying to add the level to the scene from another outer class, but i keep getting a null pointer exception.

package ClassFiles;



import com.jme3.light.AmbientLight;

import com.jme3.math.ColorRGBA;





public class MainThread extends Maps{



public static void main(String[] args) {

MainThread thread = new MainThread();

thread.start();

}



@Override

public void simpleInitApp() {

Maps object = new Maps();

object.loadMaps();

AmbientLight al = new AmbientLight();

al.setColor(ColorRGBA.Blue);

rootNode.addLight(al);

}

}


package ClassFiles;

import com.jme3.app.SimpleApplication;
import com.jme3.scene.Spatial;

public class Maps extends SimpleApplication {

Spatial map;

public Maps() {
}

public void loadMaps() {
map = assetManager.loadModel("dgame/maps/main.scene");
map.setLocalScale(2f);
rootNode.attachChild(map);
}

@Override
public void simpleInitApp() {
}
}

Any help would be great! :)

yeah, we explained a lot of that already in the wiki:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:intermediate:best_practices#controls_and_appstates_--_the_smart_way_to_implement_game_logic

1 Like

you can have a look at other people’s source code for guidance. Sometimes people post it in these forums: http://hub.jmonkeyengine.org/groups/user-code-projects/forum/ and http://hub.jmonkeyengine.org/groups/free-announcements/forum/ . There is also a JME project created by the very own JME developers. It is called “MonkeyZone” and full source is available, the wiki is here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:monkey_zone .

i found your problem :

Do this :

  1. MainThread extends SimpleApplication
  2. remove inheritance from Maps. IT NOW WORKS !!!
  3. Advice : Dont use inheritance. All code can be written with delegate design pattern instead of inheritance. E.g “Has a variable” instead of “isA variable”. Use inheritance only once every 1000 years when the stars align and subclass should inherit 100% of all methods/variables/behavior.
  4. Why does a subclass initialize a superclass ?

@ tralala

i understand why you want me to extends SimplaApplication, but removing the ‘Maps object = new Maps();’ did not make it work.

Ops you are right, you also need to make main class “singleton” : I renamed that class to scene.



[java]

import com.jme3.app.SimpleApplication;

import com.jme3.light.AmbientLight;

import com.jme3.math.ColorRGBA;



public class Scene extends SimpleApplication

{

private static Scene scene ;



public static void main(String[] args)

{

Scene scene = Scene.get();

scene.start();

}



private Scene()

{

}



public static Scene get()

{

if (scene == null) scene=new Scene();

return scene;

}



@Override

public void simpleInitApp()

{

Maps object = new Maps();

object.loadMaps();

AmbientLight al = new AmbientLight();

al.setColor(ColorRGBA.Blue);

rootNode.addLight(al);

}

}[/java]



[java]

import com.jme3.app.SimpleApplication;

import com.jme3.scene.Spatial;



public class Maps {



Spatial map;



public Maps() {

}



public void loadMaps() {

Scene scene = Scene.get();

map = scene.getAssetManager().loadModel(“dgame/maps/main.scene”);

map.setLocalScale(2f);

scene.getRootNode().attachChild(map);

}



public void simpleInitApp() {

}

}[/java]

1 Like

Thanks soooo much! it worked perfectly. It was written very well too, I understand every line of it. Thanks again man. :slight_smile:

Looking back at this topic embarrasses me lol. I was such a n00b way in over my head :stuck_out_tongue: By the way I have a much stronger grasp on OOP :wink: