ked2000Join Date: 2011-07-10 Post Count: 1059 |
I have a file that is inside of a folder called 'libs'. I am trying to access the _main file using require. How can I do this?
I have tried:
require "libs/_main"
and
require "libs\_main" |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
Anyone? |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
Help? |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
I am sure there is someone out there who knows. |
|
vlekje513Join Date: 2010-12-28 Post Count: 9057 |
Eh, wut. |
|
DataStoreJoin Date: 2012-02-07 Post Count: 8540 |
The main reason people aren't responding, or so I'd assume, is because you're not being very clear.
If this is regarding RBX.Lua's require function, then you'd use it like (but would differ to what you need):
local Module = require(script.Parent.libs["_main"]) |
|
|
He wants to load a .lua file from his library.
You could do this using loadfile as far as I know. |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
Ok, sorry I didn't explain this earlier. I am writting in Lua, not ROBLOX-styled Lua. Meaning, Lua's require function differs from ROBLOX's require function. |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
@warspyking
Will I have to give the absolute path of the file or just 'libs/_main.lua'.
Besides, shouldn't Lua have this kind of capability with require? |
|
|
Use load;
f = loadfile("C:/file.lua")
f()
Works like that (the wiki says) |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
Tried this:
local user = "#####" --Just for show, my actually name is in 'user'
loadfile("C:/Users/"..user.."/Desktop/Folders/Programming/Lua/Projects/My Framework/_main.lua")
Still doesn't work. |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
Even after I put the (), it doesn't work. |
|
|
F = loadfile("C:/Users/%username%/Desktop/Folders/Programming/Lua/Projects/My Framework/_main.lua")
F()
%username% automatically corrects to the username. |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
Thanks, but it still doesn't work. |
|
|
Try getting rid of ".lua" |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
Yep, did that and it didn't work still. Perhaps I should just move it into the same directory as main.lua? |
|
|
I did some research. Try this;
F = assert(loadfile("C:/Users/%username%/Desktop/Folders/Programming/Lua/Projects/My Framework/_main.lua"))
F() |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
After looking around on stackoverflow I came up with this:
package.path = package.path .. ";C:/Users/#######/Desktop/Folders/Programming/Lua/Projects/MyFramework/?.lua"
require "_main" |
|
|
Did you try the one I showed you with assert? |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
Yes, but it didn't work. I will show you the script I am working on. I did not make this script, I edited it. It was made by TheLuaWeaver in his framework(OO).
----------------------------------------
package.path = package.path .. ";C:/Users/#######/Desktop/Folders/Programming/Lua/Projects/MyFramework/?.lua"
require "main"
----------------------------------------------------------------------
package "Classes" --Define as a package
----------------------------------------------------------------------
addException("ClassNotFound", "Class ['~1~'] was not located") --Add important exceptions
----------------------------------------------------------------------
-- @Public
----------------------------------------------------------------------
function class(arg0)
return function(arg1)
if (type(arg1) == "string") then
return function(arg2)
Private.addClass(arg0, arg1, arg2)
end
else
Private.addClass(arg0, "base", arg2)
end
end
end
----------------------------------------------------------------------
-- @Private
----------------------------------------------------------------------
local Classes = {
["base"] = {
mt = {
__toString = function(self)
return "Class ['"..self.ClassName.."']";
end;
};
};
}
local Private = {}
Private.addClass = function(name, extends, args)
if (Classes[extends]) then
args = Private.inherit(args, Classes[extends])
else
throwException("ClassNotFound", name)
end
Classes[name] = args;
args["ClassName"] = name
setmetatable(Private.inherit({}, args), {
__call = function()
local tab = Private.inherit({}, Classes[name])
setmetatable(tab, tab.mt)
return tab
end;
}
)
end
Private.inherit = function(tab0, tab1)
for i, v in pairs(tab1) do
tab0[i] = tab0[i] or (type(v) == "table" and Private.inherit({}, v) or v)
end
return tab0
end
os.execute("pause") --To check if it passes without errors. |
|
|
package.path = package.path .. "C:/Users/%username%/Desktop/Folders/Programming/Lua/Projects/MyFramework/_main.lua" |
|
|
If it STILL don't work, try using dofile. |
|
ked2000Join Date: 2011-07-10 Post Count: 1059 |
No luck. |
|
|
Did you attempt to use dofile? |
|
|
If you still want to attempt require, here's a good link;
http://www.lua.org/pil/8.1.html |
|