Including inside a function

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:



Couldn't include file 'modules/hud/init.lua' (File not found)


-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("========================================

“)
ServerLog(”= BlackOps Admin scripts loaded… =
“)
ServerLog(”========================================
")
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("========================================

“)
Msg(”= BlackOps Admin scripts loaded… =
“)
Msg(”========================================
")
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.

[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…”
");
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)

I do a lot of useless things, Mainly 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 very 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!

Making epic things in a shorted time is better then making them in a long time.

Because it’s useless… If you release something like that and people see it they will be like “what… the… fuck…”

[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!?