I'm using LuaInterface in C# and I "exported" some custom C# classes to be used in Lua. For instance:
local myVector = Vector2(10, 100)
But, when I want to use class operators like in this example:
local v1 = Vector2(1, 1)
local v2 = Vector2(2, 2)
local v3 = v1 + v2
I'm getting the following error: attempt to perform arithmetic on local 'p1' (a userdata value)
The C# variant of the class does have the + operator:
public static cVector2 operator +(cVector2 vector1, cVector2 vector2)
{
return new cVector2(vector1.X + vector2.X, vector1.Y + vector2.Y);
}
I know that you should make use of the Lua metatables and add a function to "__mul" for the * operator for example. But doesn't LuaInterface does that automatically? And if not, how could I automate this myself?
No. You can see for yourself via:
You'll see no
__add
metamethod.You'd have to modify the LuaInterface source to look for the
operator+
method and add the__add
metamethod. It simply doesn't do that now.Given that you have the type proxy available (because you imported the type via
import_type
), you can access operator+, which is a static method on the type.To say
v1 + v2
you'd need to modify the metamethod used by Vector2 object instance, but this requires creating an instance of the type:This affects the metamethod used by all instance, so you only need to do it once. Now you can write:
Because you need an object to edit its metamethod, it would be hard to make this cleaner. If you modify your C# code to make sure that your class has a default constructor (i.e. no parameters), you could create a wrapper for
import_type
that does this:You could extend that for other operators. Note that LuaInterface throws an exception if you try to access a member that doesn't exist (rather than returning
nil
), so we have to wrap the attempt to access a handler withpcall
.With that in place you could write:
Of course, this would work for other types that have overloaded operators.
LuaInterface is a bit of a hot mess. There are a few projects in the Lua world like it, where somebody at PUC-Rio does it as a research project, publishes a paper, then abandons it. They did it to see if they could, not because they actually use it.