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

Cannot use language keywords as the name of a component #1032

Closed PopSlime closed 1 year ago

PopSlime commented 1 year ago

Describe the bug

When language keywords like Event are used as the name of a component, the code generator fails to handle the component name correctly.

To Reproduce

Generate code for the following component.

[Game]
public sealed class EventComponent : IComponent {
    public MyEvent Event;
}

Generated code:

public partial class GameEntity {

    public EventComponent event { get { return (EventComponent)GetComponent(GameComponentsLookup.Event); } }
    // ...
}

Compilation error:

Assets\Generated\Game\Components\EventComponent.cs: error CS1519: Invalid token 'event' in class, record, struct, or interface member declaration

Potential Solution

Prefix the component name with a verbatim identifier @ if it is a language keyword. For example, event becomes @event.

Entitas Version

sschmid commented 1 year ago

I removed the check for reserved keywords temporarily, because I had issues with CodeDom. I will re-add in next version.

sschmid commented 1 year ago

for reference, here's the commit to blame: https://github.com/sschmid/Entitas/commit/bf344c8182b17464785e27395c50885653bc3b95

sschmid commented 1 year ago

Fixed in Entitas 1.14.1 https://github.com/sschmid/Entitas/releases/tag/1.14.1