Using assetManager in another class than SimpleApplication

Hello, i am new to JME and also new to JAVA!



And i have a little problem : like it is said in the title, i am trying to use a material in a custom class, using the assetManager

[java]

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

[/java]

But i get “cannot find symbol” about assetManager. I know i have something to import, and i tried to add “import com.jme3.asset.*;” but that’s not enough. I thought that it would be like inside the SimpleApplication class, but it’s not.



Can you explain to me why it doesn’t work(so i can improve my knowledge on both java and JME) and what i have to do to fix it?



Thanks!

2 Likes

You have to add Assetlocators to the assetManager so it can find your stuff

lol Empire you want to make him cry? xD

To answer the question: You don’t have an assetManager field in your class and it does not reference the valid AssetManager object from the SimpleApplication. You can pass a reference e.g. via the constructor of your class.

Don’t ask me for code, I used all the right terms and you should probably try and find out yourself what this means if you don’t understand.

I saw that 3 or 4 months ago, you told someone who had the same problem to add this to the constructor of his class :



[java]

public MyClass(AssetManager manager){

this.assetManager=manager;

}

[/java]



Problem is i am not sure what does that exactly means. I know what is a constructor , and i did what you suggest, but i am still stuck. Why adding this to the constructor, isn’t a constructor used to initialize the properties of an object when it is instantiated from a class ? I don’t see the logic, and that’s why i am not able to fix it!

4 Likes

Exactly, how do you expect to be able to write games?

1 Like

By trial and error. I have read enough theoretical tutorials and things like that about java, and now i want to learn by trying myself with a small and fun project. That’s my first object oriented programming language. Sorry to be a newbie.

I am sorry I don’t know how to dumb this down more.

1 Like

You are a bad teacher then :slight_smile:

If only it was in the tutorials, but there’s nothing about that, even in the “asset tutorial”. And that’s how i learn, i look at others code and try to understand how it works, and from that i can build my own thing starting from scratch. But here, you expect me to understand how you have built JME, and i can’t do that just right now.

You should learn the Java programming language before learning jMonkey.

There are many different ways to pass an Object (here the assetManager) to another Object.



In my project I store all the important instances in a Global static class.



[java]public static AssetManager assetManager;[/java]

your better off learning all the programming constructs of java first, then it would be obvious to you. The tutorials state that assetManager is an instance variable which is available if you extend SimpleApplication. Your simply passing the reference of the assetManager instance variable to the constructor of another class, its pretty basic stuff. Then that class can now use the assetManager reference. There are many other ways to achieve this. You could also pass a SimpleApplication reference to the class constructor, or create a static function inside your extended SimpleApplication class which returns a “this” reference, and then call getAssetManager(), and many others, depending on what you wanna do

I really suggest to start learnign java first and make some simple console games. (Like asci pong or similar) to get a better feeling and knowledge first). Otherwise you will probably become to frustrated before archiving anything at all.

1 Like

Thanks for all the advice.



(i received a message notification because someone posted on

http://test.hub.jmonkeyengine.org/groups/import-assets/forum/topic/using-assetmanager-in-another-class-than-simpleapplication/

Notice the “test”. Is that some kind of bug in the matrix?)

@lamadorsa said: I saw that 3 or 4 months ago, you told someone who had the same problem to add this to the constructor of his class :<br /> <br /> [java]<br /> public MyClass(AssetManager manager){<br /> this.assetManager=manager;<br /> }<br /> [/java]<br /> <br /> Problem is i am not sure what does that exactly means. I know what is a constructor , and i did what you suggest, but i am still stuck. Why adding this to the constructor, isn't a constructor used to initialize the properties of an object when it is instantiated from a class ? I don't see the logic, and that's why i am not able to fix it!

This is a dependency injection technique called “constructor injection”. See this article: What is constructor injection.

@devinbost said: This is a dependency injection technique called "constructor injection". See this article: What is constructor injection.

This thread is almost exactly three years old, though.

I know this post is very old, though.

But the main point I can’t see.
Where is this assetManager stored? I mean in which class?
Don’t you have to import something, when using this Manager?
Or set a reference, like you said - but reference to what Object?

The SimpleApplication class has a field called “assetManager” which when the class is instantiated and initialized holds a reference to the created AssetManager object.

@iova said: I know this post is very old, though.

But the main point I can’t see.
Where is this assetManager stored? I mean in which class?
Don’t you have to import something, when using this Manager?
Or set a reference, like you said - but reference to what Object?

A larger question is… how are you looking? Have you tried any of the asset related tutorials? Have you looked at any of the javadocs?

If you learn to use these resources then you can get answers much faster.

I found it! I found it! Finally a good example how to import this dammed AssetManager:

Got a solution for the import:

create a Object in the class with:
private AssetManager assetManager;

and use in header

import com.jme3.asset.AssetManager;

But now I get a null-pointer Exception. I think it’s no good Idea to use a different assetManager from Simple Application.
Is there a way to use the assetManager from SimpleApplication?

1 Like

Yes, pass it as a parameter to your other class. Without some basic Java skills you will struggle like this for every simple thing. Strongly encourage you to do a few Java tutorials to get a good feel for the language.