sschmid / Entitas

Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
MIT License
7.08k stars 1.11k forks source link

Is it safe to store a List<GameEntity> in a Component? #964

Closed atkdefender closed 3 years ago

atkdefender commented 3 years ago

Players and Monsters must fight each other in order. I wonder if I write it right. And in what situation this will go wrong.

[State, Unique]
public sealed class BattleComponent : IComponent
{
    public List<GameEntity> partners;
    public List<GameEntity> monsters;
}
WeslomPo commented 3 years ago

No, it is not safe, and it is against rules. If you need to link entities - make aIdComponent with unique int number in it, and made it PrimaryEntityIndex, then you can save Id in arrays and take entities by it with stateContext.GetEntityWithId.

[State]
    public sealed class IdComponent : IComponent {
        [PrimaryEntityIndex] public int Value;
        public override string ToString() => $"Id[{Value}]";
    }

Regarding the problem, that you have. Mark each partner with PartnerComponent, and enemies with EnemyComponent, make a component - BattleOrderComponent - that will have int order value with EntityIndexAttribute (or PrimaryEntityIndex), that represent the order of fight. In one system calculate order and assign it to units.

Now you have entities with order of fight and distinct relationships with each other.

atkdefender commented 3 years ago

No, it is not safe, and it is against rules. If you need to link entities - make aIdComponent with unique int number in it, and made it PrimaryEntityIndex, then you can save Id in arrays and take entities by it with stateContext.GetEntityWithId.

[State]
  public sealed class IdComponent : IComponent {
      [PrimaryEntityIndex] public int Value;
      public override string ToString() => $"Id[{Value}]";
  }

Regarding the problem, that you have. Mark each partner with PartnerComponent, and enemies with EnemyComponent, make a component - BattleOrderComponent - that will have int order value with EntityIndexAttribute (or PrimaryEntityIndex), that represent the order of fight. In one system calculate order and assign it to units.

Now you have entities with order of fight and distinct relationships with each other.

If putting a BattleOrderComponent on partner/enemy, to GetNextPartnerEnemy() must contexts.GetGroup(GameMatcher.BattleOrder), then sort the entities by battleOrder everytime. Maybe List partnerIds;/monsterIds is easy enough. Or it has some better way that I don't imagine. Please tell me : )