My question would be: what is ghe proper thing to do with the materials after all the initial loading has completed, but there is a possibility of more objects being loaded? Should i keep a resource class that has a dictionary of all used materials so far? Let me explain some more. In my game I am not going to use the jmonkey scenes, just have text files that store information of the map. It will be loading in the background sort of like the terrain grid of core. Each object has a mmaterial, but from what ive read, it is best to reuse materials. So if there is one object that uses a material and another one gets loaded later that uses the same texture or material, that new object should reuse the material or texture.
If you create a Material variable and need to use it later, the garbage collector will notice and wont erase it.
This question has little to do with materials and more to do with variable memory allocation and permanence.
The material you create is merely an object instance from the class Material. Like any other object in java, if you reference that material instance at a later time you don’t need to worry about the variable being collected by the garbage collector.
Thanks, but I do have a separate system set up that loads all my game data from text files (except the models, i use j3o for that). It also has basic scripting so that when a plater gets to a certain point in the map, a new section will be loaded in in another thread and another part of the map can be unloaded. So there is no way the garbage collector can know if it will be used, it is not directly hard coded.
There are only two ways to go with this in my opinion:
You keep the variable myMat for example and then reuse it by passing it through the constructor of the new classes that need it. Disadvantage: You will hug resources with the myMat variable.
you create “new Material()” inside each constructor of the classes you will need in the future. Disadvantage: you will create a brand new material that if you are loading from a material file will take time to load every time.
I don’t get the complication. Keep a reference to it if you need it, get rid of the references and let the GC clean it up if you don’t?
It sounds like a pretty standard scenario to me.