libgdx/ashley

Before-after update methods in IteratingSystem

rpazyaquian opened this issue · 1 comments

I think it'd be convenient to add a version of IteratingSystem that has overrideable callback methods that run before the main body of update. I wanted to use an IteratingSystem to render all my sprites in a single batch, instead of opening and closing a new batch every time processEntity was run. So I tried to override update, like so:

public class RenderSystem extends IteratingSystem {

    private SpriteBatch batch;
    private OrthographicCamera camera;

    private ComponentMapper<PositionComponent> pm = Mappers.position;
    private ComponentMapper<TextureComponent> tm = Mappers.texture;

    public RenderSystem(OrthographicCamera camera) {
        super(Family.all(PositionComponent.class, TextureComponent.class).get());

        batch = new SpriteBatch();
        this.camera = camera;
    }

    @Override
    public void update(float deltaTime) {
        camera.update();

        batch.begin();

        batch.setProjectionMatrix(camera.combined);
        super(deltaTime);  // yes, i know this doesn't work the way i think it does
        batch.end();
    }

    @Override
    protected void processEntity(Entity entity, float deltaTime) {

    }
}

It didn't work, for obvious reasons. Replacing the super call with the contents of IteratingSystem's update method seemed redundant, and I considered extending IteratingSystem and just adding beforeUpdate and afterUpdate methods, but I was talked out of it by people who are better at OOP than I am. So, at the moment, the current solution is to just do it the normal way via extending EntitySystem and handling the convenience methods yourself.

Is this something that could potentially be of use, or is it inadvisable for some reason?

Never mind, I'm an idiot:

    @Override
    public void update(float deltaTime) {
        camera.update();

        batch.begin();

        batch.setProjectionMatrix(camera.combined);
        super.update(deltaTime);
        batch.end();
    }

All is well!