Reputation: 877
I have library file service_functions.lua. There are hundreds of functions in it.
function foo1()
------------
end
function foo2()
------------
end
and so on
I don't want to load all functions.
Now I do:
dofile(script_path.."\\service_functions.lua")
and get all of them.
How to get only specific functions?
Upvotes: 1
Views: 158
Reputation: 2205
but if, as you wrote in the question, you need minimal memory load, then maybe my version will come in handy module:
local arg={...}
local function foo1() print("foo1")
end
local function foo2() print("foo2")
end
local function foo3() print("foo3")
end
local function foo4() print("foo4")
end
local M = {foo1=foo1, foo2=foo2, foo3=foo3, foo4=foo4}
if not arg[1] then return M -- all
elseif type(arg[1])=="string" then
local x = {}
x[arg[1]] = M[arg[1]]
return x -- 1 function by name
elseif type(arg[1])=="table" then
local res = {}
for k,v in pairs(arg[1]) do
res[v] = M[v]
end
return res -- some functions
end
and return only what is needed from "hundreds" of functions
-- only 1 function loaded
local x = loadfile("funk.lua")("foo1")
x.foo1()
-- only 2 functions loaded
local x = loadfile("funk.lua")( { "foo1", "foo2"} )
x.foo1()
x.foo2()
-- all functions loaded
local x = loadfile("funk.lua")()
x.foo1()
x.foo2()
x.foo3()
x.foo4()
Upvotes: 2
Reputation: 28946
Remove any function you don't want/need from service_functions.lua or wrap the library with your own module as shown by lhf
You cannot execute just a part of a Lua file. The file is loaded and exectuted as one chunk.
Alternatively just live with it. It doesn't harm you to have more functions available.
Upvotes: 0
Reputation: 72332
Write your library as follows:
local M={}
function M.foo1()
------------
end
function M.foo2()
------------
end
return M
Then load the library with
local lib=dofile(script_path.."\\service_functions.lua")
and extract the functions you need with
foo1 = lib.foo1
Upvotes: 2