Problem solved except for assetmanager

i need to use asset manager in other classes as well but when i try to do that it gives me error



Public Class mesh(){

AssetManager ASM;

public Geometry Load(Geometry mgem){

Material mesh_mat;

Box cube = new Box(Vector3f.ZERO, 1,1,1);

mgem = new Geometry(“box”, cube);

=> Material mat = new Material(ASM, “Common/MatDefs/Misc/Unshaded.j3md”); <= Problem here

mgem.setMaterial(mat);

etc…



as a sample for what i wanted to create



note : this error not only with material if i tried to load mesh same problem but i can normally do load them in the main class so the problem not the models or directory sure.

[java]AssetManager ASM;[/java] you never give this variable a reference to the AssetManager



http://www.javabeginner.com

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/outsourcing-code-from-simpleapplication

…and when posting code snippets please click the little “java” button up there between “blockquote” and “xml”. It will keep our eyes from bleeding more than necessary. :wink:

thanks for the help wezrule im not dumm in java except to know how to use jmonky engine lib i still don’t have it all but im still reading and studying :slight_smile:

pspeed i will, i was searching to know how to do this code section know i do thanks

You may not be dumb in Java but you have to understand that instantiating an object and hoping it to be automagically referenced is just erratic, at best. All the objects you use in the MainClass are actually also instantiated and configured. If there is an object that is “already there” you can always right-click to follow it into the JME code and see where and how it was configured. There is no magic involved :wink:

@codeguard said:
thanks for the help wezrule im not dumm in java except to know how to use jmonky engine lib i still don't have it all but im still reading and studying :)


Saying this does not earn you any points. It is very clear from your posted code snippets in this and other posts that you are very new to Java or at the very least have been stumbling along for a while without learning good habits. It's ok to be new at something... everyone started out some time. But it's not ok to posture as if you are more of an expert than you are... that will just make people not want to help you.

This post is a prime example of where the issue was a simple non-JME related Java problem easily determined through the most basic debugging. That's ok. Pretending it was something else is not.

i m not saying I’m expert in java i said i started reading and studying :slight_smile: i used Unity game engine a lot with c# not a differ from Java but i was wondering if there is a good free open source game engine and searching till found jmonkey that it. so i know java but not all the fundamentals maybe like u said but im not trying to look in my post like an expert only asking question i don’t know and get help and say thanks not trying to impress someone.





but actually there is something since u mentioned it some forums without names ask a questions like from 2 or 3 years ago and they dont answer it after a while someone answered it and said its dumm question, if no helps telling me whats is wrong or where to start reading then its no use to do anything with the forum but maybe i tried it some how to make you feel it a good question lol :slight_smile: but actually this forum very helpful with all also the documentation and tutorials.

but thanks for help really helped.

ok here is my idea i need to create a class to load mesh i want to use i made it use specific models first then i will add it by name get error of loading it don’t know the error last time i know its from mesh class this time from the line.



[java]public class Mesh{



private Main myApp = Main.getApp();



private AssetManager assetManager = myApp.getAssetManager();



private Node RootNode = myApp.getRootNode();



public void Load(){

Spatial mesh;

mesh = assetManager.loadModel(“Models/Jack.mesh.xml”);

mesh.setLocalScale(0.5f);

Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mesh.setMaterial(mat);

RootNode.attachChild(mesh);

};

}[/java]



[java]public class Main extends SimpleApplication

{

private static Main thisApp;

public static void main(String[] args) {

Main app = new Main();

app.start();

}

public static Main getApp() {

return thisApp;

}



public Mesh mesh;

@Override

public void simpleInitApp() {

mesh.Load();

};

}[/java]



i don’t the error it says at simpleinitapp and as u see there is only one line.

i need to get this rootnode inside i tried first to create a node inside the class and attach it out in main but not working.

Ehh man, seriously, there is a reason for having “.” and “,” on your keyboard.





I believe that the Main class and the AssetManager are static objects (too lazy to check it now). So trying to get the rootNode, which is not static, will fail in such a construction as yours. Why don’t you just write a constructor that looks like this?



[java]



public class Mesh{



private Main myApp;

private AssetManager assetManager;

private Node RootNode;



public Mesh(Main m)

{

myApp = m;

assetManager = myApp.getAssetManager();

RootNode = myApp.getRootNode();

}



public void Load(){

Spatial mesh;

mesh = assetManager.loadModel(“Models/Jack.mesh.xml”);

mesh.setLocalScale(0.5f);

Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mesh.setMaterial(mat);

RootNode.attachChild(mesh);

};

}

[/java]





And then on your main class you have your init like this:



[java]

public void simpleInitApp() {

mesh = new Mesh(this);

mesh.Load();

};

[/java]



The way you did this is actually not very good and should be considered evil if you don’t know what you are doing exactly! Please, read up about Java and OO in general. I believe that people will get annoyed by such questions because this is really basic knowledge of static objects vs dynamic objects. And in the end this will result in people refusing to help you at all :wink:

thanks for ur help i already started reading in www.javabeginner.com search inside :slight_smile:

this better start there right after u said it looks to me like java is an ocean and im on shore :smiley: lol maybe i start read more

do u suggest anything from the code as u see.



but for the record few things i don’t get it in the engine lib not blaming anyone just i will read more tutorials cause this things i do the most is wrong because i don’t know the value of this objects.

it works thanks :slight_smile: after i sow ur code its not jmonky thing its java srry i would put a gun in head in this moment my bad i tried initialize mesh but didn’t get a way to do it but really thanks saved the day.

c# is object oriented so you should be able to handle OO concepts like passing references around etc.



SimpleApplication has references to the asset manager etc inside it. They are protected so your class extending SimpleApplication can access them directly. Other classes you will need to deliberately expose them too though by either passing in a reference to them or by putting query methods where the other classes can see them.

about know what i do i guess u ask for trying to attach things inside other class part or what?

im trying to create separate classes to create my game only attach the needed class to use with specific parameters.

so i could easily use it with Model, animation, audio, post processing and AI by solving this one it will be easily to create it with the others



really thanks :slight_smile:

@zarch i know but in unity ur script is easily handled with scene editor like end user but u cant dig in if you don’t own the pro license so OO not my problem my problem is getting insane with what i want to do and not working :smiley: thanks for the tip

@codeguard said:
ok here is my idea i need to create a class to load mesh i want to use i made it use specific models first then i will add it by name get error of loading it don't know the error last time i know its from mesh class this time from the line.

[java]public class Mesh{

private Main myApp = Main.getApp();

private AssetManager assetManager = myApp.getAssetManager();

private Node RootNode = myApp.getRootNode();

public void Load(){
Spatial mesh;
mesh = assetManager.loadModel(&quot;Models/Jack.mesh.xml&quot;);
mesh.setLocalScale(0.5f);
Material mat = new Material(assetManager, &quot;Common/MatDefs/Misc/Unshaded.j3md&quot;);
mesh.setMaterial(mat);
RootNode.attachChild(mesh);
};
}[/java]

[java]public class Main extends SimpleApplication
{
private static Main thisApp;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
public static Main getApp() {
return thisApp;
}

public Mesh mesh;
@Override
public void simpleInitApp() {
mesh.Load();
};
}[/java]

i don't the error it says at simpleinitapp and as u see there is only one line.


thisApp is never set. This would have been a problem in C#, too.

ok i already admit it im wrong @pspeed thanks for noticing :slight_smile:

do u suggest a specific book to read i need to get my hands dirty with more java :slight_smile: could u please tell me a good book.

There is no need for books in my opinion. The internet is abundant in good tutorials. Read through a bunch of them and above all the other things: write code yourself as much as possible. No better way to learn any programming language!

1 Like