Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conserving RAM in java by using references to objects

I know some about java, and a lot about the lower level functions of a computer, so I’m always looking for ways to conserve ram. I’m also a fan of dwarf fortress, so I’ve been trying to get some similar system working. So, what I want to do is to make multiple tiles, assign them some type of material, and all the tiles with the same type of material share the same material object. I drew a little picture to illustrate it: here
Does java do this automatically? or do I have to implement it by myself? If I make two copies of the material properties, is there a way to merge them both into one?

Thank you for responding.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Lets say you have a material.

interface Material{
    void render( Context c);
}

Then you have some instance of the material.

class Concrete implements Material{
    static Texture concreteTexture = loadConcreteTexture();
    Texture texture;
    public Concrete(){
      texture = concreteTexture();
    }
    void render(Context c){
        //do stuff with texture.
    }
}

In this case there would be 1 concrete texture for all of the instances of concrete, and it gets loaded when the class gets loaded. The texture gets assigned in the constructor, but you might use a factory method instead that would create new materials and load/assign the assets.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading