Entitas icon indicating copy to clipboard operation
Entitas copied to clipboard

PrimaryEntityIndex/EntityIndex generates wrong Contexts.cs variables

Open Gallardo994 opened this issue 4 years ago • 4 comments

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.

Gallardo994 avatar Sep 13 '21 13:09 Gallardo994

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 avatar Sep 16 '21 20:09 sschmid

Example from Match-One

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

Entitas.CodeGeneration.Plugins.Contexts = Game, \
                                          Input, \
                                          GameState, \
                                          Config

sschmid avatar Sep 16 '21 20:09 sschmid

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.

Gallardo994 avatar Sep 16 '21 22:09 Gallardo994