sschmid / Entitas

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

Replace one property of a component #979

Closed woshiZS closed 1 year ago

woshiZS commented 3 years ago

Hi,

I am wondering if there is a way to change a property of my component and update the index of that property.

Here's some code

using Entitas;

public sealed class MyComponent : IComponent {
    [EntityIndex]
    public long TeamId{get; set;}
    // other properties
    ......
}

If I just change value of TeamId, the index will not be notified by the group. Instead I need to replace or remove the correspoding componet so that the Index will refreshed. Is there a easier way to notify my index without removing or replacing other properties? Cheers

JustinSchneider commented 3 years ago

We've done this in our project. I can't recall how we handled it exactly off the top of my head, and I'm leaving for vacation in the morning.

If you haven't gotten your solution in 1 week, remind me and I'll share our approach.

sschmid commented 1 year ago

@woshiZS when you use entity.ReplaceMyComponent(teamId, other, properties) it should always update the index. This is fine to do and will also reuse the components itself.

You can also consider to have less properties per component, e.g.

public sealed class TeamIdComponent : IComponent {
    [EntityIndex]
    public long Value;
}

btw, you don't need get and set. I would recommend to get rid of those, unless you need them for sth