• Including inside a function
    23 replies, posted
I got this code: [lua] include("modules/hud/init.lua") function MRP:LoadModules() MRP:Print("Loading modules") for v,k in pairs(file.FindDir("../gamemodes/macrp/gamemode/modules/*")) do MRP:Print("modules/"..k.."/init.lua") include("modules/"..k.."/init.lua") include("modules/hud/init.lua") end end [/lua] Above the LoadModules function it includes just fine, but inside the for loop it just fails, both the includes fail. Any ideas what the hell is going on?
It fails because it errors, or it fails because it doesn't include correctly?
No errors at all. And its the same path? Error im getting: [code] Couldn't include file 'modules/hud/init.lua' (File not found) [/code]
-snip- didn't read first post properly.
Bump, since the forums fills incredibly fast!
Here's a good example. I use this on my server. [lua]if(SERVER) then function LoadServerSideScripts() AddCSLuaFile("autorun/BAdmin.lua") for k, v in pairs( file.FindInLua( "BlackAdmin/shared/*.lua" ) ) do AddCSLuaFile( "BlackAdmin/shared/"..v ) end for k, v in pairs( file.FindInLua( "BlackAdmin/client/*.lua" ) ) do AddCSLuaFile( "BlackAdmin/client/"..v ) end for k, v in pairs( file.FindInLua( "BlackAdmin/shared/*.lua" ) ) do include( "BlackAdmin/shared/"..v ) end for k, v in pairs( file.FindInLua( "BlackAdmin/server/*.lua" ) ) do include( "BlackAdmin/server/"..v ) end ServerLog("========================================\n") ServerLog("= BlackOps Admin scripts loaded.. =\n") ServerLog("========================================\n") end else function LoadClientSideScripts() for k, v in pairs( file.FindInLua( "BlackAdmin/shared/*.lua" ) ) do include( "BlackAdmin/shared/"..v ) end for k, v in pairs( file.FindInLua( "BlackAdmin/client/*.lua" ) ) do include( "BlackAdmin/client/"..v ) end Msg("========================================\n") Msg("= BlackOps Admin scripts loaded.. =\n") Msg("========================================\n") end end [/lua]
Here's a peace of my library include from SCHRP .. [lua] -- Create our global library table. Library = {} -- Lets create our library including function Library.IncludePath( path ) include( path ) end [/lua] Then after that you can do something like this .. [lua] MRP = {} function MRP.IncludeModules() for k,v in pairs(file.FindDir("../gamemodes/macrp/gamemode/modules/*")) do MRP:Print("Included module library path: modules/"..k.."/init.lua") Library.IncludePath("modules/"..k.."/init.lua") Library.IncludePath("modules/hud/init.lua") end end [/lua] That's just what I would do, but then again I do a lot of unnecessary shit because I never remember what I'm doing and for what.
[QUOTE=schumacher;18859411]Here's a peace of my library include from SCHRP [lua] -- Create our global library table. Library = {} -- Lets create our library including function Library.IncludePath( path ) include( path ) end [/lua] Then after that you can do something like this [lua] MRP = {} function MRP.IncludeModules() for k,v in pairs(file.FindDir("../gamemodes/macrp/gamemode/modules/*")) do MRP:Print("Included module library path: modules/"..k.."/init.lua") Library.IncludePath("modules/"..k.."/init.lua") Library.IncludePath("modules/hud/init.lua") end end [/lua] That's just what I would do, but then again i do alot of unnecessary shit because I never remember what I'm doing and for what.[/QUOTE] [lua] -- Create an unnecessary global table Library = {} -- Create a useless function that does nothing but run a function with an easier to type name with the same parameters function Library.IncludePath( path ) include( path ) end [/lua] Why would you do this, let alone suggest that someone else does? [editline]07:09PM[/editline] @OP Where are you calling the function from? include() will scope from that file, rather than the root gamemode folder, so you'll need to take that into account with your paths.
The file is inside the root gamemode folder, it includes just fine just above the function... (same file)
First of all, if you're only including init.lua of each "Module" why does each one need its own folder when you could simple name the Lua file after the module and use this. [lua] function MRP.IncludeModules() local k, v; for k,v in pairs(file.FindInLua("GamemodeName/gamemode/modules/*.lua")) Msg("Found -> "..v.."\n"); include("modules/"..v); end end [/lua]
Because each module got several files, but there is no need to randomly include everything when each module can take care of it itself.
Bump, since im now on the second page (google statistics says that sucks)
[QUOTE=Lexic;18860968][lua] -- Create an unnecessary global table Library = {} -- Create a useless function that does nothing but run a function with an easier to type name with the same parameters function Library.IncludePath( path ) include( path ) end [/lua] Why would you do this, let alone suggest that someone else does? [editline]07:09PM[/editline] @OP Where are you calling the function from? include() will scope from that file, rather than the root gamemode folder, so you'll need to take that into account with your paths.[/QUOTE] I do a lot of useless things, [b]Mainly[/b] because I'm retarded and i forget what I'm including for half the time and if I do that, i can just read the prefix there and it tells me. It was a direct copy of code from my SCHRP gamemode so it seems [b]very[/b] unneeded, which really, it is, but i use it because i have alot of different things I include for.
It has no meaning, the code you posted.
schumacher Your code does this. [lua] include(path) [/lua] So why would anyone want to type something longer. [lua] Library.IncludePath(path) == include(path) [/lua] It's a pretty big waste of time.
Cause then you can brag about how long it took to make it!
Because it's useless.. If you release something like that and people see it they will be like "what... the... fuck..."
[QUOTE=aualin;18944013]Cause then you can brag about how long it took to make it![/QUOTE] [Insert Gmod Tower reference here]
I messed around a little and noticed that if the code is not inside a function it works perfectly fine, like this: [lua] MRP:Print("Loading modules") for v,k in pairs(file.FindDir("../gamemodes/macrp/gamemode/modules/*")) do MRP:Print("modules/"..k.."/init.lua") include("modules/"..k.."/init.lua") include("modules/hud/init.lua") end [/lua] Now i have to ask, WHAT IS WRONG WITH THIS WORLD!?
I figured this one out, it won't work when i call it from anything else than directly in the file outside any kind of block... This means no hooks for me...
Are you even calling MRP.LoadModules() or are you just creating the function and hoping it will magically run when you load the server? Serverside stuff should be able to be called from any serverside file.. If not, then you're doing it wrong.
I called the function in a hook, sadly that did not work, it works in a do block. Right now i call the function at the end of the file
-Snip-
Sorry, you need to Log In to post a reply to this thread.