Thursday, 8 August 2013

C# LuaInterface class operators

C# LuaInterface class operators

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 comments:

Post a Comment