2
\$\begingroup\$

I have been working on libGDX and the Ashley framework, and I have not been able to find any information on resource control in memory.

I would like to know if there is any way to control those entities that are in memory before and after closing the application or change the screen. Mainly to add the dispose() methods where necessary to avoid unnecessary resources loaded into memory.

\$\endgroup\$
1
\$\begingroup\$

The Ashley ECS framework typically does not control the loading or management of resources as this would usually be done separately by something like an asset manager or in a bespoke loader.

If however you have a bespoke loading system and are more interested in using Ashley to trigger the dispose methods you can use an EntityListener

Let's just say you have a textureComponent which is used to store the image for an enemy and when the enemy is removed you no longer need any references to that image and you want it disposed.

You would first make an EntityListener:

import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntityListener;

public class TexturecompListener implements EntityListener{


    @Override
    public void entityAdded(Entity entity) {    
    }

    @Override
    public void entityRemoved(Entity entity) {
        TextureComponent tc = ComponentMapper.getFor(TextureComponent.class).get(entity);
        tc.region.getTexture().dispose();
    }

}

Now you have a listener you need to add it to your engine with a family so it knows which entities this listener should be linked to:

engine.addEntityListener(
            Family.all(TextureComponent.class).get(),   // set which entities to call this lisneter for
            new TexturecompListener());                 // set the listener

This would then dispose the image when the entity is removed from the engine.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.