英文:
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<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();
}
}
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 > 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); // <-- 调用父更新方法
}
英文:
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); // <-- call parent update
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论