Load the Lua script into a table named filename
I load scripts using luaL_loadfile
and then lua_pcall
from my game and was wondering, instead of loading them into a global table, I could load them into a table with a name after the filename?
For instance:
I have a file called "Foo.lua" that contains the following:
function DoSomething()
--something
end
Once downloaded, I want to be able to access it, for example:
Foo.DoSomething()
Thanks!
a source to share
Where they go is determined by the Lua code itself - if a file declares it in the global namespace, it will go into the global namespace when the file is run through pcall.
The simplest option would be to encourage people who write Lua files to create their own namespaces (just Foo = {}
at the beginning of the file, and then later declare functions as Foo.whatever).
If you want to force things into a private namespace, you have to complicate things a bit - basically, find what changed in the global namespace after the file was run, and manually move the new items to the private namespace.
a source to share