lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Igor Trevisan
> Sent: woensdag 30 april 2014 12:23
> To: Lua mailing list
> Subject: Re: How to load more than a file from C
> 
> Hi,
> 
> 
> On 30 April 2014 10:32, Thijs Schreijer <thijs@thijsschreijer.nl> wrote:
> Depending on the type of system, it requires that a filesystem is present
> and you need to point the LUA_PATH environment variables to your USB
> location.
> 
> According to your example, below the module (in file_y) and program (in
> file_x)
> 
> file_y.lua
> ==========
> local M = {}  -- define module table
> 
> M.func_y = function()    -- define function inside module table
>    print("func_y was called")
> end
> 
> return M    -- return module table
> 
> file_x.lua
> ==========
> local file_y = require("file_y")   -- load your module
> 
> local func_x = function()
>    file_y.func_y()   -- call the function in the other module
>    print("func_x was called")
> end
> 
> -- here is where your program starts
> func_x()  -- call func_x
> 
> Your example is perfect.
> I added:
> 
> package.path = "0:"
> 
> at the beginning of each file because the USB stick root, where the files
> are stored, is seen as "0:".
> 
> By the way I get an "attempt to call a nil value" error message.
> Do I have to set the LUA_PATH in a different way?
> Or is there something else going wrong?

On what line do you get that? On my system it works as expected. But because you do it embedded; have you opened the standard libraries? I suspect that the 'print' function is not available. See [1]

The core engine only has the language constructs, no functions to execute. All functions are located in the standard libraries, but you need to explicitly open them (when embedding Lua in an application) otherwise they won't be available.

In this case 'print' just is a global variable holding the print function. While the libraries haven't been loaded it doesn't exist and any call on it will get you the error "attempt to call a nil value"

Thijs

[1] http://www.lua.org/manual/5.1/manual.html#luaL_openlibs