Entitas
Entitas copied to clipboard
Support C# 7 deconstruction in components
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!
Somehow I didn't notice I was extending the Entity, which makes no sense... Still, this can be generated for components themselves.
Sounds good! Will do
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.
Re-opened, I might reconsider and add it
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;
}
}