Entitas icon indicating copy to clipboard operation
Entitas copied to clipboard

Support C# 7 deconstruction in components

Open ribbanya opened this issue 4 years ago • 2 comments

Hi!

I was hoping you could implement deconstructors for components in the generator.

Here's an example of what the generated code would look like:

public partial class GameEntity {

    public PositionComponent position { get { return (PositionComponent)GetComponent(GameComponentsLookup.Position); } }
    public bool hasPosition { get { return HasComponent(GameComponentsLookup.Position); } }

    public void AddPosition(float newX, float newY) {
        var index = GameComponentsLookup.Position;
        var component = (PositionComponent)CreateComponent(index, typeof(PositionComponent));
        component.x = newX;
        component.y = newY;
        AddComponent(index, component);
    }

    public void ReplacePosition(float newX, float newY) {
        var index = GameComponentsLookup.Position;
        var component = (PositionComponent)CreateComponent(index, typeof(PositionComponent));
        component.x = newX;
        component.y = newY;
        ReplaceComponent(index, component);
    }

    public void RemovePosition() {
        RemoveComponent(GameComponentsLookup.Position);
    }

//  <suggested code>
    public void Deconstruct(out float x, out float y) {
        var index = GameComponentsLookup.Position;
        var component = (PositionComponent)CreateComponent(index, typeof(PositionComponent));
        x = component.x;
        y = component.y;
    }
//  </suggested code>
}

And an example usage:

    public void Execute() {
        foreach (entity in this.entities) {
            (x, y) = entity.position;
            x += 10;
            entity.ReplacePosition(x, y);
        }
    }

Currently, this is already possible, albeit a bit verbose:

    public void Execute() {
        foreach (entity in this.entities) {
            (x, y) = (entity.position.x, entity.position.y);
            x += 10;
            entity.ReplacePosition(x, y);
        }
    }

Thanks for your consideration!

ribbanya avatar Oct 13 '19 14:10 ribbanya

Somehow I didn't notice I was extending the Entity, which makes no sense... Still, this can be generated for components themselves.

ribbanya avatar Oct 25 '19 15:10 ribbanya

Sounds good! Will do

sschmid avatar Apr 25 '20 18:04 sschmid

Hello! While working on the new code generator that uses C# IIncrementalGenerator I remembered this issue and implemented it.

https://github.com/sschmid/Entitas/issues/1005#issuecomment-1600583297

Unfortunately, very shortly after I decided to remove it again. I will close for now, but if I ever change my mind again, I will update.

sschmid avatar Jul 02 '23 20:07 sschmid

Re-opened, I might reconsider and add it

sschmid avatar Jul 06 '23 20:07 sschmid

I think that's something the component author can implement and should not be generated by default, e.g.

public sealed class MyComponent : IComponent
{
    public float Value1;
    public int Value2;

    public void Deconstruct(out float value1, out int value2)
    {
        value1 = Value1;
        value2 = Value2;
    }
}

sschmid avatar Jul 07 '23 12:07 sschmid