UnityProPlejer
UnityProPlejer

Reputation: 31

Require module in LUA

So I have a main LUA folder (extracted folder from zip) on C drive in C:\Lua\ folder. How can I require my own module that is located in: D:\Users\Admin\Desktop\LuaMod\Modules\myModule.lua

to a file that is located in: D:\Users\Admin\Desktop\LuaMod\main.lua ?

I've search everything but nothing worked.

Upvotes: 3

Views: 4112

Answers (2)

IsLilyYaGirl
IsLilyYaGirl

Reputation: 46

Have you tried:

require("Modules/myModule")

since one's in a sub-folder of the other and therefore can be indexed by slashes?

Upvotes: 2

Nifim
Nifim

Reputation: 5021

You can add the path to package.path, this is a list of places lua will look for a file when you call require.

Simple solution:

package.path = package.path .. ";D:/Users/Admin/Desktop/LuaMod/?.lua"

This cause to require to look for the give .lua file in D:/Users/Admin/Desktop/LuaMod/ but it will not look for the file in nested folders(ie ..\main\main.lua) and it wont find any .dll files.

To do that you can add more locations:

package.path = package.path .. ";D:/Users/Admin/Desktop/LuaMod/?.lua;D:/Users/Admin/Desktop/LuaMod/?/?.lua;D:/Users/Admin/Desktop/LuaMod/?/init.lua"
package.cpath = package.cpath .. ";D:/Users/Admin/Desktop/LuaMod/?.dll;D:/Users/Admin/Desktop/LuaMod/?/?.dll;D:/Users/Admin/Desktop/LuaMod/?/core.dll"

Resources:

Lua User's Wiki: Package Path

Lua Reference Manual: 5.3 Modules

Upvotes: 2

Related Questions