Lua: require vs. module

Started by
4 comments, last by tokaplan 15 years, 6 months ago
Hi there, I've spent quite some time trying to "include" lua script files into one another, but still I'm confused by "require" and "module" functions. Which one's for what? Thank you
Advertisement
i personally just put requires in. if you just want functions and variables loaded to be used later that should be fine
Well, it looks like I'm gonna have to bother your guys with a little bit more details ;)

So, all I'm trying to do is call a function processEvent2 which is declared in ReliefModificationEndedEET.lua from eventProcessor.lua.


ReliefModificationEndedEET.lua:

function processEvent2( ev, renderer )
return 5
end

eventProcessor.lua:

package.path = "./?.lua;./../Resources/custom/rendering programs/?.lua"
require( "ReliefModificationEndedEET" )

function processEvent( ev, renderer )
return ReliefModificationEndedEET.processEvent2( ev, renderer )
end

When run, processEvent returns a runtime error "...\eventProcessor.lua:7: attempt to index global 'ReliefModificationEndedEET' (a nil value)".

Ok, so require doesn't assign a module to ReliefModificationEndedEET like I expected. If I simply rename ReliefModificationEndedEET.lua to something else, require signals an error "file not found", so it looks like it does find the file.

I've read through Lua 5.1 specification, and it says require puts the module loader output into package.loaded. So I print out package.loaded["ReliefModificationEndedEET"] and get "true". The docs say this is exactly what happens when the module loader returnes nothing after loading module. I have no idea where to look for this "loader" and what my next step may be;)

Any help would be greatly appreciated ;)

Thank you
iirc require doesnt need brackets, it would be require "ReliefModificationEndedEET" but maybe youre right after all

athe main problem seems to be the indirection, to call the function you just use

function processEvent( ev, renderer )
return processEvent2( ev, renderer )
end

as you dont have to redirect

ill have a check now but its slow to double check on things quickly here since i can have one ie window (with a bit of sneakiness to even get that) and its 3 key presses just to get a full stop here, so back in a bit
It's not a case of require vs module, the two work together.

"require" informs Lua that you need this file to continue.
The module function on the other hand provides namespace scoping for your functions by messing with the meta-table for the enviroment of the loaded file (remember; all loaded lua files are pushed onto the stack and executed as a function in order to setup the various functions/globals as such this enviroment table for the function can be messed with and it applies to the defined functions).

The problem you are having is that your 'processEvent2' function isn't defined to be in a module, but as a global function, as such when you try to access 'ReliefModificationEndedEET' the variable doesn't exist as the table was never created during the loading process.

So, either remove 'ReliefModificationEndedEET' from the function call, use the module() function or declare your ReliefModificationEndedEET table and add the functions to it by hand.
NOW I understand how it's working ;) Thanks a lot.

This topic is closed to new replies.

Advertisement