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

PrimaryEntityIndex/EntityIndex generates wrong Contexts.cs variables #982

Closed Gallardo994 closed 1 year ago

Gallardo994 commented 2 years ago

Hello.

For some strange reason, Entitas code generator creates wrong EntityIndex/PrimaryEntityIndex pieces of code for Contexts.cs.

Say, i have 2 contexts, named ElementContext and PlayerContext, and create a component relying on IDs:

using Entitas;
using Entitas.CodeGeneration.Attributes;

[ElementContext, PlayerContext]
public class RemoteIdComponent : IComponent
{
    [PrimaryEntityIndex] public uint Value;
}

After code generation, it creates such piece of code in Contexts.cs:

public static class ContextsExtensions {

    public static ElementContextEntity GetEntityWithRemoteId(this ElementContextContext context, uint Value) {
        return ((Entitas.PrimaryEntityIndex<ElementContextEntity, uint>)context.GetEntityIndex(Contexts.RemoteId)).GetEntity(Value);
    }

    public static PlayerContextEntity GetEntityWithRemoteId(this PlayerContextContext context, uint Value) {
        return ((Entitas.PrimaryEntityIndex<PlayerContextEntity, uint>)context.GetEntityIndex(Contexts.RemoteId)).GetEntity(Value);
    }
}

Meanwhile, this is wrong. Look precisely at:

this ElementContextContext context

and

this PlayerContextContext context

ElementContextContext and PlayerContextContext does not exist (it has to be ElementContext and PlayerContext respectfully). Code generator basically adds "Context" word to the end of it. If I fix it manually, it works as intended. But because it's code generation, it will overwrite next time I press generate.

In fact, when I remove all EntityIndex/PrimaryEntityIndex, re-generation does absolutely nothing, even if I purge Generated folder and re-generate it again: errorous code gets generated again, even though nothing has EntityIndex/PrimaryEntityIndex.

I am pretty sure the issue lies here, which adds Context to the end of the variable: alt img

PR: https://github.com/sschmid/Entitas-CSharp/pull/983

Thanks.

sschmid commented 2 years ago

Hi,

this issue most likely happens, because you called your contexts ElementContext and PlayerContext instead of Element and Player

Consider the word "Context" a keyword that might get stripped away or added by code generators.

Usually your context attributes should look like this

using Entitas;
using Entitas.CodeGeneration.Attributes;

[Element, Player] // <-- no context suffix
public class RemoteIdComponent : IComponent
{
    [PrimaryEntityIndex] public uint Value;
}
sschmid commented 2 years ago

Example from Match-One

https://github.com/sschmid/Match-One/blob/master/Jenny.properties#L52-L55

Entitas.CodeGeneration.Plugins.Contexts = Game, \
                                          Input, \
                                          GameState, \
                                          Config
Gallardo994 commented 2 years ago

Should it be better to forbid usage of Context word in context names then?

I can confirm that the change fixes the issue. Considering it happens only for (Primary)EntityIndex, it might be useful to fix the issue another way than the PR I suggested. If you have a specific method on your mind, I'd be glad to submit a new PR.

Thanks.