sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

attempt to call field 'new' (a nil value)

Open zulqarnain1337 opened this issue 2 years ago • 1 comments


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)

zulqarnain1337 avatar Oct 13 '23 13:10 zulqarnain1337

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;
}

Rochet2 avatar Nov 05 '23 12:11 Rochet2