• How does including files work.
    17 replies, posted
I see ULX, DarkRP, TTT, pretty much any big gmod addon. How does it work? What's the point? I was fooling around with something like this but it didn't work. file1.lua: [CODE] include( "file2.lua" ) print( "this file loaded" ) my_test_var1 = "test1" print( my_test_var2 ) --prints nil [/CODE] file2.lua: [CODE] print( "this file loaded" ) my_test_var2 = "test2" print( my_test_var1 ) --this prints nil. [/CODE] Am I doing it wrong? Am I doing it correctly but using it wrong? I'm confused.
[QUOTE=CallMePyro;43306781]I see ULX, DarkRP, TTT, pretty much any big gmod addon. How does it work? What's the point? I was fooling around with something like this but it didn't work. file1.lua: [CODE] include( "file2.lua" ) print( "this file loaded" ) my_test_var1 = "test1" print( my_test_var2 ) --prints nil [/CODE] file2.lua: [CODE] print( "this file loaded" ) my_test_var2 = "test2" print( my_test_var ) --this prints nil. [/CODE] Am I doing it wrong? Am I doing it write but using it wrong? I'm confused.[/QUOTE] Files included have 3 different types - clientside, serverside, and then this one is really just both, but shared. If it's serverside, you just use include. If it's clientside, you have to AddCSLuaFile the file on the server, and then include it clientside. For shared, just do both. (Quick thing on your printing - The 2nd file is included before you set my_test_var1, so it's nil when it runs. Also, it's test_var1 in the first file, and no 1 in the 2nd.)
[QUOTE=Ghost_Sailor;43306803]Files included have 3 different types - clientside, serverside, and then this one is really just both, but shared. If it's serverside, you just use include. If it's clientside, you have to AddCSLuaFile the file on the server, and then include it clientside. For shared, just do both. (Quick thing on your printing - The 2nd file is included before you set my_test_var1, so it's nil when it runs. Also, it's test_var1 in the first file, and no 1 in the 2nd.)[/QUOTE] Alright. So say I had a table that I wanted to be shared across multiple clientside files. Without simply copy pasting that table to each file, how would I get the files to load it? Edit: Oviously ULX does this, with the global ULX table. How do I replicate that? I looked at their code and didn't see anything stick out to me.
It works as if you copy pasted the file contents instead of the include function, it's purpose to divide code into parts - gui in one file, server mechanics another, some awesome library in third, etc.
sv_func.lua [lua]function hi() print("hi") end[/lua] init.lua [lua] include("sv_func.lua") hi() [/lua] hi() would print hi
[QUOTE=zerothefallen;43306899]sv_func.lua [lua]function hi() print("hi") end[/lua] init.lua [lua] include("sv_func.lua") hi() [/lua] hi() would print hi[/QUOTE] Ok, awesome. So it seems like I'm conglomerating all this data into my init.lua or cl_init.lua when I do this. But does that mean that the individual files have access to eachother's data? So in your example, if I included a second file into init.lua, would that file be able to run hi() and have it print? If not, how would I achieve that? simply including the files I want every time?
[QUOTE=CallMePyro;43306961]Ok, awesome. So it seems like I'm conglomerating all this data into my init.lua or cl_init.lua when I do this. But does that mean that the individual files have access to eachother's data? So in your example, if I included a second file into init.lua, would that file be able to run hi() and have it print? If not, how would I achieve that? simply including the files I want every time?[/QUOTE] If you do something like [code]SteveJobs = {} SteveJobs.Enemies = {"Bill Gates"}[/code] in one file, you can then do [code]PrintTable(SteveJobs)[/code] From another, and you'll have it shared. If you want a variable/function only in one file, make it local. If it isn't made local, anything in that realm can access that variable/function.
[QUOTE=Ghost_Sailor;43307301]If you do something like [code]SteveJobs = {} SteveJobs.Enemies = {"Bill Gates"}[/code] in one file, you can then do [code]PrintTable(SteveJobs)[/code] From another, and you'll have it shared. If you want a variable/function only in one file, make it local. If it isn't made local, anything in that realm can access that variable/function.[/QUOTE] Ah. I thought I might need to use _G or some other method of global indexing. Thanks [editline]25th December 2013[/editline] Alrighty I feel pretty dumb. I'm trying to copy ULX's method of adding files. I can get my server files to be included and whatnot, but I just can't get my clientside files to load at all. I had it all working just fine when it was kind of jumbled into the autorun client and server folders, but obviously this is much nicer and more modular, as well as expandable. directories are this: addons/myaddon/lua/dir/ contains: init.lua, cl_init.lua, cl (IsDir), sv(IsDir) and sh(IsDir) cl, sh, and sv all contain serveral lua files. They run fine and do not produce any errors. my init.lua code looks like this: [CODE] -- Add Server files local sv_files = file.Find( "dir/sv/*.lua", "LUA" ) for _, file in ipairs( sv_files ) do include( "sv/" .. file ) end -- Add Shared Files local sh_files = file.Find( "dir/sh/*.lua", "LUA" ) for _, file in ipairs( sh_files ) do include( "sh/"..file ) end -- Add CS Lua files local cl_files = file.Find( "dir/cl/*.lua", "LUA" ) AddCSLuaFile( dir/cl_init.lua" ) for _, file in ipairs( cl_files ) do AddCSLuaFile( "dir/cl/"..file ) end MsgN( "Client files loaded." ) [/CODE] And my cl_init.lua looks like this: [CODE] MsgN( "cl_init loaded" ) --doesn't print -- Include shared local sh_files = file.Find( "adv_mv/sh/*.lua", "LUA" ) for _, file in ipairs( sh_files ) do include( "sh/"..file ) end -- Include clientside local cl_files = file.Find( "dir/cl/*.lua", "LUA" ) for _, file in ipairs( cl_files ) do include( "cl/"..file ) end [/CODE] Serverside loads fine. No errors, no missing files. It doesn't even error on the AddCSLuaFile for the cl_init. But cl_init simply doesn't run. I'm confused.
[CODE][/CODE]You use [B]AddCSLuaFile[/B] for [U]clientside[/U] scripts (AddClientSideLuaFile) [B]include[/B] you can use for both [U]shared[/U] and [U]serverside[/U] files you just include the files in the init Like if you want to include sv_round you place sv_round in the same map as your include(script/line) and then you just do like this [CODE]include("sv_round.lua")[/CODE] Have Fun
[QUOTE=CallMePyro;43307590]Ah. I thought I might need to use _G or some other method of global indexing. Thanks [editline]25th December 2013[/editline] Alrighty I feel pretty dumb. I'm trying to copy ULX's method of adding files. I can get my server files to be included and whatnot, but I just can't get my clientside files to load at all. I had it all working just fine when it was kind of jumbled into the autorun client and server folders, but obviously this is much nicer and more modular, as well as expandable. directories are this: addons/myaddon/lua/dir/ contains: init.lua, cl_init.lua, cl (IsDir), sv(IsDir) and sh(IsDir) cl, sh, and sv all contain serveral lua files. They run fine and do not produce any errors. my init.lua code looks like this: [CODE] -- Add Server files local sv_files = file.Find( "dir/sv/*.lua", "LUA" ) for _, file in ipairs( sv_files ) do include( "sv/" .. file ) end -- Add Shared Files local sh_files = file.Find( "dir/sh/*.lua", "LUA" ) for _, file in ipairs( sh_files ) do include( "sh/"..file ) end -- Add CS Lua files local cl_files = file.Find( "dir/cl/*.lua", "LUA" ) AddCSLuaFile( dir/cl_init.lua" ) for _, file in ipairs( cl_files ) do AddCSLuaFile( "dir/cl/"..file ) end MsgN( "Client files loaded." ) [/CODE] And my cl_init.lua looks like this: [CODE] MsgN( "cl_init loaded" ) --doesn't print -- Include shared local sh_files = file.Find( "adv_mv/sh/*.lua", "LUA" ) for _, file in ipairs( sh_files ) do include( "sh/"..file ) end -- Include clientside local cl_files = file.Find( "dir/cl/*.lua", "LUA" ) for _, file in ipairs( cl_files ) do include( "cl/"..file ) end [/CODE] Serverside loads fine. No errors, no missing files. It doesn't even error on the AddCSLuaFile for the cl_init. But cl_init simply doesn't run. I'm confused.[/QUOTE] You're supposed to AddCSLuaFile and include shared files on the server, not just include them.
[QUOTE=brandonj4;43308208]You're supposed to AddCSLuaFile and include shared files on the server, not just include them.[/QUOTE] Apologies for being slow. So I've amended the code to also run AddCSLuaFile on my shared files also. However, that wasn't the issue, or atleast didn't fix it. Currently my cl_init.lua isn't even being loaded. At no point is [CODE] MsgN( "cl_init loaded" ) [/CODE] run, nor is any of the code below it.
[QUOTE=sm69baller;43308206][CODE][/CODE]You use [B]AddCSLuaFile[/B] for [U]clientside[/U] scripts (AddClientSideLuaFile) [B]include[/B] you can use for both [U]shared[/U] and [U]serverside[/U] files you just include the files in the init Like if you want to include sv_round you place sv_round in the same map as your include(script/line) and then you just do like this [CODE]include("sv_round.lua")[/CODE] Have Fun[/QUOTE] DOn't forget to do AddCSLuaFile shared files
[QUOTE=CallMePyro;43308284]Apologies for being slow. So I've amended the code to also run AddCSLuaFile on my shared files also. However, that wasn't the issue, or atleast didn't fix it. Currently my cl_init.lua isn't even being loaded. At no point is [CODE] MsgN( "cl_init loaded" ) [/CODE] run, nor is any of the code below it.[/QUOTE] Just put AddCSLuaFile("dir/cl_init.lua") in your init file, also in your cl_init you have "adv_mv/sh/*.lua". Not sure if that's a mistake or you forgot to change it.
[QUOTE=brandonj4;43308346]Just put AddCSLuaFile("dir/cl_init.lua") in your init file, also in your cl_init you have "adv_mv/sh/*.lua". Not sure if that's a mistake or you forgot to change it.[/QUOTE] Oh that's a mistake. It's correct in my code. I'll add the cl_init file. brb edit: Still didn't run. Here's updated code. my init.lua code looks like this: [CODE] -- Add Server files local sv_files = file.Find( "dir/sv/*.lua", "LUA" ) for _, file in ipairs( sv_files ) do include( "sv/" .. file ) end -- Add Shared Files local sh_files = file.Find( "dir/sh/*.lua", "LUA" ) for _, file in ipairs( sh_files ) do include( "sh/"..file ) AddCSLuaFile( "sh/"..file ) end -- Add CS Lua files local cl_files = file.Find( "dir/cl/*.lua", "LUA" ) AddCSLuaFile( "dir/cl_init.lua" ) for _, file in ipairs( cl_files ) do AddCSLuaFile( "dir/cl/"..file ) end MsgN( "Client files loaded." ) [/CODE] And my cl_init.lua looks like this: [CODE] MsgN( "cl_init loaded" ) --doesn't print (still) -- Include shared local sh_files = file.Find( "dir/sh/*.lua", "LUA" ) for _, file in ipairs( sh_files ) do include( "sh/"..file ) end -- Include clientside local cl_files = file.Find( "dir/cl/*.lua", "LUA" ) for _, file in ipairs( cl_files ) do include( "cl/"..file ) end [/CODE]
[QUOTE=CallMePyro;43308392]Oh that's a mistake. It's correct in my code. I'll add the cl_init file. brb edit: Still didn't run. Here's updated code. my init.lua code looks like this: [CODE] -- Add Server files local sv_files = file.Find( "dir/sv/*.lua", "LUA" ) for _, file in ipairs( sv_files ) do include( "sv/" .. file ) end -- Add Shared Files local sh_files = file.Find( "dir/sh/*.lua", "LUA" ) for _, file in ipairs( sh_files ) do include( "sh/"..file ) AddCSLuaFile( "sh/"..file ) end -- Add CS Lua files local cl_files = file.Find( "dir/cl/*.lua", "LUA" ) AddCSLuaFile( "dir/cl_init.lua" ) for _, file in ipairs( cl_files ) do AddCSLuaFile( "dir/cl/"..file ) end MsgN( "Client files loaded." ) [/CODE] And my cl_init.lua looks like this: [CODE] MsgN( "cl_init loaded" ) --doesn't print (still) -- Include shared local sh_files = file.Find( "dir/sh/*.lua", "LUA" ) for _, file in ipairs( sh_files ) do include( "sh/"..file ) end -- Include clientside local cl_files = file.Find( "dir/cl/*.lua", "LUA" ) for _, file in ipairs( cl_files ) do include( "cl/"..file ) end [/CODE][/QUOTE] Are you even including your init.lua in autorun?
I am indeed. Obviously a very simple file which is why I didn't show it. autorun/server/dir_init.lua [CODE] include( "dir/init.lua" ) [/CODE] edit: Also, my server init file does run correctly, as far as I can tell. I put debug print functions into all of the AddCSLuaFile loops and each one printed correctly. Simply the cl_init is not running.
[QUOTE=CallMePyro;43308665]I am indeed. Obviously a very simple file which is why I didn't show it. autorun/server/dir_init.lua [CODE] include( "dir/init.lua" ) [/CODE][/QUOTE] Did you include the cl_init in autorun? Your easiest reference can be to look at ulib and see how they did it, even though it's not that complicated.
My god. I'm glad to see that my shrine of you that I built in my garage has not been wasted. Thanks for helping me fix this, much appreciated.
Sorry, you need to Log In to post a reply to this thread.