Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to require file from relative path in lua

Tags:

lua

My directory structure looks like this:

|-- ball.lua |-- entity.lua |-- test     `-- ball_test.lua 

I'm using the following code in test/ball_test.lua to require ball.lua from the parent directory:

package.path = package.path .. ";../entity.lua" require("entity") package.path = package.path .. ";../ball.lua" require("ball") 

entity.lua is a dependency of ball.lua. So I require("entity") first otherwise I get a module 'entity.lua' not found error. This seems like a hack, what's a better way to do this?

like image 601
Seth Reno Avatar asked Apr 23 '11 00:04

Seth Reno


People also ask

Where does Lua look for require?

To determine its path, require first checks the global variable LUA_PATH . If the value of LUA_PATH is a string, that string is the path. Otherwise, require checks the environment variable LUA_PATH .

How do you specify relative paths?

Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy. A single dot represents the current directory itself.

What is Dofile Lua?

The dofile function is actually an auxiliary function; loadfile does the hard work. Like dofile , loadfile also loads a Lua chunk from a file, but it does not run the chunk. Instead, it only compiles the chunk and returns the compiled chunk as a function.


1 Answers

package.path = package.path .. ";../?.lua" 

will work for both.

like image 96
Doug Currie Avatar answered Sep 26 '22 03:09

Doug Currie