Why is my lua script throwing exceptions for global boolean variables?

Viewed 141

I'm trying to assign a boolean default value to global variable in my Lua script and then print out or make use of the value. However, in my main program this throws an exception... In the following small program, I get no exception... but as soon as I try to output a boolean it does not output anything else (even non booleans)... Does anyone know what's going on? I've spent hours on this already....

This is the exception I get on my main program, but not on my sample program. This exception is ONLY thrown when I make the variables booleans (if I make them integers it does not happen).

[sol3] An error occurred and panic has been invoked: stack index 1, expected string, received no value: bad get from protected_function_result (is not an error)

Main.cpp

#pragma warning(push, 0)
#define SOL_ALL_SAFETIES_ON 1
#include "sol.hpp"
#pragma warning(pop)

int ExceptionHandler(
   lua_State* L,
   sol::optional<const std::exception&> maybe_exception,
   sol::string_view description)
{
   if (maybe_exception) {
      const std::exception& ex = *maybe_exception;
      std::cout << "Lua Error1: " << std::string(ex.what());
   }
   else {
      std::cout << "Lua Error2: " << std::string(description.data(), description.size());
   }

   return sol::stack::push(L, description);
}

int main()
{
   sol::state lua_state;
   lua_state.open_libraries(sol::lib::base);
   lua_state.set_exception_handler(&ExceptionHandler);

   sol::protected_function_result result = lua_state.safe_script_file("File1.lua", &sol::script_pass_on_error);
   if (!result.valid())
   {
      sol::error err = result;
      std::cout << err.what();
   }
   else
   {
      lua_state["PrintInfo"]("CPP String");
   }

   system("pause");
}

File1.lua

SampleString = "Sample";
IsSampleBool1 = false;
IsSampleBool2 = false;
IsSampleBool3 = false;
IsSampleBool4 = false;
SampleInt1 = 0;
SampleInt2 = 1;

dofile("File2.lua");

File2.lua

function PrintInfo(cpp_string)
    print("--       CPPString: " .. cpp_string);
    print("--       SampleString: " .. SampleString);
    print("--       SampleBool1: " .. IsSampleBool1);
    print("--       SampleBool2: " .. IsSampleBool2);
    print("--       SampleBool3: " .. IsSampleBool3);
    print("--       SampleBool4: " .. IsSampleBool4);
    print("--       SampleInt1: " .. SampleInt1);
    print("--       SampleInt2: " .. SampleInt2);
    print("--       End");
end
0 Answers
Related