jeffcampbellmakesgames / Entitas-Redux

An entity-component framework for Unity with code generation and visual debugging
MIT License
102 stars 13 forks source link

[BUG] Custom indexes are not generated #56

Closed miroslavzeman closed 3 years ago

miroslavzeman commented 3 years ago

Describe the bug No code related to custom indexes is generated at all. The bug is probably related to previously reported bug with indexes not being generated.

Unity Version: Unity 2020.3.0f1

To Reproduce

  1. Setup project as same as in previously reported bug

  2. Create component

[Game]
public class ExampleComponent : IComponent
{
    public int value;
}
  1. Create custom index
using JCMG.EntitasRedux;

[CustomEntityIndex(typeof(GameContext))]
public sealed class ExampleIndex : PrimaryEntityIndex<GameEntity, int>
{
    public ExampleIndex(GameContext context) : base(
        nameof(ExampleIndex),
        context.GetGroup(GameMatcher.Example),
        (entity, component) => (
            component is ExampleComponent exampleComponent
                ? exampleComponent.value
                : entity.Example.value
        ))
    {
    }

    [EntityIndexGetMethod]
    public GameEntity GetExample(int value)
    {
        return GetEntity(value);
    }
}
  1. Generate code via GenesisSettings's Generate button
  2. GetExample() method should be available in Game context, but it's not

Expected behavior Additional code should be generated for context to get entities by custom index.

Additional context Win 10, using Mono .NET 4.x in Unity player settings

miroslavzeman commented 3 years ago

I'm sorry, I thought there are some helper methods generated as same as in Entitas, but the usage in tests, is different so there's probably no additional code generated because it's not required.

Closing this one. Sorry!

JesseTG commented 2 years ago

No, actually, I think this is a legitimate bug. Looking at the API and implementation for the code generators here, I see no reason that [EntityIndexGetMethod]-marked methods shouldn't be used to generate extension methods for their associated Context. I'm running into this issue as well; my custom indexes are being recognized by Genesis because they're being added to the generated Contexts.InitializeEntityIndices method. However, the generated ContextsExtensions class has suspicious gaps where the generated index extension methods should be.

JesseTG commented 2 years ago

@jeffcampbellmakesgames Would you be able to take a look at this? I suspect the issue is somewhere in EntityIndexGenerator.

Specifically, I think this area is the best candidate:

https://github.com/jeffcampbellmakesgames/Entitas-Redux/blob/e566ed345a663edbc7ecf2428655e8edff132ec0/Plugins/EntitasRedux.Core.Plugins/EntityIndex/CodeGenerators/EntityIndexGenerator.cs#L168-L226

The reason for that is that instead of extension methods for custom entity indexes, the relevant part of my generated Contexts.cs file has more blank spaces than usual. Some string is probably being replaced with whitespace, newlines, or string.Empty behind your back.

I tried debugging it myself, but the local builds I'm using (due to pending PRs containing features I need) are complicating that process.

JesseTG commented 2 years ago

Ta-da!