https://metaplay.io logo
Entity kind specific EntityId types?
# questions-and-feedback
a
Hi, would it be a good idea to use entity kind specific EntityId types? So that instead of EntityId I could say PlayerId and OtherGamethingId? It would make things more robust so I would not mix up params in function calls etc. I recalled wrong that there were some examples in the docs or somewhere, I don't think there are. I asked Claude Sonnet and it proposed this, which seems quite sensible, but maybe a bit much. I'm curious whether Metaplay folks think this would be a good idea at all or not. https://gist.github.com/antont/53e205507765eda02b5d9ca46c71f88d
I think the implementation in the Gist would not work as a replacement for EntityId as it would not work in serialization and maybe other places .. it wraps the EntityId whereas I guess it should extend it
but it's a struct so I guess it's not possible to extend it
Claude suggested clever things like such tagged struct but I don't think that would work as a replacement for EntityId either, I think what I wanted is just not possible really and I'll just keep using EntityId
Copy code
csharp

public readonly struct TypedEntityId<TTag> : IEquatable<TypedEntityId<TTag>>
{
    public EntityId Value { get; }

    private TypedEntityId(EntityId value)
    {
        Value = value;
    }

    public static implicit operator EntityId(TypedEntityId<TTag> typed) => typed.Value;
    public static explicit operator TypedEntityId<TTag>(EntityId entityId) => new TypedEntityId<TTag>(entityId);

    public bool Equals(TypedEntityId<TTag> other) => Value.Equals(other.Value);
    public override bool Equals(object obj) => obj is TypedEntityId<TTag> other && Equals(other);
    public override int GetHashCode() => Value.GetHashCode();
    public override string ToString() => Value.ToString();
}

public struct PlayerId : TypedEntityId<IPlayerIdTag>
{
    public PlayerId(EntityId entityId) : base(entityId) {}
    public static PlayerId Create(ulong value) => new PlayerId(EntityId.Create(EntityKindCore.Player, value));
}
j
Yeah I don't think you can really replace EntityId (I guess maybe if you do some very invasive SDK patches, you can but would not recommend), if you just want a typed EntityId then I'd guess the gist will work good enough. Of course when calling metaplay APIs you need to unwrap it somehow (implicit conversion or directly accessing the value). You should be able to support serialization just fine, but the resulting byte blob will be larger than just serializing EntityId, likely pretty minor but good to be aware of.
a
i suspect that i'm just asking for trouble if i don't use EntityId as is, i prefer to have it serialized normally and have the usual type there in case something in the dashboard or somewhere needs it
.. and i don't want to be unwrapping it in places where it's passed to metaplay
j
Yeah it's definitely not super user friendly 😅