sol2
sol2 copied to clipboard
attempt to call field 'new' (a nil value)
lua_state.new_usertype<Vector_t>("vector",
sol::constructors <Vector_t(), Vector_t(float, float, float)>(),
"x", &Vector_t::x,
"y", &Vector_t::y,
"z", &Vector_t::z
);
local vec = vector.new(0,0,0); -- attempt to call field 'new' (a nil value)
Can you provide more information? For example what version of sol you are using, what version of lua, how Vector_t looks like exactly?
I tried to replicate the issue in godbolt but I did not succeed: https://godbolt.org/z/6Yxz7o6od Everything seems to work fine.
#include <limits>
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
struct Vector_t {
int x,y,z;
};
int main() {
sol::state lua;
lua.open_libraries();
lua.new_usertype<Vector_t>("vector",
sol::constructors <Vector_t(), Vector_t(float, float, float)>(),
"x", &Vector_t::x,
"y", &Vector_t::y,
"z", &Vector_t::z
);
lua.script(R"(
local v = vector.new()
print(v.x, v.y, v.z)
local v = vector.new(1,2,3)
print(v.x, v.y, v.z)
local v = vector.new(0,0,0)
print(v.x, v.y, v.z)
)");
return 0;
}