libgdx/ashley

RenderingSystem with SortedIteratingSystem in wiki is wrong

Closed this issue · 4 comments

This is an issue in a part of the wiki that is misleading and plain wrong.

The following code does not work and it does not provide a good alternative/implementation of the Comparator to be used with the SortedIteratingSystem:

`public class RenderingSystem extends SortedIteratingSystem {
private ComponentMapper rm;
private ComponentMapper pm;

public RenderingSystem() {
    super(Family.all(RenderableComponent.class, PositionComponent.class).get(), new ZComparator());

    rm = ComponentMapper.getFor(RenderableComponent.class);
    pm = ComponentMapper.getFor(PositionComponent.class);
}

protected void processEntity(Entity entity, float deltaTime) {
    // Render the entity
}

private class ZComparator implements Comparator<Entity> {
    @Override
    public int compare(Entity e1, Entity e2) {
        return (int)Math.signum(pm.get(e1).z - pm.get(e2).z);
    }
}

}`

That code does not compile as the call to the super constructor should be the first thing called inside the RenderingSystem constructor, but this rule is broken when you are instantiating the ZComparator());

Now the ZComparator must be an inner class as it needs to access pm. Not sure how to solve that but the way it is right now its wrong.

The ZComparator just needed to be static, that's all. Fixed.

Sorry, Its not my intention to bother, bot setting the ZComparator to static makes the RenderingSystem.pm unaccessible from within the ZComparator, thus it does not solve the issue.

Did this ever get resolved?

I think it should be OK now.