Working Outside of the Main class

So I’m pretty much trying to work outside of my Main class and only keep important method calls within it.

I created a basic class named Floor, which holds my Geometry & materials for my Floor object and such. I can’t seem to attach any of my materials that I assign in this class to the rootNode of my Main class. I created a basic class file, created a few class fields, created a basic method, and tried calling the method from my Main class file and I get an error when trying to initialize the assetManager. Here’s the code:

[java]
import com.jme3.asset.AssetManager;

public class Floor {

 Main main;
 AssetManager assetManager;

 public void initializeAssetManager(AssetManager assetManager) {
      this.assetManager = main.getAssetManager(); // Assign the newly instantiated assetManager object to the Main class files Asset Manager
      System.out.println("Asset Manager in class: " + this.toString() + " successfully initalized!");
 }

}
[/java]

and within my Main class, I have:

[java]
public class Main extends SimpleApplication {

Floor floor;

public static void main(String[] args) {
    Main app = new Main();
    app.start();
}

@Override
public void simpleInitApp() {
    createLight();
    floor.initializeAssetManager(getAssetManager()); // Error here with passing the current Asset Manager to the created Asset Manager within the Floor class file
}

}
[/java]

You pass the assetManager from main but then use the “main” variable in Floor, which is null, to get the assetManager. Just do “this.assetManager = assetManager;” in line 9. If you’re new to java I suggest going through some java basics before trying to create a jME game.

<cite>@normen said:</cite> You pass the assetManager from main but then use the "main" variable in Floor, which is null, to get the assetManager. Just do "this.assetManager = assetManager;" in line 9. If you're new to java I suggest going through some java basics before trying to create a jME game.

That will not work, when I change “main.getAssetManager()” to “this.getAssetManager()” I get an error telling me that “getAssetManager()” cannot be found. So I created getter & setter methods for my setAssetManager method, and called the getter method for it.

It is indeed telling me that the assetManager is null, but how would I initialize this variable and call it correctly? Using a constructor?

you didn’t do exactly as normen said.

<cite>@wezrule said:</cite> you didn't do exactly as normen said.

Realized that after I posted :stuck_out_tongue: I edited the code to what he told me and it still isn’t working properly. Also, I’m trying to use the AbstractControl class to extend on my Player class, I’ve used the methods exactly as they’re shown within the documentation and yet nothing is happening. I’m not quite sure why.

It’s as if Overrding the method does nothing…

I might strongly! suggest to first do a few tutorials for a few days with basic java, as it will speed things up, and jme alone is already a lot to learn without mixing it with java learning.

Learning to shave might not be that hard. Learning to ride a bike might be a bit more difficult. Learning to ride a bike with no hands is kind of advanced. Trying to learn to shave while riding a bike with no hands is nearly impossible.

In short: I agree with Empire. It is best to learn Java first before tackling Java game programming.

That isn’t my issue, pspeed… It doesn’t matter which class I extend, implement, whatever… None of the methods I Override are working. If I’m extending an AbstractControl and overrding the setSpatial(Spatial spatial) method just like it asks, and adding logic inside the method to make sure it’s actually being called (which it isn’t), then I don’t understand what the issue is. I’m pretty sure Abstract classes cannot be instantiated. So what’s the deal?

Why can’t I simply do,

[java]
class MySpecialPlayer extends AbstractControl {

 @Override
 public void setSpatial(Spatial spatial) {
      super.setSpatial(spatial);
      this.setEnabled(true);

      System.out.println("Method being called!");
 }

}
[/java]

For obvious reasons?

well that snippet won’t work because you haven’t overridden the other methods which actually require overriding

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/control/AbstractControl.html#controlUpdate(float)

<cite>@wezrule said:</cite> well that snippet won't work because you haven't overridden the other methods which actually require overriding

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/control/AbstractControl.html#controlUpdate(float)

I know, I’m on my cell phone so for times sake, I didn’t do all of the overriden methods. I was just showing what I’m trying to do specifically.

then it will work. Methods of java classes are all virtual, and hence Polymorphic. So if you create an instance of MySpecialPlayer, and pass it to method which expects an AbstractControl or Control, then your setSpatial will be called.

@xtgscott said: That isn't my issue, pspeed.. It doesn't matter which class I extend, implement, whatever.. None of the methods I Override are working. If I'm extending an AbstractControl and overrding the setSpatial(Spatial spatial) method just like it asks, and adding logic inside the method to make sure it's actually being called (which it isn't), then I don't understand what the issue is. I'm pretty sure Abstract classes cannot be instantiated. So what's the deal?

It has to be added to a spatial that is in turn attached to the rootNode of the scene to be used/called.

You somehow seem to expect the class to be just called after you wrote and compiled it, right? Like it would maybe be in a script language like PHP or Javascript? Thats the reason the advice about java is repeating here. You seem to have a few misconceptions that are at a more basic level like the difference of objects vs. classes and variable access.

<cite>@normen said:</cite> It has to be added to a spatial that is in turn attached to the rootNode of the scene to be used/called.

You somehow seem to expect the class to be just called after you wrote and compiled it, right? Like it would maybe be in a script language like PHP or Javascript? Thats the reason the advice about java is repeating here. You seem to have a few misconceptions that are at a more basic level like the difference of objects vs. classes and variable access.

Thank you, Normen, that cleared it up a bit. But how would I extend my rootNode to my Floor class? Could I create a new rootNode object and attach it that way possibly? Yes, I assumed that it would run when the application is started because the method was overriden. My apologies. I

Best start here with the tutorial, it explains how you add new Nodes and Spatials to the rootNode and much more.
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3

<cite>@normen said:</cite> Best start here with the tutorial, it explains how you add new Nodes and Spatials to the rootNode and much more. https://wiki.jmonkeyengine.org/legacy/doku.php/jme3

Thanks Normen, I will get to reading.

@xtgscott said: That isn't my issue, pspeed.. It doesn't matter which class I extend, implement, whatever.. None of the methods I Override are working. If I'm extending an AbstractControl and overrding the setSpatial(Spatial spatial) method just like it asks, and adding logic inside the method to make sure it's actually being called (which it isn't), then I don't understand what the issue is. I'm pretty sure Abstract classes cannot be instantiated. So what's the deal?

I’m just trying to offer advice. It’s good advice and we can all clearly see that your Java skills are basic. That’s not an insult. We all had to start somewhere.