• You've discovered RedGuides, an EverQuest multi-boxing and scripting community 🧙‍♀️⚙️. We want you to play several EQ characters at once, come join us and say hello! 👋

  • A TLP without truebox has thawed (Very Vanilla ready)
    Frostreaver

Lua - Require filepath tip and implementation

AmericanNero

Seasoned veteran member
Joined
Oct 13, 2020
RedCents
4,709¢
Hardcoding your Lua project name (package) into require statements can make it difficult to update filepaths and cause the module finder to not find your libs.

You can avoid hardcoding your Lua project / package name into your requires using the following example:

local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
require(_PACKAGE..'lib/mylib')

Likewise, if you have another directory level to traverse in a lib file, you can repeat the sequence above. In this way you can freely embed nested requires without having to type them out and correct them should they change. This is useful when comparing multiple versions of the code.

The solution to this issue, which is getting into the Lua code here, was gleaned from: https://github.com/tanema/behaviourtree.lua/blob/master/lib/behaviour_tree.lua
 
Last edited:
I tried setting search paths, but the libs weren't found unless I specified the top level directory.

I think the issue was that acc. to what I read about Lua, the preferred method to require libs was to use object notation rather than filepath. I tried both and for some reason it wouldn't work. When I look at lootly, everything looks fine, so I'm confused.

For example. Given this file structure,

Lua
myproject
init.Lua
lib
mylib.Lua

Start in EQ chatwindow: /Lua run myproject/init

init.Lua would then require('lib.mylib')

Unless I added "myproject", require ('myproject.lib.mylib') mylib.Lua wouldn't be found.

I'm a neophyte.
 
Lua - Require filepath tip and implementation

Users who are viewing this thread

Back
Top
Cart