Handling runtime errorsΒΆ

/**
 * 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
    )", {}, "faultyScript");

    // Script is successfully created, as it is syntactically correct
    assert(faultyScript != nullptr);

    /**
     * Update the logic engine including the faulty script above.
     * Because there is a runtime error in the script, the execution will return "false" and print
     * the error alongside stacktrace in the logs.
     * The stack trace is coming from the Lua VM and has limited information on the error. See
     * https://ramses-logic.readthedocs.io/en/latest/lua_syntax.html#the-global-in-and-out-objects for more information
     * how to read the stack trace
     */
    logicEngine.update();

    // The LogicEngine provides a list of errors from the last call. Use it to get
    // the error message and a pointer to the object which causes the error.
    assert(
        logicEngine.getErrors().size() == 1u &&
        logicEngine.getErrors()[0].object == faultyScript);

    logicEngine.destroy(*faultyScript);

    return 0;
}