I recently started a personal project aimed to randomly generate a 3D world. I also started this project to get more comfortable with Java.
I will try to explain my issue quickly:
By now I only have three classes: World, which will contain the terrain generation algorithm, MeshedWorld which is aimed to draw quads, and Main, from which a test my code.
The issue I have occurs when I try to call the function createMesh (in MeshedWorld) from Main. I have two errors in MeshedWorld:
At line 88 (where I initialize the material): assetManager cannot be resolved to a variable
At line 94 (where a attach a geometry to the rootNode): rootNode cannot be resolved
These two errors only appear when the class MeshedWorld extends SimpleApplication or when the method createMesh is declared static (and in my code I need these two conditions).
I would like to clarify the fact that this piece of code works perfectly when I execute this class as the main class with the method SimpleInitApp in it and without declaring createMesh as a static method.
I think I said everything I need to say. I would really appreciate any help from you guys
Once more, I insist on the fact that I am leaning Java (the first OOP language I learn) and thus I may try doing things that are not correct (for instance creating quads in a static method may be a mistake, I don’t really know …).
And also, I am sorry for my poor english, I’m not English speaking native (I’m French).
Hm. When you had your meshedworld method that you’re calling (say it’s called public MeshWorld) did you include the assetmanager and rootnode as parameters? The code should look something like this.
public MeshedWorld(AssetManager assetManager, Node rootNode) {
…
}
I hope that helps answer your question. Simply extending your MeshedWorld class doesn’t automatically give it access to the assetmanager and rootnode.
assetManager and rootNode are field of SimpleApplication in scope “protected” so you can access them in class that inherit from SimpleApplication. Your MeshedWorld doesn’t know assetManager, nor rootNode.
Simple solution : pass them as argument to your createMesh(…) method, like orientation and color.
public static void createMesh(int x, int y, int z, Orientation orientation, ColorRGBA color, AssetManager assetManager, Node anchor) {
I prefer to name anchor or parent than rootNode the node on witch I attach other Spatial (it’s not mandatory to be the rootNode of the scene tree).
I don’t know why this solution did not came to my mind … I spent a lot of time trying to import libraries and doing weird workaround. But once you say it it seem so bright !
Some hints: Java is not a scripting language and the purpose of putting code into a class is not to separate code you’d otherwise have put into one class for the sake of keeping the single files small. If you just put it in another class because “oh, my SimpleApplication class has already so much code in it” then just keep that code in SimpleApplication for now until you learn how objects work, when you’d make static methods in utility classes and how the jME base classes like AppStates and Controls help you keep your application organized. Generally I’d suggest learning java first and then trying to write a game because both topics are hard enough for themselves.