Auto-Including files for gamemode problems

So I made a quick system similar to DarkRP’s where the depending on the file prefix (sv/cl/sh) it will auto load the files to their respective realm.

[lua]
//Load all files

local gmfolder = “nzombies”

local _,dirs = file.Find( gmfolder…"/gamemode/*", “LUA” )

function RecInclude(name, dir)
local sep = string.Explode("_", name)
name = dir…name
if sep[1] == “sv” then
if SERVER then
include(name)
end
elseif sep[1] == “sh” then
if SERVER then
AddCSLuaFile(name)
include(name)
else
include(name)
end
elseif sep[1] == “cl” then
if SERVER then
AddCSLuaFile(name)
else
include(name)
end
end
print("Including: "…name)
end

if SERVER then
for k,v in pairs(dirs) do
local f2,d2 = file.Find( gmfolder…"/gamemode/"…v…"/*", “LUA” )
for k2,v2 in pairs(f2) do
RecInclude(v2, v…"/")
end
end
end

if CLIENT then
for k,v in pairs(dirs) do
local f2,d2 = file.Find( gmfolder…"/gamemode/"…v…"/*", “LUA” )
for k2,v2 in pairs(f2) do
RecInclude(v2, v…"/")
end
end
end
[/lua]

Serverside Console output



Including: config/sh_config.lua
Including: enemies/sv_enemies.lua
Including: entity_spawning/sv_spawnents.lua
Including: hud/cl_fonts.lua
Including: hud/cl_hud.lua
Including: players/sv_chatcommands.lua
Including: players/sv_players.lua
Including: points/sh_meta.lua
Including: resources/sv_resources.lua
Including: rounds/cl_round.lua
Including: rounds/sv_round.lua
Including: spectate/sv_spectate.lua


Client Side



Including: config/sh_config.lua
Including: hud/cl_fonts.lua
Including: hud/cl_hud.lua
Including: points/sh_meta.lua
Including: rounds/cl_round.lua


So it seems like its working; However, the code within the included files (timers, functions), don’t seem to run until a change has been made (Lua autorefresh). It will then run fine until I reload completely. Any ideas on what causes this or how to fix it?

Since the client and server code is identical, I’d get rid of the if SERVER and if CLIENT and just use the include code…

Can you show us your folder structure and how you have it pieced together?

I recommend doing something like this, where init.lua and cl_init.lua points to sh_init.lua ( or shared.lua ) and then loads all the data. Understanding how code executes for each realm will help in creating a recursive auto-include system. If the client executes shared code, it is only executed on the client, the same is true for server… So you don’t need if CLIENT and if SERVER except for client / server only code such as the if SERVER for AddCSLuaFile

Here’s a basic game-mode skeleton which includes an recursive-auto-include system which supports client/shared/server and maps/ folders. This one has repetitive code to aide in learning.
https://dl.dropboxusercontent.com/u/26074909/tutoring/_redistributable/basedev_gamemode.rar

This one has no repetitive code…:
I’m about to release an advanced one that supports many more options ( https://dl.dropboxusercontent.com/u/26074909/tutoring/_redistributable/basedev_gamemode/documentation/file_recursive_inclusions.txt ) including prefixes, postfixes, NOLOAD options, folders, etc…

Folder structure:
https://dl.dropboxusercontent.com/u/26074909/tutoring/_redistributable/basedev_gamemode/documentation/folder_structure_and_how_to_use_it.txt

I use two functions for adding every file in a directory and sub directory.

[LUA]
local function SearchAndInclude( main_dir )
local files, dirs = file.Find( main_dir…"/*", “LUA” )
for _, file in pairs( files ) do
include( main_dir … “/” … file )
end
for _, dir in pairs( dirs ) do
SearchAndInclude( main_dir … “/” … dir )
end
end

local function SearchAndAdd( main_dir )
local files, dirs = file.Find( main_dir…"/*", “LUA” )
for _, file in pairs( files ) do
AddCSLuaFile( main_dir … “/” … file )
end
for _, dir in pairs( dirs ) do
SearchAndAdd( main_dir … “/” … dir )
end
end
[/LUA]