Skip to content

Add: allows to get LuaObject metatable statically.#247

Merged
nuskey8 merged 1 commit into
mainfrom
feature-static-metatable-access
May 1, 2026
Merged

Add: allows to get LuaObject metatable statically.#247
nuskey8 merged 1 commit into
mainfrom
feature-static-metatable-access

Conversation

@akeit0
Copy link
Copy Markdown
Collaborator

@akeit0 akeit0 commented Jan 17, 2026

No description provided.

@akeit0
Copy link
Copy Markdown
Collaborator Author

akeit0 commented Jan 17, 2026

local v = Vector2(1, 2)
state.Environment["Vector2"] = new (LuaVector2.TypeMeta);

[LuaObject]
public partial class LuaVector2
{
    class TypeMetatableClass : ILuaUserData
    {
        LuaTable? ILuaUserData.Metatable
        {
            get => LuaVector2.Metatable;
            set => throw new NotSupportedException();
        }
    }

    public static ILuaUserData TypeMeta
    {
        get
        {
            typeTable ??= new TypeMetatableClass();
            return typeTable;
        }
    }

    static TypeMetatableClass? typeMeta;
    float x;
    float y;

    [LuaMember("x")]
    public float X
    {
        get => x;
        set => x = value;
    }

    [LuaMember("y")]
    public float Y
    {
        get => y;
        set => y = value;
    }

    [LuaMetamethod(LuaObjectMetamethod.Call)]
    public static LuaVector2 Create(LuaValue _, float x, float y)
    {
        return new LuaVector2() { x = x, y = y };
    }

    [LuaMetamethod(LuaObjectMetamethod.Add)]
    public static LuaVector2 Add(LuaVector2 a, LuaVector2 b)
    {
        return new LuaVector2() { x = a.x + b.x, y = a.y + b.y };
    }

    [LuaMetamethod(LuaObjectMetamethod.ToString)]
    public override string ToString()
    {
        return $"x:{X} y:{Y}";
    }
}

@akeit0 akeit0 requested a review from nuskey8 January 17, 2026 14:02
@akeit0 akeit0 changed the title add: allows to get LuaObject metatable statically. Add: allows to get LuaObject metatable statically. Jan 17, 2026
@nuskey8
Copy link
Copy Markdown
Owner

nuskey8 commented May 1, 2026

Why isn't the generated global::Lua.LuaTable? global::Lua.ILuaUserData.Metatable static? Since it's essentially an alias for an internal static Metatable, I think we should rename the current static Metatable and make this one static.

@akeit0
Copy link
Copy Markdown
Collaborator Author

akeit0 commented May 1, 2026

Since static abstract members are not available in .netstandard2.1...

@nuskey8
Copy link
Copy Markdown
Owner

nuskey8 commented May 1, 2026

Ah, I understand. From the perspective of IUserData implementation, this makes sense.

However, it's unnatural behavior for an instance's properties to overwrite the metamethods of the entire type, so it might need to be replaced with a more appropriate API. But that's not the main point of this PR, so I'll merge it first. Thanks!

@nuskey8 nuskey8 merged commit 529b177 into main May 1, 2026
@ashtonmeuser
Copy link
Copy Markdown
Contributor

For anyone reading through this and leaving a little confused, I think there is a typo in the example given by @akeit0, specifically between typeTable and typeMeta. If I'm not mistaken, the example should be:

state.Environment["Vector2"] = new (LuaVector2.TypeMeta);

[LuaObject]
public partial class LuaVector2
{
    class TypeMetatableClass : ILuaUserData
    {
        LuaTable? ILuaUserData.Metatable
        {
            get => LuaVector2.Metatable;
            set => throw new NotSupportedException();
        }
    }

    public static ILuaUserData TypeMeta
    {
        get
        {
            typeMeta ??= new TypeMetatableClass();
            return typeMeta;
        }
    }

    static TypeMetatableClass? typeMeta;
    float x;
    float y;

    [LuaMember("x")]
    public float X
    {
        get => x;
        set => x = value;
    }

    [LuaMember("y")]
    public float Y
    {
        get => y;
        set => y = value;
    }

    [LuaMetamethod(LuaObjectMetamethod.Call)]
    public static LuaVector2 Create(LuaValue _, float x, float y)
    {
        return new LuaVector2() { x = x, y = y };
    }

    [LuaMetamethod(LuaObjectMetamethod.Add)]
    public static LuaVector2 Add(LuaVector2 a, LuaVector2 b)
    {
        return new LuaVector2() { x = a.x + b.x, y = a.y + b.y };
    }

    [LuaMetamethod(LuaObjectMetamethod.ToString)]
    public override string ToString()
    {
        return $"x:{X} y:{Y}";
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants