libgdx / ashley

A Java entity system inspired by Ash & Artemis.
Apache License 2.0
861 stars 145 forks source link

entity with special tag/flag (like family.forAll()) #150

Closed mars3142 closed 9 years ago

mars3142 commented 9 years ago

How can I find entities with special values?

For example, I have entities (human and NPC) in my engine. Every entity has the same components, but I just need the human entity. How can I find it with my system? Is the only way to iterate through all entities and check the tag/flag?

I don't want to create a HumanComponent and NpcComponent, because I don't know how much different components it would be in the end. It would be better to add a TagComponent with a special string or integer value, which can be filtered directly with ashley.

54k commented 9 years ago

You can create tag system which contains map for tag-entity relationship. Refer to https://github.com/junkdog/artemis-odb/blob/master/artemis/src/main/java/com/artemis/managers/TagManager.java for example.

As for me I don't think that systems like tag managers, schedulers and other utilities should be in the core framework.

andresaraujo commented 9 years ago

You could create a Tag component and group them in arrays depending on the value of the tag

class Tag extends Component {
  public static final ComponentMapper<Tag> mapper = ComponentMapper.for(Tag.class);
  public String value;
  public Tag(String value) {
    this.value = value;
  }
}

//... in your system 
  public void update(float deltaTime) {
    super.update(deltaTime);

    //Do something with npcs, humans etc

    npcs.clear();
    humans.clear();
  }

  public void processEntity(Entity entity, float deltaTime) {
    Tag tag = Tag.mapper.get(entity);
    switch(tag.value) {
      case "human": humans.add(entity); break;
      case "npc": npcs.add(entity); break;
    }
  }
dsaltares commented 9 years ago

I think this pretty much answers the question.