Calling Scripts

Parameters are passed to the script through the CallString function with automatic type deduction. Multiple return values are returned as a tuple and the return type must be specified as a template parameter (a single return value is returned as an object of that type).

Multiple parameters and return values
try {
  std::string scr = R"(
    local i, j = ...
    local k = i + j

    local s = ""
    if k < 0 then
      s = ": in the red..."
    else
      s = ": in the black!"
    end

    return k, s
  )";

  auto r = luaAPI.CallString<int, std::string>(scr, 3, -5);
  std::cout << std::get<0>(r) << std::get<1>(r) << std::endl;
} catch (std::exception& e) {
  std::cout << e.what() << std::endl;
}

The CallString function requires to stack to be empty in order to properly perform stack management. If you wish to maintain the stack yourself you can instead call the unmanaged function (CallStringU), adding the parameters to the stack beforehand, indicating the number of parameters and then reading the return values from the stack afterwards.