1
votes

How can I run a function from another file on same directory?

Example:

file1:

function sleep(n)
  local t = os.clock()
  while os.clock() - t <= n do
    -- nothing
  end
end

file2:

dofile('/barboszalib.lua')

function DoSomething(target)
print(target + 3)
end

while true do
DoSomething(4)
barboszalib.sleep(5)
end
1

1 Answers

1
votes

file1 defines a global function named sleep. So you have to call sleep(5) in file2.

barboszalib.sleep(5) fails because there is no table named barboszalib.

If you want to make file1 into a library, do this:

file1:

local M={}

function M.sleep(n)
  local t = os.clock()
  while os.clock() - t <= n do
    -- nothing
  end
end

return M

and the in file2 do

local barboszalib=dofile('/barboszalib.lua')
...
barboszalib.sleep(5)

If you want require instead of dofile, do this:

local barboszalib=require('barboszalib')

but make sure Lua can find it in LUA_PATH or package.path.