Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
zulqarnain1337 opened this issue Oct 13, 2023 · 1 comment
Open

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

zulqarnain1337 opened this issue Oct 13, 2023 · 1 comment

Comments

@zulqarnain1337
Copy link

zulqarnain1337 commented Oct 13, 2023


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)
@Rochet2
Copy link

Rochet2 commented Nov 5, 2023

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants