sschmid / Entitas

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

Create constructor overload for PrimaryEntityIndex and EntityIndex without name #1042

Open Discipol opened 1 year ago

Discipol commented 1 year ago
[CustomEntityIndex(typeof(GameContext))]
public sealed class TileIndex : PrimaryEntityIndex<GameEntity, uint>
{
    public TileIndex(GameContext context) : base( nameof(TileIndex), etc.
}

I find it very comfortable to put the name of the class as the index name, but I'd rather not HAVE to write nameOf. My request is having 1 or more super constructor overloads to omit that parameter and just do nameof(this) inside of it.

JesseTG commented 1 year ago

I think you might have an incomplete understanding of nameof, because it's actually the most convenient option here.

nameof does not look at whatever object is passed into it; it looks at whatever symbol is given and replaces it with a string constant at compile time. So nameof(TileIndex) will always resolve to "TileIndex"; nameof exists to simplify refactoring and searching. This also works for parameters, fields, properties, local variables...like I said, it operates on names.

Since nameof doesn't look at the underlying object, it can't be used to derive a name from this, therefore nameof(this) is a compiler error.

If you're dead-set on not using nameof, you could do one of the following...

The best thing you can do is exactly what you're doing now. See here for more justification.

Discipol commented 1 year ago

I think you might have an incomplete understanding of nameof, because it's actually the most convenient option here.

nameof does not look at whatever object is passed into it; it looks at whatever symbol is given and replaces it with a string constant at compile time. So nameof(TileIndex) will always resolve to "TileIndex"; nameof exists to simplify refactoring and searching. This also works for parameters, fields, properties, local variables...like I said, it operates on names.

Since nameof doesn't look at the underlying object, it can't be used to derive a name from this, therefore nameof(this) is a compiler error.

If you're dead-set on not using nameof, you could do one of the following...

  • Pass "TileIndex" to the base constructor instead of nameof(TileIndex). But then as soon as you rename TileIndex, you'll have to find each use of TileIndex in string literals. From experience, this is more annoying than writing out nameof(...).
  • (Ask Simon to) implement a base class constructor that does accept this, then get the class name at runtime with this.GetType().Name. But that adds unnecessary runtime overhead when starting the game.

The best thing you can do is exactly what you're doing now. See here for more justification.

Thank you for the reply. This issue came from me copy/pasting an index file and forgetting to change the nameof(TileIndex). My goal was to minimize the amount of copy/paste/renamings.

I did want the super class to do GetType().Name. I wonder if I can create my own superclass and use a generic type. SO MANY THINGS TO PLAY WITH!

JesseTG commented 1 year ago

Your editor probably has a file templates feature that'll let you fill out a form or something. I use Rider, so I use the feature described here. This is exactly what file templates are for!