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

How to print Error Messages? I am not getting any. #1570

Closed theanimatorspal closed 6 months ago

theanimatorspal commented 6 months ago

I am using Sol3, and have the following structure in my lua Program

function load()
end
function draw()
end
function update()
end

It is similar to Love2D, however the error messages are not being even printed, suppose I call a non existing function in load(), that will be called later by the executable, it lets it happen.

This is what I have for handling in C++, l is sol::state.


    sol::protected_function draw_callback = l["Draw"];
    sol::protected_function load_callback = l["Load"];
    sol::protected_function event_callback = l["Event"];
    sol::protected_function disptach_callback = l["Dispatch"];
    sol::protected_function update_callback = l["Update"];

    auto SafeCall = [](sol::protected_function& infunc) {
        auto result = infunc();
        if (not infunc.valid()) {
            sol::error err = result;
            std::string what = err.what();
            std::cout << what << std::endl;
            exit(EXIT_FAILURE);
        }
    };

    auto Event = [&](void*) { event_callback(); };
    em.SetEventCallBack(Event);
    auto Draw = [&](void* data) {
        SafeCall(draw_callback);
    };
    w.SetDrawCallBack(Draw);
    auto Update = [&](void* data) {
        SafeCall(update_callback);
    };
    w.SetUpdateCallBack(Update);
``` This is what I am doing, I have turned SOL_ALL_SAFTIES_ON on. 
Rochet2 commented 6 months ago

Look at the example here https://sol2.readthedocs.io/en/latest/exceptions.html#various-sol-and-lua-handlers You need to change at least if (not infunc.valid()) { to if (!result.valid()) {

theanimatorspal commented 6 months ago

That was too foolish of me, Thank you.