Are variables shared between files

I’m working on a gamemode, and my question here is are variables and functions shared between the files? I know serverside variables don’t go over to clientside, but if you’re including multiple serverside files, will your variables & functions share between those?

Yes, everything is in the same pool, hence why you should always make variables local if they aren’t used elsewhere, to avoid conflicts in naming.

Any way to make a variable local to a specific file?

local var = x

Local.

var = x

Global.

Var can also be a function.

Actually, technically no. Variables are not shared between files, they are only shared in their scope (checkout: http://lua-users.org/wiki/ScopeTutorial). For example, there is a variable in a wiremod file called “GateAction” if you use this variable name in init.lua or cl_init.lua you will not run into any problems.

See though, in init.lua you include all your serverside files. e.g. include(“shared.lua”). Then all variables in shared.lua become included in init.lua. Gmod only executes one (cl/init.lua) script and that script then includes everything else.

Basically only init.lua or cl_init.lua respectively execute, all the other files are included in them, hence the variables can be seen.

It is bad programming practice to reference variables between files in such a manner, for example
Print Functions.lua:
[lua]
Msg(FooPrint)
[/lua]

DefineVariables.lua
[lua]
FooPrint = “Foo!”
[/lua]

init.lua
[lua]
include(“PrintFunctions.lua”)
include(“DefineVariables.lua”)
Msg(FooPrint)
[/lua]

You’ll get an error about FooPrint not being declared yet in PrintFunctions.lua