• Explain to me how configs work?
    9 replies, posted
Hi, I am making a script and I am needing to make a config. I made a file named "sh_config.lua" located in "addonname/lua/sh_config.lua" and added it to cs lua. I am wanting to make it where the user can type in jobs like "Citizen" and such and then target the jobs they typed in the config in my code. How would I do that?
Most likely you'd use a global table or return the table in the file to be used with [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/include]include[/url].
Yeah I understand that but what is the specific way of doing it. Is this it? CONFIG [CODE] JobsToTarget = { "Citizen", "Civil Protect", } [/CODE] SERVER [CODE] for k,v in pairs(JobsToTarget) v:Blah BLah Blah, whatever to the job. end [/CODE]
A config file is a file that makes it easy to edit certain aspects of whatever you're making. Say you are making a vehicle or something and you wanted other people to edit certain aspects of it. Your config might look like this - [code] vehicle = {} vehicle.speed = 100 vehicle.weight = 200 [/code] There are better ways of implementing settings like that but I think you get the idea. You should never have to loop through your config file settings, unless it was a pretty specific situation. For example, if I was making a gamemode and I wanted to give users the ability to add certain things, like player models, then I would do something similar to what you did above. Something maybe like this: [code] gamemode = {} gamemode.rounds = 5 // this is just for show gamemode.playermodels = { "player/model1/path.mdl", "player/model2/path.mdl", "player/model3/path.mdl" } [/code]
Ok so the config what I said would work, what about accessing every single thing in the table for use in server.lua
Accessing the entire table at once server side is just a loop. The way you did above works just fine.
Yeah so I did it and it does work but I came across another thing I need to fix. How do I access specific ones? Heres what I mean : CONFIG [CODE] color = { r = 255 g = 0 b = 0 } [/CODE] SERVER [CODE] local color = Color(r, g, b) [/CODE] Get what I mean? I want to access specific ones.
Accessing a variable from within a table can be done like this - [code] local color = Color( color.r, color.g, color.b ) [/code] Edit: If your table doesn't have a defined index, or you structured the table this way: [code] table = { "Mary", "Had", "Lamb" } // To access the table: var = table[1] // or var = table[2] [/code] All of this is information you can find if you search for it though. This should help you - [url]https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index2584.html[/url] It's the old wiki, but still has a lot of useful information in it.
oh okay thankyou!!! [editline]10th August 2017[/editline] Well now I got a error :( [CODE] ConfigM = { OpeningText = "Blah " -- Set to "" for nothing. =1= JobText = "M" -- The text of the job in chat. =2= UseUserName = true -- Use the user name? =3= OpenColor = Color(182, 0, 0) -- The opening Color that is used in 1, 2, 3 CloseColor = Color(255, 255, 255) -- The closing Color that is used in 4 } [/CODE] ERROR : [CODE] [ERROR] addons/namer/lua/sh_config.lua:62: '}' expected (to close '{' at line 60) near 'JobText' 1. unknown - addons/namer/lua/sh_config.lua:0 [/CODE] [editline]10th August 2017[/editline] This is whats using JobText and stuff (CLIENT) : -- EDIT : I mean client lol [CODE] if callerType == 1 then for k, v in pairs(JobsForMedic) do if LocalPlayer():getDarkRPVar("job") == v then if ConMedic.UseUserName == true then chat.AddText(ConMedic.OpenColor, ConMedic.OpeningText .. ConMedic.JobText .. LocalPlayer():Nick(), ConMedic.CloseColor, callerMessage) else chat.AddText(ConMedic.OpenColor, ConMedic.OpeningText .. ConMedic.JobText, ConMedic.CloseColor, callerMessage) end end end end [/CODE] [editline]10th August 2017[/editline] Its because I didnt have the commas :(
You need a comma at the end of each line, before your comments, in your config EDIT: Didn't see you already solved it, my bad
Sorry, you need to Log In to post a reply to this thread.