Ashley ECS 迭代系统未处理实体组合

huangapple go评论68阅读模式
英文:

Ashley ECS IteratingSystem not processing entity family

问题

一些原因导致我无法使IteratingSystem正常工作。在更新中,我会仔细检查是否存在实体,它返回的数量超过0。但这应该意味着processEntity会在这些实体上调用,但它从未运行。

//...
import net.madmenyo.teststuff.AshleyTest.components.MovementComponent;
import net.madmenyo.teststuff.AshleyTest.components.PositionComponent;
//...

public class MovementSystem extends IteratingSystem {
    private ComponentMapper<PositionComponent> pm;
    private ComponentMapper<MovementComponent> mm;

    public MovementSystem () {
        super(Family.all(PositionComponent.class, MovementComponent.class).get());
        pm = ComponentMapper.getFor(PositionComponent.class);
        mm = ComponentMapper.getFor(MovementComponent.class);
    }

    @Override
    public void processEntity (Entity entity, float deltaTime) {
        System.out.println("Process moveSystem");
        PositionComponent position = pm.get(entity);
        MovementComponent movement = mm.get(entity);

        position.x += movement.velocityX * deltaTime;
        position.y += movement.velocityY * deltaTime;
    }

    @Override
    public void update(float deltaTime) {
        Engine engine = getEngine();

        int count = engine.getEntitiesFor(Family.all(PositionComponent.class, MovementComponent.class).get()).size();
    }
}

据我所知,在update方法中的每次循环中,它都应该每次更新引擎时都运行ProcessEntity。由于调用了update方法,引擎会更新,而且由于count > 0,它应该处理实体,然而它从未被调用。通过在运行时检查引擎,我还发现它包含了我定义的多个族群(Family)的实体。

英文:

Somehow I cannot get the IteratingSystem to work. In the update I double check if there are entities, it returns more then 0. But that should mean that processEntity gets called on those but it never runs.

//...
import net.madmenyo.teststuff.AshleyTest.components.MovementComponent;
import net.madmenyo.teststuff.AshleyTest.components.PositionComponent;
//...

public class MovementSystem extends IteratingSystem {
    private ComponentMapper&lt;PositionComponent&gt; pm;
    private ComponentMapper&lt;MovementComponent&gt; mm;

    public MovementSystem () {
        super(Family.all(PositionComponent.class, MovementComponent.class).get());
        pm = ComponentMapper.getFor(PositionComponent.class);
        mm = ComponentMapper.getFor(MovementComponent.class);
    }

    @Override
    public void processEntity (Entity entity, float deltaTime) {
        System.out.println(&quot;Process moveSystem&quot;);
        PositionComponent position = pm.get(entity);
        MovementComponent movement = mm.get(entity);

        position.x += movement.velocityX * deltaTime;
        position.y += movement.velocityY * deltaTime;
    }

    @Override
    public void update(float deltaTime) {
        Engine engine = getEngine();

        int count = engine.getEntitiesFor(Family.all(PositionComponent.class, MovementComponent.class).get()).size();
    }
}

Afaik, for each count in the update method it should run ProcessEntity each time the engine is updated. Since update is called the engine is updated and since count &gt; 0 it should be processing entities, yet it is never called. By inspecting the engine during runtime it also contains multiple entities for the family I define.

答案1

得分: 1

processEntity(...) 被父更新方法调用,我之前没有调用它。再次查看后很容易解决,我将其留在这里以备案。

@Override
public void update(float deltaTime) {
    super.update(deltaTime); // &lt;-- 调用父更新方法
}
英文:

processEntity(...) is called by the parent update method which I wasn't calling. Easy fix once I looked at it again, I just leave it here in case.

@Override
public void update(float deltaTime) {
	super.update(deltaTime); // &lt;-- call parent update
}

huangapple
  • 本文由 发表于 2020年9月14日 17:47:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63881921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定