Add: allows to get LuaObject metatable statically.#247
Conversation
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}";
}
} |
|
Why isn't the generated |
|
Since static abstract members are not available in |
|
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! |
|
For anyone reading through this and leaving a little confused, I think there is a typo in the example given by @akeit0, specifically between 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}";
}
} |
No description provided.