Closed rleigh-codelibre closed 2 years ago
This was primarily a misunderstanding on my part, I was missing the need for sol::overload
, and I have got it working with:
lua.new_usertype<glm::vec1>(
"vec1",
sol::constructors<glm::vec1(),
glm::vec1(float const&)>(),
sol::meta_function::multiplication, sol::overload(sol::resolve<glm::vec1 (glm::vec1 const&, glm::vec1 const&)>(&glm::operator*), sol::resolve<glm::vec1 (glm::vec1 const&, float)>(&glm::operator*)),
sol::meta_function::division, sol::resolve<glm::vec1 (glm::vec1 const&, glm::vec1 const&)>(&glm::operator/),
sol::meta_function::addition, sol::resolve<glm::vec1 (glm::vec1 const&, glm::vec1 const&)>(&glm::operator+),
sol::meta_function::subtraction, sol::resolve<glm::vec1 (glm::vec1 const&, glm::vec1 const&)>(&glm::operator-)
);
which does the job nicely.
lua.script("print(vec1.new(5.5))\n"
"print(vec1.new(5.5) * 4.0)\n"
"print(vec1.new(5.5) * vec1.new(2.1))");
==>
vec1(5.500000)
vec1(22.000000)
vec1(11.549999)
I'm attempting to wrap the C++ GLM matrix and vector types to make them accessible within Lua. Compiler is from VS2022 via CLion, no special options used other than warnings.
The wrapping of all of the matrix types (
mat2
,mat3
andmat4
) works fine, as does wrapping of the vector types (vec1
,vec2
,vec3
andvec4
). I've hit a problem when I tried to overload thevec1
operators for both vector and scalar variants. It compiles fine, but fails at runtime. With the above:but if I uncomment the
print(vec1.new(5.5) * vec1.new(2.1))
line:So it's using the "float" variant rather than the "vec1" variant. If I reorder the meta-functions:
I see:
So it's working fine for the
vec1
case but not thefloat
case. So it looks like in both cases, only one of the overloaded operators is being used, rather than it selecting the correct variant.Is this something which is expected to work, or am I using sol2 incorrectly to register these overloads?
One other question: when you use
lua.new_usertype<Type>
, is it possible to add these to a table other than the root table so I can collect all of the library functions in a specific table?Many thanks, Roger