ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 493 forks source link

package.path error when requiring a file #1486

Closed rohezal closed 1 year ago

rohezal commented 1 year ago

First I want to thank you for this amazing project :).

Is there prefered / better way to add a folder / all files in a folder to the lua package.path? LikeaddPath("/home/user/luafiles/*); or addPathDirectory("/home/user/luafiles/);

I am using sol2 for a game engine and call a lua script when an event happens (unit spawns etc.). When I use a require in the script, it fails, because it is not in the file is not in the lua package path.

When I use "/home/my/path/?.lua" the package path becomes "/home/my/path/lua.lua" which is strange.

So I add lua file of my extracted map separately to the lua path. Which leads to a very long package path, which also fails.

        const std::string package_path = m_lua["package"]["path"];
        m_lua["package"]["path"] = std::string("/home/rohezal/.savage2/game/testfunction.lua") + ";" + package_path;

package.path = /home/rohezal/.savage2/game/playerjoin_server.lua;/home/rohezal/.savage2/game/gamestartscript_client.lua;/home/rohezal/.savage2/game/worldscript_server.lua;/home/rohezal/.savage2/game/itemscript_client.lua;/home/rohezal/.savage2/game/json.lua;/home/rohezal/.savage2/game/item_activation_functions.lua;/home/rohezal/.savage2/game/playerjoinscript_client.lua;/home/rohezal/.savage2/game/playerspawn.lua;/home/rohezal/.savage2/game/worldscript_client.lua;/home/rohezal/.savage2/game/gamestartscript_server.lua;/home/rohezal/.savage2/game/deathscript_server.lua;/home/rohezal/.savage2/game/itemscript_server.lua;/home/rohezal/.savage2/game/testfunction.lua;/home/rohezal/.savage2/game/utility_functions.lua;/home/rohezal/.savage2/game/deathscript_client.lua;/home/rohezal/.savage2/game/buildingplaced.lua;/usr/local/share/lua/5.4/?.lua;/usr/local/share/lua/5.4/?/init.lua;/usr/local/lib/lua/5.4/?.lua;/usr/local/lib/lua/5.4/?/init.lua;./?.lua;./?/init.lua;/home/rohezal/.savage2/game/playerjoinscript_server.lua ` The error was something like:

"testfunction.lua" file not found in path 
/usr/lib/x86/lua/
/usr/lib/lua/

If I add this path after creating the monster package path, it works.

        const std::string package_path = m_lua["package"]["path"];
        m_lua["package"]["path"] = std::string("/home/rohezal/.savage2/game/testfunction.lua") + ";" + package_path;

This is very confusing for me. May I ask why this works? testfunction is in the game folder when it is extracted. The path is correct. But it only works if I add the path an other time.

Rochet2 commented 1 year ago

If you use the following as a package path /home/my/path/?.lua then your require should look like this requrie("myfile") and it should not have the .lua extension as it is already in the package path. The question mark ? will be replaced with what you pass into the require function. Take a look at this https://www.lua.org/pil/8.1.html

I believe there is no way to include a directory tree without some kind of additional system to walk through a directory tree. I myself have used dirent as set up here. Dirent is available on linux and mac by default and there is a MIT windows implementation in single file for lightweight cross platform use.

Based on the error message you show being so short, it looks as if the package path was not yet replaced when your code that contains require(testfunction.lua") was run. The error should contain all of the paths searched, and it shows only two instead of several. I might be wrong, but I cant think of another reason from the top of my head.

rohezal commented 1 year ago

I was to stupid to use lua.

Should have used

test = require "testfunction" instead of

test = require "testfunction.lua"

My bad.