ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 492 forks source link

Class method stops working for no reason? #1526

Closed UltraEngine closed 9 months ago

UltraEngine commented 9 months ago

My Bone:SetRotation method works fine for several seconds, and then inexplicably reports a syntax error, as if it no longer recognizes the function parameters. I have tried creating a new Vec3 each frame, and I don't think that is the problem.

-- Main loop
while window:Closed() == false and window:KeyHit(KEY_ESCAPE) == false do

    --Update world
    world:Update()

    --Adjust the skeleton
    rotation.y = Cos(Millisecs() / 10.0) * 65.0
    neck:SetRotation(rotation)    

    --Render world
    world:Render(framebuffer)
end

Full code here: https://www.ultraengine.com/learn/Entity_Attach?lang=lua

The variable "neck" is a shared pointer of the bone class: (std::shared_ptr<Bone>). The error seems to be occurring when the garbage collector is triggered. If I call collectgarbage() it happens right away, and if I call collectgarbage("stop") the error never occurs.

This is the error. It says it expected userdata but received Bone. I don't understand what this means? Both the bone and the Vec3 should be userdata values?

Exception thrown at 0x00007FFEBC8FCF19 in Ultra Engine_d.exe: Microsoft C++ exception: Poco::NotFoundException at memory location 0x000000F939DEFE50.
Exception thrown at 0x00007FFEBC8FCF19 in Ultra Engine_d.exe: Microsoft C++ exception: Poco::NotFoundException at memory location 0x000000F939DEFE90.
Exception thrown at 0x00007FFEBC8FCF19 in Ultra Engine_d.exe: Microsoft C++ exception: sol::error at memory location 0x000000F9384FE480.
sol: runtime error: stack index 1, expected userdata, received sol.sol::d::u<UltraEngine::Bone>: value at this index does not properly reflect the desired type (bad argument into 'void(UltraEngine::Bone&, UltraEngine::Vec3&)')
stack traceback:
    [C]: in method 'SetRotation'
    [string "C:\users\xxxxx\documents\ultra engine\projects\ultra project\source\main.lua"]:54: in main chunk
Error: [string "C:\users\xxxxx\documents\ultra engine\projects\ultra project\source\main.lua"]:54: in main chunk
The program '[15532] Ultra Engine_d.exe' has exited with code 1 (0x1).

Here is the class binding code:

L->new_usertype<Bone>
(
    "BoneClass",
    sol::meta_function::index, &Object::dynamic_get,
    sol::meta_function::new_index, &Object::dynamic_set,

    //Members
    "kids", &Bone::m_kids,
    "animations", &Bone::animations,

    //Properties
    "position", sol::property([](Bone& b) { return b.position; }, [](Bone& b, Vec3 p) { b.SetPosition(p); }),
    "rotation", sol::property([](Bone& b) { return b.rotation; }, [](Bone& b, Vec3 p) { b.SetRotation(p); }),
    "quaternion", sol::property([](Bone& b) { return b.quaternion; }, [](Bone& b, Quat p) { b.SetRotation(p); }),
    "scale", sol::property([](Bone& b) { return b.scale; }, [](Bone& b, float p) { b.SetScale(p); }),
    "matrix", sol::property([](Bone& b) { return b.mat; }, [](Bone& b, Mat4 p) { b.SetMatrix(p); }),

    //Methods
    "debug", &debug,
    "SetPosition", sol::overload
    (
        [](Bone& e, float x, float y, float z) { e.SetPosition(x,y,z); },
        [](Bone& e, float x, float y, float z, bool g) { e.SetPosition(x, y, z, g); },
        [](Bone& e, Vec3 p) { e.SetPosition(p); },
        [](Bone& e, Vec3 p, bool g) { e.SetPosition(p,g); }
    ),
    "SetRotation", [](Bone& e, Vec3& p) { e.SetRotation(p); },
    /*"SetRotation", sol::overload
    (
        [](Bone& e, float x, float y, float z) { e.SetRotation(x, y, z); },
        [](Bone& e, Vec3& p) { e.SetRotation(p); }//,
        [](Bone& e, float x, float y, float z, bool g) { e.SetRotation(x, y, z, g); },
        [](Bone& e, Vec3& p, bool g) { e.SetRotation(p, g); }
    ),*/
    "Turn", sol::overload
    (
        [](Bone& e, float x, float y, float z) { e.Turn(x, y, z); },
        [](Bone& e, Vec3 p) { e.Turn(p); },
        [](Bone& e, float x, float y, float z, bool g) { e.Turn(x, y, z, g); },
        [](Bone& e, Vec3 p, bool g) { e.Turn(p, g); }
    ),
    "SetQuaternion", sol::overload
    (
        [](Bone& e, Quat p) { e.SetRotation(p); },
        [](Bone& e, Quat p, bool g) { e.SetRotation(p, g); }
    ),
    "SetScale", &SetScale,
    "SetMatrix", sol::overload
    (
        [](Bone& e, Mat4 p) { e.SetMatrix(p); },
        [](Bone& e, Mat4 p, bool g) { e.SetMatrix(p, g); }
    ),
    "GetPosition", sol::overload
    (
        [](Bone& e) {return  e.GetPosition(); },
        [](Bone& e, bool g) { return e.GetPosition(g); }
    ),
    "GetRotation", sol::overload
    (
        [](Bone& e) { return e.GetRotation(); },
        [](Bone& e, bool g) { return e.GetRotation(g); }
    ),
    "GetQuaternion", sol::overload
    (
        [](Bone& e) { return e.GetQuaternion(); },
        [](Bone& e, bool g) { return e.GetQuaternion(g); }
    ),
    "GetMatrix", sol::overload
    (
        [](Bone& e) { return e.GetMatrix(); },
        [](Bone& e, bool g) { return e.GetMatrix(g); }
    ),
    "GetScale", &GetScale
);

I'm using Lua 5.4.

Any idea what is wrong here?

UltraEngine commented 9 months ago

If I try just printing out a class member, I get this error:

sol: runtime error: [string "C:\users\xxxxxxxxx\documents\ultra engine\projects\ultra project\source\main.lua"]:49: sol: 'self' argument is lua_nil (bad '.' access?)

This only occurs with the Bone class.

UltraEngine commented 9 months ago

Son of a bitch, I did it again!: https://github.com/ThePhD/sol2/issues/1291#issuecomment-991288323

The Bone class was being declared to sol twice.