Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Override the require Lua Function in C++ for Custom Script Loading #1669

Closed
gsisinna opened this issue Feb 6, 2025 · 2 comments
Closed

Comments

@gsisinna
Copy link

gsisinna commented Feb 6, 2025

Hi,
I'm working on a project where I need to override the require function in Lua using C++.

My goal is to achieve the following:

  1. Look up in a scripts map (C++): I have a map in C++ that contains script content and other flags (e.g., debug_mode = true/false). I want the require function to first check this map for the script.

  2. Load at runtime: If the script is not found in the map, I want the require function to search for Lua scripts in a specified package path at runtime.
    (Without hard-coding this path in the Lua script, but from a config file I parse in C++).

Here are some examples to illustrate what I'm trying to achieve:

Lua Example:

-- Custom require function in Lua
function custom_require(name)
    -- Check the C++ map for the script
    local script = cpp_lookup_script(name)
    if script then
        return load(script)()
    end

    -- Fallback to the default require behavior
    return original_require(name)
end

-- Override the default require function
original_require = require
require = custom_require

C++ Example:

#include <sol/sol.hpp>
#include <unordered_map>
#include <string>

// Example script map
std::unordered_map<std::string, std::string> script_map = {
    {"example", "print('Hello from example script!')"}
};

// Function to look up scripts in the map
std::string cpp_lookup_script(const std::string& name) {
    auto it = script_map.find(name);
    if (it != script_map.end()) {
        return it->second;
    }
    return "";
}

int main() {
    sol::state lua;
    lua.open_libraries(sol::lib::base);

    // Expose the C++ lookup function to Lua
    lua.set_function("cpp_lookup_script", cpp_lookup_script);

    // Load and execute the Lua script that overrides require
    lua.script(R"(
        original_require = require
        function custom_require(name)
            local script = cpp_lookup_script(name)
            if script ~= "" then
                return load(script)()
            end
            return original_require(name)
        end
        require = custom_require
    )");

    // Test the custom require function
    lua.script(R"(
        require('example')
    )");

    return 0;
}

Could you provide guidance or examples on how to implement this correctly in sol2?

Thank you!

@Perl99
Copy link

Perl99 commented Feb 6, 2025

@gsisinna
Copy link
Author

Maybe this example will be helpful: develop/examples/source/require_override_behavior.cpp

Thank you very much, after some trials I was able to overload the require function and return scripts from a custom map in C++ 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants