Skip to content

Code Generator generates invalid code for components whose names are C# keywords #744

Closed
@akanix42

Description

@akanix42

Only recently started using Entitas, gotta say it's really awesome!
I just ran into this issue while setting up some components:

Create a BaseComponent:

using Entitas;

[Game]
public sealed class BaseComponent : IComponent
{
  public string value;
}

Run the Generate command in Unity.

The generated code has errors because base is a keyword in C#. My key takeaway is to just note that you can't use keywords as your component names (which of course makes sense, since you can't use keywords as variable names). However, it would be nice if the generator displayed a friendly error instead of generating invalid code.

The error message is:

Assets/Sources/Generated/Game/Components/GameBaseComponent.cs(11,29): error CS1519: 
Unexpected symbol `base' in class, struct, or interface member declaration

Here's the generated code, for reference:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by Entitas.CodeGeneration.Plugins.ComponentEntityApiGenerator.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
public partial class GameEntity {

    public BaseComponent base { get { return (BaseComponent)GetComponent(GameComponentsLookup.Base); } }
    public bool hasBase { get { return HasComponent(GameComponentsLookup.Base); } }

    public void AddBase(string newValue) {
        var index = GameComponentsLookup.Base;
        var component = CreateComponent<BaseComponent>(index);
        component.value = newValue;
        AddComponent(index, component);
    }

    public void ReplaceBase(string newValue) {
        var index = GameComponentsLookup.Base;
        var component = CreateComponent<BaseComponent>(index);
        component.value = newValue;
        ReplaceComponent(index, component);
    }

    public void RemoveBase() {
        RemoveComponent(GameComponentsLookup.Base);
    }
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by Entitas.CodeGeneration.Plugins.ComponentMatcherApiGenerator.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
public sealed partial class GameMatcher {

    static Entitas.IMatcher<GameEntity> _matcherBase;

    public static Entitas.IMatcher<GameEntity> Base {
        get {
            if (_matcherBase == null) {
                var matcher = (Entitas.Matcher<GameEntity>)Entitas.Matcher<GameEntity>.AllOf(GameComponentsLookup.Base);
                matcher.componentNames = GameComponentsLookup.componentNames;
                _matcherBase = matcher;
            }

            return _matcherBase;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions