• A quick Lua Question
    5 replies, posted
So I am confused about gamemodes and the lua files in it. I am Confused Because from what I have learned that gamemodes can only use the normal 3 files (shared server and client), But Why do other Gamemode have more lua files than just those 3? What Would be the use of doing that?
You can have as many files as you want - the only two that are absolutely required are cl_init and init. The reason people tend to split their gamemodes into a larger amount of files is to make them more easily maintained - imagine if darkrp was jammed into cl_init and init and had no other files - how would you remember where in either file anything is? One line 23K or 27K? If you put them into an organized structure, it becomes easier - you put all your server hook stuff in sv_hooks. You put your hud in cl_hud. So if you want to go in and adjust something, you don't need to root through a titanic file, just open the one that has the name that matches what you want.
Ok so if I wanted to add more files too keep things More organized then how would i do it?
Use this, include("File.lua") [url]http://wiki.garrysmod.com/page/Global/include[/url]
I'd recommend setting up a folder-structure you're comfortable with, and then it is as easy as just including files, and AddCSLuaFiles that the client gets ( shared / client ). Here are two helper-functions which do the same thing, one shorter / more concise than the other. ( second one which essentially lets you put all includes in open, not in if/else, and define the realm as an argument. It does the rest. ). [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/loading_files_across_realms_simple.lua.html[/url] I'd suggest setting init.lua and cl_init.lua to do minimal work ( load sh_init.lua or shared.lua, whichever you prefer, then use shared to load files based on realm... ) At the top, it shows how I set up the 3 basic files. Then I do all my includes in sh_init.lua. It makes it easier because shared files are out in the open, server / client files are in an if / else. Addons files ( addons to the game-mode ), then content files ( content ie non-important stuff ) last. [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/loading_files_across_realms.lua.html[/url] One nice way to do it, is to have something load files for you; feel free to contact me I can set you up with something that automatically loads files for you; no need to type any include / AddCSLuaFile ever again!
I agree with Acecool .. its easier to make a system. Since I always make the files start with "sv_", "sh_" or "cl_" I can use a function like this: (Remember to call it on both sides) [code]-- sv_file.lua Will only run serverside. -- sh_file.lua Will get send and run on both sides. -- cl_file.lua Will get send and only run clientside. function SmartInclude(_file) local _type = string.sub(fl,0,3) if SERVER and (_type == "cl_"||_type == "sh_") then AddCSLuaFile(_type) -- Make the client download the file end if _type =="sh_" or (SERVER and _type !="cl_") or (CLIENT and _type !="sv_") then include(_file) -- Make the right side run the given file end end[/code]
Sorry, you need to Log In to post a reply to this thread.