Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Referencing a module's file name from with the module
Started by sporkfin Dec 20 2019 09:22 AM

- - - - -
5 replies to this topic

Best Answer StarCrunch , 20 December 2019 - 09:57 AM

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.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 711 posts
  • Corona SDK

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 = {}
 
M.name = -- I need "creatures.lua" here
print(M.name) --> creatures.lua
 
return M
 



[TOPIC: post.html]
#2

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 842 posts
  • Corona SDK

  Best Answer

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.


  • Michael Flad and sporkfin like this

[TOPIC: post.html]
#3

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 711 posts
  • Corona SDK

Haha!  I expected to do some string manipulation,

 

That was way too easy, thanks!



[TOPIC: post.html]
#4

pixec

[GLOBAL: userInfoPane.html]
pixec
  • Contributor

  • 102 posts
  • Corona SDK

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?

[TOPIC: post.html]
#5

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 842 posts
  • Corona SDK

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.


  • pixec likes this

[TOPIC: post.html]
#6

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 711 posts
  • Corona SDK

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!


  • StarCrunch likes this


[topic_controls]
[/topic_controls]