Referencing a module's file name from with the module

I’d like my modules to be able to reference the file from which they came.  Is there a way to do this?  For example:


— module  creatures.lua

local M = {} = – I need “creatures.lua” here

print( --> creatures.lua

return M


You can do this somewhere (outside a vararg function, of course):

local name = ...

It will pick up any sub-directory and separate them with dots.

Haha!  I expected to do some string manipulation,

That was way too easy, thanks!

I just tried that and it is super cool and all but I dont fully understand it.

Doesnt ”…” take all arguments thrown into a function and you would then list them out inside the function like ”t = {…}”. Is that whenever a file is required, then the require function’s argument string is passed and it is got like that or is it something else?

In the docs for require() you see the comment “Once a loader is found, require() calls the loader with a single argument of modname.”

While not something you normally think about, a module is actually the body of a function. Since it has no named parameters, you get it through . Maybe arg will also work; I tend to avoid it myself since it’s a compatibility holdover from an older Lua.

FYI - “arg” didn’t work for me but “…” has added just the little bit of flexibility I needed to avoid many hours of work - thanks again!