I have integrated Lua 5.3 in my C++ code and I have added several math classes that should be interchangeable between the two environments.
For example, I have a vec2 Lua metatable with C functionality to link it to my C++ class for vec2d.
Now, my vec2 metatable has a __mul operator so that I can write Lua code like:
local vector = vec2.create(1, 1)
local scaledVector = vector * 5
print(tostring(scaledVector)) -- outputs 5, 5
But sometimes, I just want to write it the other way around, I want this to work too:
local vector = vec.create(1, 1)
local scaledVector = 5 * vector -- error: Class metatable function __index called on something else than userdata
print(tostring(scaledVector)) -- I want 5, 5
I understand why it doesn't work.
Is this at all possible in Lua? And if so... how? (And I am looking for a C/C++ solution, not some kind of construction written in Lua)
__muland__tostring?