/**
* This example shows how to deal with runtime errors in Lua scripts.
*/
int main()
{
rlogic::LogicEngine logicEngine;
/**
* This script contains a runtime error, i.e. from Lua point of view this is
* valid syntax, but the type check of the Logic engine will fire a runtime
* error for trying to assign a string to a VEC4F property
*/
rlogic::LuaScript* faultyScript = logicEngine.createLuaScript(R"(
function interface()
OUT.vec4f = VEC4F
end
function run()
OUT.vec4f = "this is not a table with 4 floats and will trigger a runtime error!"
end
)");
// Script is successfully created, as it is syntactically correct
assert(faultyScript != nullptr);
/**
* Update the logic engine including our script
* Because there is a runtime error in the script, the execution will return "false"
*/
assert(!logicEngine.update());
// To get further information about the issue, fetch errors from LogicEngine
auto errors = logicEngine.getErrors();
assert(!errors.empty());
// Print out the error information
for (const auto& error : errors)
{
/**
* The stack trace is coming from the Lua VM and has limited information on the error. See the
* docs at https://ramses-logic.readthedocs.io/en/latest/api.html#additional-lua-syntax-specifics for more information
*/
std::cout << "Script '" << error.object->getName() << "' caused a runtime error:\n" << error.message << std::endl;
}
logicEngine.destroy(*faultyScript);
return 0;
}