5

I have something like this.

/config.lua
/client/init.lua

How can I include the config.lua with include() or dofile()? Lg

Blowfish
  • 130
  • 6
nn3112337
  • 459
  • 6
  • 21
  • 1
    `dofile"/config.lua"` doesn't work for you? – lhf Dec 17 '13 at 19:27
  • no: http://puu.sh/5PTeZ.png – nn3112337 Dec 17 '13 at 20:29
  • Odd. You're on Linux and the root of your filesystem has config.lua? So if you do "ls /*.lua" you see config.lua listed? – Oliver Dec 17 '13 at 22:55
  • You can find out what your working directory is when running your script. Use something like `os.execute("cd")` or `popen` as suggested below. Once you know the current working dir you can construct the path to the target script relative to that directory. – greatwolf Dec 17 '13 at 23:55
  • well, the problem is I run the scripts from a Just Cause 2 MP Server, so nothing really happens when I'm trying to do that. Altough I don't understand atm what exactly you mean, so it would be nice if you can explain it retard-like. thanks – nn3112337 Dec 18 '13 at 13:59
  • Can you use `print` or `io.write` in your script from this program? If so, try `print(io.popen('cd', 'r'):read'*a')`, and it should display where the current working directory is. – Ryan Stein Dec 18 '13 at 19:02
  • http://puu.sh/5QToB.png its shows this – nn3112337 Dec 18 '13 at 19:34

2 Answers2

5

You can (and probably should) do this with require by adding ../?.lua to package.path, like so:

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

See the require and package.path docs for more info.

furq
  • 5,648
  • 3
  • 16
  • 21
0

You're on the right track with dofile. (However, there is no include function in Lua.) As you may have noticed, you can't do this with require:

local cfg = require'../config'

require typically works from the directory of the initial script in a one-way direction, and any required modules which require their own modules must use relative names from that starting point to work properly.

I.e., if you have the following structure:

--+ main.lua          requires 'lib.test1'
  +-- lib/test1.lua   requires 'test2'
  +-- lib/test2.lua

test1.lua will fail to require test2 because it cannot be found from the initial directory. lib.test2 is the appropriate module name here. I'm not sure if there are any good patterns for this, short of writing your own stateful require, but it's helpful to know about when writing library code.

Perhaps it's a bad sign when it comes to this.


Going back to the question, you could make an exemption for your config file in package.loaded. This is effectively loading it manually:

package.loaded.config = dofile'../config.lua'
-- ...
local cfg = require'config'
Ryan Stein
  • 7,930
  • 3
  • 24
  • 38
  • Hello, first of all thanks for you answer, but it doesn't work. (my config is named job_config): http://puu.sh/5PWb3.png the structure looks like that: (in both folders there is a .lua who needs to include the config) http://puu.sh/5PWdB.png – nn3112337 Dec 17 '13 at 21:13
  • Where is your current directory in relation to `job_config.lua`? You should be able to determine this by printing `io.popen('cd', 'r'):read'*a'` somewhere or running `os.execute'cd'` in a terminal. – Ryan Stein Dec 17 '13 at 21:18