Problem extending SimpleApplication

I’m trying to implement https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:intermediate:best_practices . I’m trying “Extend SimpleApplication”.



Given the folowing :



[java]package myapp;



import com.jme3.app.SimpleApplication;

import com.jme3.system.JmeContext;

import myapp.server.MyServer;



public class BaseHeadlessServer extends SimpleApplication {



private MyServer myServer;



public static void main(String[] args) {

BaseHeadlessServer app = new BaseHeadlessServer();

app.start();

}



@Override

public void simpleInitApp() {



System.out.println(“BaseHeadlessServer simpleInitApp”);



}



@Override

public void simpleUpdate(float tpf) {

//System.out.println(“BaseHeadlessServer simpleUpdate”);

}



}

[/java]



and then



[java]package mygame.MyZombieGameServer;



import myapp.BaseHeadlessServer;



public class MyZombie extends BaseHeadlessServer {



@Override

public void simpleInitApp() {

super.simpleInitApp();

System.out.println(“zombie simpleInitApp”);

}



@Override

public void simpleUpdate(float tpf) {

super.simpleUpdate(tpf);

System.out.println(“zombie simpleInitApp”);

}



}[/java]



My problem is that it doesnt seem to matter, if i comment out or not, these 2 lines:

super.simpleInitApp();

System.out.println(“zombie simpleInitApp”);



the only simpleInitApp() that is executed is the one in my BaseHeadlessServer class.

Shouldn’t the simpleInitApp from class MyZombie also be executed and thus => System.out.println(“zombie simpleInitApp”); ?

You do not want your Zombie class to be an application class anyway. No reason to extend your application class. I guess you want to do that so you have access to the variables in the application. This approach is wrong and shows that you should probably look into sites like http://www.javabeginner.com before continuing working on coding games :wink:

Maybe i should have used

public class MyZombieServer extends BaseHeadlessServer {



instead of

public class MyZombie extends BaseHeadlessServer {



to avoid people thinking MyZombie class is some kind of entity.

It was just some code to test the principle explained on the site in ‘best practices’ under (Extend SimpleApplication). The fact of having a base application in a jar file you can put on the class path and extend your application with.

But i do agree that i’m an absolute beginner.

Your main() is instantiating the base class… not the subclass. So there is no MyZombie instance.



a) you may want to pick up some Java skills before attempting to write a game in java:

http://www.javabeginner.com/



b) there is no reason that a server needs to extend SimpleApplication if it is not doing any OpenGL UI stuff. But given your Java skill level it may be the easiest way, I guess.

I agree about picking up some java skills.



Its just that i was confused by this piece of text:



Add MyBaseGame.jar to the classpath of MyZombieGame. You can now re-use you custom generic game library.

Make MyZombieGame.java extend your MyBaseGame class (which extends SimpleApplication).

MyZombieGame.java has no main() method because it uses the inherited main() method.

MyZombieGame.java makes super calls and overrides the SimpleApplication methods it inherited:



I’m sorry to insist, but isn’t that an error:



“MyZombieGame.java has no main() method because it uses the inherited main() method.”

This comes from https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:intermediate:best_practices

Yeah, this:

MyZombieGame.java has no main() method because it uses the inherited main() method.



Is completely false.



Not sure who wrote that but a) static methods are not inherited, and b) there is no way that the base class could know which of any of an infinite number of subclasses to instantiate.



A fruit could not possibly know how to instantiate an apple, a pear, an orange, a mango, a pineapple, etc… Looks like the “best practices” might need a once over.

1 Like
@pspeed said:
static methods are not inherited


Note: while they will _act_ that way sometimes... you should never ever ever ever use them this way. As a general rule, always call a static method on the class that it was defined on.
1 Like